Gridview数据绑定事件 [英] Gridview databound event

查看:125
本文介绍了Gridview数据绑定事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在gridview中添加搜索标题,但是当我渲染grdiview数据绑定方法时,我在客户端获得了空白页输出。



我不清楚我可能会出错的地方。我还调试了我的数据绑定gridview事件,并且rows参数显示为null。但是,我测试了我的'bindgrid'方法,这确实有效(输出用户表)。



I am trying to add a search header within my gridview, however when I render the grdiview databound method, I get a blank page output on the client-side.

I am little unclear where I may be going wrong. I have also debugged the my databound gridview event and the rows parameter is shown to be null. However, I have tested my 'bindgrid' method and this does work (outputs user table).

private void BindGrid()
       {
           string strConnString = ConfigurationManager.ConnectionStrings["cdwConnectionString"].ConnectionString;
           using (SqlConnection con = new SqlConnection(strConnString))
           {
               using (SqlCommand cmd = new SqlCommand())
               {
                   cmd.CommandText = "select top 20 u.[uID], u.[uForenames], u.[uSurname], u.[uCompany], u.[uEmailAddress], s.[sStartDate] from [dbo].[UserDetails]";
                   cmd.Connection = con;
                   con.Open();
                   GridView3.DataSource = cmd.ExecuteReader();
                   GridView3.DataBind();
                   con.Close();
               }
           }
       }


       protected void OnDataBound(object sender, EventArgs e)
       {
           GridViewRow row = new GridViewRow(0, 0, DataControlRowType.Header, DataControlRowState.Normal);
           if (row == null) { return; }

           else
           {
               if (GridView3.Rows.Count > 0) // Add this condition
               {
                   for (int i = 0; i < GridView3.Columns.Count; i++)
                   {
                       TableHeaderCell cell = new TableHeaderCell();
                       TextBox txtSearch = new TextBox();
                       txtSearch.Attributes["placeholder"] = GridView3.Columns[i].HeaderText;
                       txtSearch.CssClass = "search_textbox";
                       cell.Controls.Add(txtSearch);
                       row.Controls.Add(cell);
                   }
                   GridView3.HeaderRow.Parent.Controls.AddAt(1, row);

               }

           }
       }




<asp:gridView ID="GridView3" HeaderStyle-BackColor="#3AC0F2" HeaderStyle-ForeColor="White"
   runat="server" AutoGenerateColumns="false" OnDataBound="OnDataBound">

   </asp:gridView>





非常感谢任何进一步的建议。非常感谢。



Any further advice would be very much appreciated. Many thanks.

推荐答案

RowCreated事件 [ ^ ]

Add the header row on RowCreated event[^]
void GridView3_RowCreated(Object sender, GridViewRowEventArgs e)
  {

    if(e.Row.RowType == DataControlRowType.Header)
      {
         // add header row here

      }
}





几个参考:

以编程方式创建ASP.NET中使用C#的GridView标题行 - 请参见:http://www.dotnetfox.com/articles/programmatically-creating-gridview-header-row-in-Asp-Net-using-C-Sharp-1059。 aspx #sthash.PkOqkaoG.dpuf [ ^ ]

使用Custom GridView中的按钮创建标题行 [ ^ ]

如何在ASP.NET中使用GridView的RowCreated事件 [ ^ ]



few references:
Programmatically creating GridView header row in ASP.NET using C# - See more at: http://www.dotnetfox.com/articles/programmatically-creating-gridview-header-row-in-Asp-Net-using-C-Sharp-1059.aspx#sthash.PkOqkaoG.dpuf[^]
Creating a header row with buttons in a Custom GridView[^]
How to use RowCreated event of GridView in ASP.NET[^]


这篇关于Gridview数据绑定事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆