Adding elements on client side

S

simonZ

I have <div id="divTbl" runat="server" enableviewstate="true"> control on my
asp.net page.

Than I add some html elements into this div element with javascript on
client side:
<script language="javascript">

var divAdd=document.createElement("div");
divAdd.id="divTest";
divAdd.innerText="test";

var divTbl=document.getElementById("<%=divTbl.ClientID%>");
divTbl.appendChild(divAdd);

</script>

On client it works, I can see new elements in my div element.
But when I post back my page, the divTbl.InnerHtml is empty.
Why and how can I do this?

Regards,
Simon
 
M

Mark Rae [MVP]

On client it works, I can see new elements in my div element.

Yes, you would...
But when I post back my page, the divTbl.InnerHtml is empty.

That's right.

In order for a control to be part of the postback, it has to be sent down to
the client in the first place. Controls which are created client-side are,
therefore, not part of this so you can't see them when the page is posted
back...
and how can I do this?

If you're only interested in the dynamically created <div>'s innerHTML
property, copy it into a hidden field before you kick off the postback...
 
B

bruce barker

the browser only posts back name value pairs for the form elements of
the form doing the submit (browsers support more than 1 form per page
even though asp.net only supports 1). form elements are <input>,
<select> and <textarea>. they must be enabled and have a name attribute
to be posted back. (in the case of input type=checkbox or tye=radio they
must also be checked).

if you dynamically add a <input> with client code it will be posted
back, but asp.net has no code to add it to the server side page model.
you will have to write this code.

-- bruce (sqlwork.com)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,190
Members
46,736
Latest member
zacharyharris

Latest Threads

Top