开发者

ASP.NET GridView Data which i set on RowDatabound event loses after post back

I have an asp.net page where i have a gridview control which bind data from a DataTable. In my 5 th column of the grid i am showing a Radio button list which has 2 Radio button items (Yes or No). For Some rows i will not show the RadioButton control, if the 4 th column cell value is empty. It works fine.But in my button click event (postback), the Grid is showing the Radio button list for those cells which was not shown initially. I have enabled ViewState for page and Control

This is my code

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns=false 
                             DataKeyNames="RequestDetailId" ondatabound="GridView1_DataBound"  EnableViewState ="true" AllowPaging=false
                             onrowdatabound="GridView1_RowDataBound">
       <Columns>
           <asp:BoundField DataField="RequestDetailId" HeaderText="Request Detail Id" />
           <asp:BoundField DataField="Item" HeaderText="Item" />
           <asp:BoundField DataField="Status" HeaderText="Status" />
           <asp:BoundField DataField="NewOffer" HeaderText="New Offer" />
           <asp:TemplateField HeaderText="Your Response" ItemStyle-CssClass="radioTD">
               <ItemTemplate>
                   <asp:RadioButtonList ID="radioList" runat="server">
                        <asp:ListItem Text="Yes" Value="Accept"></asp:ListItem>
                        <asp:ListItem Text="No" Value="Reject"></asp:ListItem>
                    </asp:RadioButtonList>

                 </ItemTemplate>
            </asp:TemplateField>
       </Columns>                    
 </asp:GridView>

in code behind

 protected void Page_Load(object sender, EventArgs e)
 {
        if (!IsPostBack)
        {
           LoadItemDetails();
        }
  }开发者_开发百科

 private void LoadItemDetails()
 {
     DataTable objDt=  GetGridDataSource();
       if (objDt.Rows.Count > 0)
       {                     
                  GridView1.DataSource = objDt;
                  GridView1.DataBind(); 
        }
 }
 protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
 {          
        if(String.IsNullOrEmpty (e.Row.Cells[3].Text.Trim())||(e.Row.Cells [3].Text =="&nbsp;"))
        {
            e.Row.Cells[4].Text = "";   
        }
  }

My results are

Before Postback

ASP.NET GridView Data which i set on RowDatabound event loses after post back

After Postback

ASP.NET GridView Data which i set on RowDatabound event loses after post back

How do i maintain the content after postback ? Thanks


The problem is that you are setting the Text of the GridView table cell to an empty string rather than setting the visibility of the RadioButtonList control to false.

Clearing the Text property is removing the markup for the RadioButtonList on the first load, but on postback the RowDataBound event is not fired and the RadioButtonList control is recreated and displayed again.

To avoid this you could find the control and set its visibility to false, this will then be remembered across postbacks.

Try the following:

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (String.IsNullOrEmpty(e.Row.Cells[3].Text.Trim()) || (e.Row.Cells[3].Text == "&nbsp;"))
    {
        RadioButtonList radioList = (RadioButtonList)e.Row.FindControl("radioList");
        radioList.Visible = false;
    }
}

Hope this helps.


you can do something like this ..

from the description of the problem. it sounds as if you are doing the databinding in the code behind. in such case asp.net does not preserve the datasource in the viewstate for you. try retrieving the data and storing it in the ViewState hashtable object with something like

ViewState["GridviewData"] = GridviewData

and retreiving it from there between postbacks

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜