如何在回发时保留网格视图数据

本文关键字:保留 网格 视图 数据 | 更新日期: 2023-09-27 18:20:41

我在网格视图中有填充数据。此外,我还在行数据绑定中编写了将图像URL更改为图像的代码。

这是代码:

protected void GridView1_RowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
 {
    for (int i = 1; i < e.Row.Cells.Count; i++)
    {
      string cellValue = e.Row.Cells[i].Text.Trim();
      if(cellValue.StartsWith("http:"))
      {
           System.Web.UI.WebControls.Image img = new System.Web.UI.WebControls.Image();
           img.ImageUrl = e.Row.Cells[i].Text.Trim();
           HyperLink hb = new HyperLink();
           hb.Controls.Add(img);
           e.Row.Cells[i].Controls.Add(hb);
     }
    }        
 }

它运行良好。该页面还有两个下拉控件。如果我选择了一个下拉控件,我会返回帖子。那时,我已经写的里面的图像被改为URL,而不是图像。

你能帮我处理吗?

提前谢谢。

这是我的问题代码(我无法发布整个代码)

aspx代码:

 <html>
 <body>
 <form id="form1" runat="server">
 <asp:DropDownList ID="DropDownList2" CssClass="borderradius" runat="server" Height="20px" Width="190px" AutoPostBack="True" OnSelectedIndexChanged="DropDownList2_SelectedIndexChanged" Font-Size="11px">
 </asp:DropDownList> 
 <asp:Button id="sidesbmt" runat="server" onclick="sidesbmt_click"/>                    
 <asp:GridView ID="GridView1" runat="server" Width="100%" CssClass="mGrid" PagerStyle-CssClass="pgr"
AlternatingRowStyle-CssClass="alt" OnRowDataBound="GridView1_RowDataBound" OnSelectedIndexChanged="GridView1_SelectedIndexChanged"
BorderStyle="None" GridLines="Both">
<AlternatingRowStyle CssClass="alt"></AlternatingRowStyle>
  </asp:GridView>
</body>
 </html>

背后的代码

 protected void Page_Load(object sender, EventArgs e)
 {
 }
 protected void sidesbmt_Click(object sender, EventArgs e)
{
  GridView1.DataSource = ds;
  GridView1.DataBind();
}
protected void GridView1_RowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
 {
     for (int i = 1; i < e.Row.Cells.Count; i++)
     {
        string cellValue = e.Row.Cells[i].Text.Trim();
        if(cellValue.StartsWith("http:"))
        {
            System.Web.UI.WebControls.Image img = new System.Web.UI.WebControls.Image();
            img.ImageUrl = e.Row.Cells[i].Text.Trim();
            HyperLink hb = new HyperLink();
            hb.Controls.Add(img);
            e.Row.Cells[i].Controls.Add(hb);
        }
     }        
 }

如何在回发时保留网格视图数据

动态添加控件时会出现此问题,而在Page Init事件期间此控件不存在。若要在回发后将视图状态恢复到控件,控件必须存在于控件树中。

您可以尝试像这样更改代码。将显示图像和url的列转换为模板字段

<asp:TemplateField>
    <ItemTemplate>
        <asp:Literal runat="server" ID="literalUrl" Text='<%#Eval("The fieldname")%>'></asp:Literal>
        <asp:Image runat="server" ID="imageUrl" Visible="false" />
    </ItemTemplate>
</asp:TemplateField>

然后在rowdatabound事件中切换图像和文字的视图

protected void GridView1_RowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
{
    for (int i = 1; i < e.Row.Cells.Count; i++)
    {
        string cellValue = e.Row.Cells[i].Text.Trim();
        Literal literal = e.Row.Cells[i].FindControl("literalUrl") as Literal;
        Image imageUrl = e.Row.Cells[i].FindControl("imageUrl") as Image;
        if (literal != null && imageUrl != null)
        {
            if (literal.Text.StartsWith("http:"))
            {
                imageUrl.ImageUrl = literal.Text.Trim();
                imageUrl.Viisible = true;
                literal.Visible = false;
            }
        }
    }
}