2014-09-04 37 views
1

我已經在網格視圖中填充數據。此外,我還使用行數據綁定中的代碼編寫了將圖像URL更改爲圖像的代碼。如何在回發時保留gridview數據?

下面是代碼:

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

是什麼在你的網頁加載列?如果(!IsPostback){...}在你的頁面加載時添加 – Sam 2014-09-04 11:25:18

+0

我已經在按鈕中寫入數據綁定點擊不在頁面加載..... – Vanarajan 2014-09-04 11:28:04

+2

請將整個代碼aspx和代碼放在後面。 – Priyank 2014-09-04 11:29:26

回答

3

當您添加此問題發生動態控制,並且在頁面初始化事件期間該控件不存在。回發後要將視圖狀態恢復到控件,控件必須存在於控件樹中。

你可以嘗試改變你的代碼有點像這樣。 CONVER其顯示圖像和鏈接到模板字段

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

太好了...非常感謝...我懂了... – Vanarajan 2014-09-04 12:20:12