簡體   English   中英

從后面的代碼讀取動態創建的隱藏字段

[英]Reading dynamically created hidden field from Code behind

我將隱藏文件添加到表單元格中的代碼中,像這樣

              HtmlTableCell tCellJson= new HtmlTableCell();
              HiddenField hdnJson = new HiddenField();
              hdnJson.ID = "hdnJson"+ count;

              tCellJson.Controls.Add(hdnJson);
              tRow.Cells.Add(tCellJson);

現在,當我嘗試從背后的代碼獲取它時,我沒有從背后的代碼獲取該隱藏字段控件,我正在這樣做

int count=0
  string controlname = "hdnJson" + ++Count;
  HiddenField hdnJson =(HiddenField)tbleFileList.FindControl(controlname);

我的問題是如何獲取從后面的代碼添加的隱藏字段的價值?

我已經看到了頁面源,其中添加了隱藏字段,並且該隱藏字段的ID為hdnJson1

執行在PreInit事件內動態添加HiddenField的代碼,您應該一切順利。

查看ASP.NET頁面生命周期中的MSDN文章。 特別是PreInit事件部分:

將此事件用於以下用途:

...

  • 創建或重新創建動態控件。
protected void Page_PreInit(object sender, EventArgs e)
{
    // whatever other code you have up here

    HtmlTableCell tCellJson= new HtmlTableCell();
    HiddenField hdnJson = new HiddenField();
    hdnJson.ID = "hdnJson"+ count;

    tCellJson.Controls.Add(hdnJson);
    tRow.Cells.Add(tCellJson);
}

暫無
暫無

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

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