【问题标题】:ASP.NET GridView RowDataBound with CheckBox clickable带有可单击复选框的 ASP.NET GridView RowDataBound
【发布时间】:2012-10-23 16:07:00
【问题描述】:

我将此代码用于gridview:

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {   if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Attributes.Add("style", "font-weight:bold;color:blue");
            e.Row.Attributes.Add("style", "cursor:pointer;");
            e.Row.Attributes.Add("onclick", "location='WebForm1.aspx?id=" + DataBinder.Eval(e.Row.DataItem, "CustomerID") + "'";);
        }}

还可以使用 TemplateField 添加一列复选框。 我的问题是,当我单击行 goto WebForm1.aspx 的任何字段时,但我想在单击 CheckBox 时没有转到 WebForm1.aspx 页面并且只检查 CheckBox Control。

【问题讨论】:

    标签: gridview checkbox rowdatabound


    【解决方案1】:

    从您的代码中取出 onclick 事件。

     protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {   if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Attributes.Add("style", "font-weight:bold;color:blue");
                e.Row.Attributes.Add("style", "cursor:pointer;");
    
            }}
    

    【讨论】:

      【解决方案2】:

      我相信你需要修改最后一行使用FindControl方法查找复选框控件并传入复选框控件的id,如下所示:

       e.Row.FindControl("myCheckBoxName").Attributes.Add("onclick", "location='WebForm1.aspx?id=" + DataBinder.Eval(e.Row.DataItem, "CustomerID") + "'";);
      

      【讨论】:

      • 您能否详细说明代码的错误之处?当您测试它时,您是否收到错误或异常?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-10-09
      • 2011-08-04
      • 2018-07-19
      • 1970-01-01
      • 2014-04-06
      • 1970-01-01
      • 2015-04-01
      相关资源
      最近更新 更多