0

I created several input text in Javascript in my .aspx

      for (var i = 0; i < listbox.options.length; i++) {
            var text = listbox.options[i].value;
            var element = document.createElement("input");


            //Assign different attributes to the element.
            element.setAttribute("type", "text");
            element.setAttribute("value", text);
            element.setAttribute("id", "TMruleItem");

            element.setAttribute("style", "width:480px; margin-top:10px");
            element.setAttribute("disabled", "disabled");
            element.setAttribute("runat","server");
            var foo = document.getElementById("Panel_TM");
            foo.appendChild(element);
      }       

However, when I try to get the text of this object in C#(code behind the .aspx), it seems impossible. Can anybody help with this? Thanks a lot!

4 Answers 4

1

The js runs client side AFTER the C# (server side) has rendered the page. You cannot do what you want because you have fundamentally flawed model of how these technologies interact. Having said that; you could implement a web service or ajax framework to pass the text value to the server.

Sign up to request clarification or add additional context in comments.

Comments

1

I'm not exactly sure how you will add this into the C# side of things (which live on the server), because this element is only being added to the clientside (even if you add an attribute runat="server"), because the page that was generated has been sent.

However, if you need to be able to add elements dynamically, you could always pass some information using an ajax call (etc) that is stored somewhere (a database) that is then read to re-create the html element next time the page is generated...

End result though is that for the C# to be able to access the element, it must exist at some point on the server. If instead of accessing the element, you just want to access the value, provided the form is posted (ajax or regular) the value will be part of the Request variables:

string value = Request.Params["TMruleItem"];

Comments

0

Add the following to your javascript code:

element.setAttribute("name", "TMruleItem");

and entered values will be available on the server side:

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

Comments

0

I think you'd need to add the element to the form and when you post back to the server you can pull the value out using Request.Form

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.