CyberFox
CyberFox

Reputation: 810

How do I add a reference to a generated TextBox in the control tree?

I have an input field that is appended to the HTML in a div. I need to have this input field bound to the ctrlmenuItems in the system but I don't know how.

I have this function which is the one calling the object:

protected void btnSave_Click(object sender, EventArgs e){
  int Id = int.Parse(Request.QueryString["edit"]);
  DA.page itmToEdit = DA.page.StaticGetById(Id);
  itmToEdit.title = ctrltitle.Text;
        itmToEdit.menuItems = ctrlmenuItems.Text;
  itmToEdit.content = ctrlcontent.Text;
  itmToEdit.Update();
  UpdateGUI();
  pnlEditArea.Visible = false;
}

I tried using:

protected global::System.Web.UI.WebControls.TextBox ctrlmenuItems;

which just creates a null reference.

I tried calling this onInit():

TextBox ctrlmenuItems = new TextBox();
ctrlmenuItems.ID = "ctrlmenuItems";
ctrlmenuItems.Text = "Enter your name";
this.form1.Controls.Add(ctrlmenuItems);

with no luck.

The appending is done like this:

litMenu.Text += string.Format(@"<div>
      <a class='button' href='?del={1}'>Slet</a>
      <input name='ctrlmenuItems' type='text' id='ctrlmenuItems' value'{0}' />
    </div>",
  itm.menuItems,
  itm.id);

I need to be able to click "save" and it passes on the value from the textField to dataAccess but I can't figure out how to bind this "virtual" textbox correctly.

Upvotes: 0

Views: 156

Answers (1)

wsanville
wsanville

Reputation: 37516

input tags with a name attribute will be stored in the Request.Form array when the page posts back.

Whether you add the TextBox control manually, or simply insert the corresponding HTML (which I would not recommend, building HTML via string concatenation makes it very easy to yield invalid HTML), you can retrieve the value when your button is clicked using the following:

string value = Request.Form["ctrlmenuItems"];

Upvotes: 1

Related Questions