【问题标题】:About ASP.NET Gridview关于 ASP.NET 网格视图
【发布时间】:2010-12-03 18:21:56
【问题描述】:

您好,我需要帮助在 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>

,我无法获取选定的行索引。

我希望我已经清除了我想问的问题。提前致谢。

【问题讨论】:

    标签: c# asp.net gridview


    【解决方案1】:

    使用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;
        }
    }
    

    【讨论】:

      【解决方案2】:

      继续 1) 的 Canvars 解决方案:

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

      【讨论】:

        猜你喜欢
        • 2011-11-23
        • 2012-07-11
        • 2017-12-05
        • 1970-01-01
        • 1970-01-01
        • 2010-10-27
        • 2012-09-07
        • 2010-09-22
        • 1970-01-01
        相关资源
        最近更新 更多