0

I have an input type-text that is created dynamically via asp.net after a div is being clicked on:

divID.innerHtml = "<input type='text' runat='server' id='box' value='' />";

what i am trying to do is when the div is clicked again, to get the value of the textbox in asp.net.

the problem is - the textbox does not exist so the compiler gives me an error for trying to use its name.

Is there a way to do this?

i also thought that maybe there is a way to somehow use jquery to pass the value to asp.net.

Noam Gal
  • 1,114
  • 2
  • 11
  • 20

2 Answers2

0

Rather than create your textbox as HTML, create the control and add it to your page.

I.E

  ''IN VB
  Dim tb as New TextBox() with {.ID = "box", .ClientIDMode =  UI.ClientIDMode.Static }

  // In C#
  TextBox tb = new TextBox {
    ID = "box",
     ClientIDMode = UI.ClientIDMode.Static
  };

Using ClientIDMode=Static will ensure the ID you set in the code-behind will be the one rendered to the HTML, rather than .net changing it.

Then add this to your divID

   divID.Controls.Add(tb)

You will now have access to it on postback by. In your postback method you will need to FindControl to get the control - this is because its not a known control on your page. I am using FindControl on the divID, this will look inside that div for the textbox we created earlier.

 ''VB
 Dim tb as TextBox = CType(divID.FindControl("box"), TextBox)

 // C#
TextBox tb = (TextBox)divID.FindControl("box");

You will be also able to access this TextBox's value via JQuery by calling $("#box").val() should you need.

Darren Wainwright
  • 30,247
  • 21
  • 76
  • 127
  • do you mean when you post back? or by jquery>? – Darren Wainwright Dec 14 '12 at 21:33
  • I did actually put the answer there for both postback AND jquery - perhaps re-read the answer... :) – Darren Wainwright Dec 14 '12 at 21:35
  • basically when the div bellow is pressed, but i have it set up so it will just refresh the page using jquery (and therefor calling Page_Load()) - how can i get the value in the Page_Load function? like when i have a div with a runat=server i write `string st=divId.innerText` – Noam Gal Dec 14 '12 at 21:37
  • after `TextBox tb = (TextBox)divID.FindControl("box");` can i just go `tb.value` (or something like that)? – Noam Gal Dec 14 '12 at 21:39
  • Ok, I'm sorry. I am VERY lost as to what you are trying to achieve. I can only suggest you try to re-form your question so it makes more sense. You would cause a postback by clicking a button (for example) - it is in the method of this button click where you get the value - not the page load. – Darren Wainwright Dec 14 '12 at 21:49
0

I would suggest using a more ASP-centric approach, such as that posted by Darren.

However, all form inputs are submitted for processing during a PostBack (full or partial).

Thus, if a text input control with a name box1 is added to the HTML form - dynamically or from a previous request - then the submitted form value can be accessed via Request.Form in the corresponding PostBack:

string boxValue = Request.Form["box"];

Note that this only gets a value submitted with a form, should it exist; it does not create any magical code-behind object or otherwise affect the Control Tree. The Control Tree might be altered as a side-effect of receiving new values such as adding new controls to the page this PostBack ..


1 HTML form inputs should have a name attribute; the id attribute is not used for HTML form submission. Also, name attributes can be duplicated which result in "control arrays". To access these, which are submitted as multiple values, use Request.Form.GetValues(name).