我在做一个考试系统,在后台添加答案项的时候,动态生成了TextBox控件,但是在保存的时候获取不到值,
我是添加到PlaceHolder控件里面的,但是我用PlaceHolder1.findControl(文本框ID) 的时候 这个对象NULL
请教高手们在怎么做?

解决方案 »

  1.   

    动态添加textbox.
    <asp:Button ID="createbutton" runat="server" Text="批量创建"   
      onclick="createbutton_Click" />
    <asp:Table ID="HolderTable" runat="server"></asp:Table> protected override void OnLoad(EventArgs e)
      {
      CreateControl();
      }     protected void createbutton_Click(object sender, EventArgs e)
      {
      if (ViewState["CreateControl"] == null)
      {
      ViewState["CreateControl"] = true;
      CreateControl();
      }
      }     void CreateControl() {
      if (ViewState["CreateControl"]==null) return;   
      for (int x = 0; x < 20; x++)
      {
      TableRow row = new TableRow();
      for (int y = 0; y < 4; y++)
      {
      TableCell cell = new TableCell();   
      Button bt = new Button();
      bt.Text = string.Format(" x={0},y={1} ", x, y);
      cell.Controls.Add(bt);   
      row.Cells.Add(cell);
      }   
      HolderTable.Rows.Add(row);
      }
      }   
    findcontrol查找 
     
      

  2.   

    可能我没有表达清楚,不好意思
    我给新添加的TextBox上面赋了值,
    但我在PlaceHolder1.findControl()找那个值的时候,Text值没有保持住,回刷后没有了
    PlaceHolder不是后台添加的
      

  3.   

    Request.Form  怎么写,没有写过
    能写一个简单的案例嘛?
      

  4.   

    如下试了是可以取到它的Text值:
    protected void Page_Load(object sender, EventArgs e)
    {
    TextBox tb = new TextBox();
    tb.ID = "TxtName";
    PlaceHolder1.Controls.Add(tb);
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
    Response.Write((PlaceHolder1.FindControl("TxtName") as TextBox).Text);
    }
      

  5.   

    我用过Repeater中动态添加TextBox,也是要用FindControl方法,看来都是差不多这样的