Stijn Van Loo
Stijn Van Loo

Reputation: 451

jquery append + code-behind

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.

Upvotes: 2

Views: 1286

Answers (1)

Kobi
Kobi

Reputation: 138017

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.

Upvotes: 1

Related Questions