2014-03-19 38 views
0

我有一個gridview的sqldatasource幫助填充。asp:gridview動態創建列

一旦GridView控件創建我要添加手動列創建註釋,使用戶可以爲每個行輸入註釋,然後點擊提交表單。

在網格視圖的<Columns>領域我有這樣的:

<asp:TemplateField HeaderText="Notes"> 
        <ItemTemplate> 
         <asp:TextBox ID="txtNotes" runat="server"></asp:TextBox> 
        </ItemTemplate> 
</asp:TemplateField> 

這實際上創建了新列就好了,但是當我在文本框中輸入數據,就點擊提交時消失。 在提交即使我通過每個GridView的行遍歷像這樣:

foreach (GridViewRow row in gvResults.Rows) 
{ 
    row.Cells[0].Text = 
    ... 
    string notes = (row.Cells[10].FindControl("txtNotes") as TextBox).Text; 
} 

所以串音符總是空,似乎在回發的值復位;因爲它重新創建了txtNotes文本框。

我該如何保留回傳值?

+0

您在哪些頁面事件中遍歷行? – mxmissile

+0

你在回發上綁定網格嗎? – mxmissile

+0

迭代通過本次活動:'保護無效btnSubmit_Click(對象發件人,EventArgs的)' –

回答

3

當你綁定GridView時,確保你檢查它是否是回發。

protected void Page_Load(object sender, EventArgs e) 
{ 
    if(!IsPostBack) 
    { 
     LoadData(); //if you did this without checking IsPostBack, you'd rebind the GridView and lose the existing rows 
    } 

protected void LoadData() 
{ 
    //bind GridView etc 
} 
+0

遺憾的標籤應該是文本框,只是一個錯字,它是在代碼文本框,是我重新綁定頁面回傳,只注意到 –