Problem with ASP.NET 2.0 GridView hidden Columns are shown on Postback
Please Help, I have User Control that contain GridView where i am hiding its Columns on GridView RowCreated event :
private void gvGrid_RowCreated(object sender, GridViewRowEventArgs e)
{
foreach (TableCell objCell in e.Row.Cells)
{
if (objCell is DataControlFieldHeaderCell)
{
objCell.Visible = false;
}
}
}
Now every thing was working fine, but On ASPX Page Postback ( containing Griview UserControl), shows all hidden columns, where my PageLoad is as follows
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
开发者_如何学编程 {
ucGridView.PopulatePage();
}
}
Note : ASPX Page start working fine if i remove !IsPostBack Check..but i dont want this...Whats the Problem...!
You need to use the RowDataBound event handler instead of RowCreated.
Are you sure you need to make the columns invisible in the code-behind?
If you just need to store field values in the grid but not display them then Microsoft recommends using the DataKeyNames property of the GridView control.
Instead of using code-behind to hide certain columns, you can just remove the bound fields from the GridView and specify them in the DataKeyNames property:
<asp:GridView ID="GridView1" runat="server"
DataKeyNames="SalesOrderID,SalesOrderDetailID"
DataSourceID="LinqDataSource1">
This way the fields will not show to the user but the GridView knows to keep the values around for updating, etc.
By Default your column visible in the grid and you are hiding when page is not postback
if (!IsPostBack)
{
ucGridView.PopulatePage();
}
so
technically ucGridView.PopulatePage(); is only beign called once thats why when you remove
!IsPostBack is works fine.
if used in RowDataBound then u cant access griview Column value
精彩评论