簡體   English   中英

asp:gridview動態創建的列

[英]asp:gridview dynamically created columns

我有一個gridview借助於sqldatasource進行填充。

創建gridview后,我想添加一個手動創建的Notes列,以便用戶可以為每一行輸入注釋,然后單擊提交表單。

在網格視圖的<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;
}

因此,字符串notes始終為空,似乎在回發時會重置該值; 因為它會重新創建txtNotes文本框。

如何保持回發的價值?

綁定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
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM