我在 RowDatabound 事件上设置的 ASP.NET GridView 数据在回发后丢失

发布于 2024-12-10 03:44:27 字数 2379 浏览 0 评论 0原文

我有一个 asp.net 页面,其中有一个 gridview 控件,它绑定来自 DataTable 的数据。 在网格的第五列中,我显示了一个单选按钮列表,其中有 2 个单选按钮项目(是或否)。对于某些行,如果第四列单元格值为空,我将不会显示 RadioButton 控件。它工作正常。但是在我的按钮单击事件(回发)中,网格显示最初未显示的那些单元格的单选按钮列表。我已经为页面和控件启用了 ViewState

这是我

<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>

在代码后面的

 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 = "";   
        }
  }

代码我的结果是

回发前

enter此处图像描述

回发后

在此处输入图像描述

回发后如何维护内容?谢谢

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 ==" "))
        {
            e.Row.Cells[4].Text = "";   
        }
  }

My results are

Before Postback

enter image description here

After Postback

enter image description here

How do i maintain the content after postback ? Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

樱桃奶球 2024-12-17 03:44:27

问题在于您将 GridView 表格单元格的文本设置为空字符串,而不是将 RadioButtonList 控件的可见性设置为 false。

清除 Text 属性会在第一次加载时删除 RadioButtonList 的标记,但在回发时不会触发 RowDataBound 事件,并且会重新创建并再次显示 RadioButtonList 控件。

为了避免这种情况,您可以找到该控件并将其可见性设置为 false,这将在回发过程中被记住。

尝试以下操作:

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

希望这会有所帮助。

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 == " "))
    {
        RadioButtonList radioList = (RadioButtonList)e.Row.FindControl("radioList");
        radioList.Visible = false;
    }
}

Hope this helps.

酒几许 2024-12-17 03:44:27

做这样的事情

你可以从问题的描述中 。听起来好像您正在后面的代码中进行数据绑定。在这种情况下,asp.net 不会为您保留视图状态中的数据源。尝试检索数据并将其存储在 ViewState 哈希表对象中

ViewState["GridviewData"] = GridviewData

,并在回发之间从那里检索数据

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文