【问题标题】:asp gridview with checkbox and select带有复选框的asp gridview并选择
【发布时间】:2015-04-08 18:43:39
【问题描述】:

我正在尝试开发一个具有选择功能的 asp c# gridview,以便我可以显示记录的其他详细信息。我还需要一个复选框以允许用户检查该行以进行进一步处理。我可以单独实现每一个,但不能一起实现。甚至可能吗?

【问题讨论】:

    标签: c# asp.net


    【解决方案1】:

    当然可以。 对于复选框的列,使用TemplateField 列,https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.templatefield%28v=vs.110%29.aspx

    在数据网格中,aspx:

    <asp:TemplateField>
        <ItemTemplate>
            <asp:CheckBox ID="CheckBoxProcess" runat="server" />
        </ItemTemplate>
    </asp:TemplateField>
    

    在后面的代码中:

    protected void ButtonProcess_Click(object sender, EventArgs e)
    {
        foreach (GridViewRow item in GridView1.Rows)
        {
            CheckBox chk = (CheckBox)item.FindControl("CheckBoxProcess");
            if (chk != null)
            {
                if (chk.Checked)
                {
                    // This record should be processed
                }
            }
        }
    }
    

    GridView 内置了行选择功能,您可以通过将AutoGenerateSelectButton 属性设置为true 来启用它:

    <asp:GridView ... AutoGenerateSelectButton="true" />
    

    但是,当用户单击行时选择行(而不是单击链接)对用户更友好。 为此,您需要在行中附加一些 java 脚本:

    void GridView1_RowCreated(Object sender, GridViewRowEventArgs e)
    {
        if(e.Row.RowType == DataControlRowType.DataRow)
            e.Row.Attributes.Add("onclick", Page.ClientScript.GetPostBackEventReference(sender, "Select$" + e.Row.RowIndex.ToString())
    }
    

    这里还有更好解释的行点击选择解决方案:

    https://stackoverflow.com/a/6250846/461810

    【讨论】:

      【解决方案2】:

      您可以使用模板字段并将控件添加到其中。

      <Columns>                             
          <asp:TemplateField>
              <ItemTemplate> 
                  <select />
              </ItemTemplate>
          </asp:TemplateField>             
          <asp:TemplateField>
              <ItemTemplate> 
                  <checkbox />
              </ItemTemplate>
          </asp:TemplateField>                  
      </Columns> 
      

      【讨论】:

      • 对不起,我不清楚。我只想通过单击来选择行。然后单击以触发后面代码中的选择方法。以及有一个复选框,单击该复选框会在后面的代码中触发其自己的方法。
      • 对造成的误解深表歉意。我从来没有满足过这个要求,所以我对代码不够熟悉,无法给你一个很好的答案。但是,我确实找到了一个网页,我相信该网页涵盖了您在此处尝试完成的工作。希望对您有所帮助! Selecting GridView Row by clicking anywhere on the Row
      【解决方案3】:
      <asp:TemplateField>
          <ItemTemplate>
              <asp:CheckBox ID="SelectChk" OnCheckedChanged="chk_Click1" runat="server" AutoPostBack="true" />
          </ItemTemplate>
      </asp:TemplateField>
      

      在 CS 代码中:

      protected void chk_Click1(object sender, EventArgs e)
      {
          CheckBox MChk = sender as CheckBox;
          GridViewRow MyGridR = MChk.NamingContainer as GridViewRow;
          GridView1.SelectRow(MyGridR.RowIndex);
      }                    
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-08-04
        • 2022-01-03
        • 1970-01-01
        • 2018-10-19
        • 2010-11-23
        • 1970-01-01
        • 2014-06-29
        • 1970-01-01
        相关资源
        最近更新 更多