关于ASP.NET Gridview

时间:2009-10-04 17:06:29

标签: c# asp.net gridview

您好我需要帮助在c#中使用Gridview控件实现逻辑。

我有一个gridview,它有很多行。每行都有一个按钮,可以为用户单击。在每个按钮上单击我正在更新数据库中的选定记录。现在一旦行更新,我需要隐藏该按钮以防止对该特定行的反应 1.如果我使用这个

<asp:CommandField ShowEditButton="True" EditText="select" />

,我无法隐藏。 2.如果我使用这个

<asp:TemplateField HeaderText="Your Action">
  <ItemTemplate>
    <asp:Button
        ID="btnAccept"
        runat="server"
        Text="Accept" 
        OnClientClick="return confirm('Are you sure you want to Accept this offer?');" 
        onclick="btnAccept_Click" />
  </ItemTemplate>
</asp:TemplateField>

,我无法获得所选的行索引。

我希望我已经清除了我想要的东西。提前谢谢。

2 个答案:

答案 0 :(得分:3)

使用Button控件的CommandArgument属性指定用户单击的行:

<asp:TemplateField HeaderText="Your Action">
  <ItemTemplate>
    <asp:Button
        ID="btnAccept"
        runat="server"
        Text="Accept" 
        OnClientClick="return confirm('Are you sure you want to Accept this offer?');" 
        CommandName="Accept"
        CommandArgument='<%# Eval("RowId") %>'
        onclick="btnAccept_Click" />
  </ItemTemplate>
</asp:TemplateField>

代码背后:

void btnAccept_Click(Object sender, CommandEventArgs e) 
{
    if (e.CommandName == "Accept")
    {
       string rowId = e.CommandArgument;
    }
}

答案 1 :(得分:2)

继续使用Canavars解决方案1):

void btnAccept_Click(Object sender, CommandEventArgs e) 
{
    if (e.CommandName == "Accept")
    {
       string rowId = e.CommandArgument;
       ((Button)sender).Visible = false;
    }
}
相关问题