views:

315

answers:

1

I dynamically add rows to divStaff using jquery:

$("span[id$='lblAddStaff']").click(function() {
        //$(".staff_tpl").find("input[id$='txtRate']").val("0,00");
        var staff_row = $(".staff_tpl");
        staff_row.find(".staff_row").attr("id", "Emp" + counter);
        $("div[id$='divStaff']").append(staff_row.html());
        counter += 1;            
    });

the row that I'm adding is inside the hidden div with class=".staff_tpl" I append the contents of this div to divStaff

When I submit the page (postback), the resulting divStaff is always empty if I try to display it like this:

lblTest.Text = divStaff.innerHtml.ToString

basically, I'm manipulating a div client side, and I want to access it server side via the code-behind of my aspx page. I think I'm missing a basic principle here.

A: 

This cannot be done.
If you want to access data you've created pn the page, you have to place it inside input fields (possibly hidden), and access it after it was posted using Request.Form["MyHiddenFieldName"].
<div>s aren't posted to the server. runat="server" elements are enechoded in the ViewState (a big string, really - you can see it in the source of your page), giving the abstraction of continuity (or the illusion of it). However, that sting isn't aware of changes you make in the DOM.
When dealing with runat="server" elements, you will see the last changes you've made on the server side, but client side changes are gone.
Only <input> (and text area, option, etc) values are posted to the server on submit, so changing these on the client will be seen on the server, after the page was posted.

Kobi
I was under the impression that adding runat="server" and a unique ID to a div allows you to access it server side? Is this not the case?
Stijn Van Loo
You can access a div with runat="server" at server side.
rahul
@phoenix - of course, but not to changes you've made with jQuery. Am I wrong somewhere?
Kobi
Thanks Kobi, your edited post clarifies a lot. It makes sense that any changes to the DOM client-side are not captured server side. I will try to use an input element.
Stijn Van Loo