如何在回发时保留gridview数据?

时间:2014-09-04 11:11:02

标签: c# asp.net gridview

我在网格视图中填充数据。此外,我还在Row Data Bound中编写了代码,用于将图像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而不是Image。

你能协助我处理吗?

提前致谢。

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

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);
        }
     }        
 }

1 个答案:

答案 0 :(得分:3)

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

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

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