質問

I am adding a hidden filed to a table cell in code behind like this

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

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

and now when i am trying to get it from code behind i am not getting that hidden field control from code behind,i am doing this

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

My question is how to get value of hidden field that is added from Code behind?

I have seen the Page Source where hidden Field is added and Id of that hiddenfield is hdnJson1

役に立ちましたか?

解決

Execute the code that dynamically adds the HiddenField inside the PreInit event, and you should be good to go.

Check the MSDN article on the ASP.NET Page Life Cycle. Especially the PreInit event section:

Use this event for the following:

...

  • Create or re-create dynamic controls.
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帰属
所属していません StackOverflow
scroll top