views:

37

answers:

1

I'm just starting out with the whole ajax thing and I need some help. I have a form where the users will have the ability to add multiple e-mail addresses to a record. When the user clicks add a new e-mail address, I am going to perform an ajax call that will insert the email address and then get the current e-mails to be displayed for editing. Sounds fine, but the only problem I have is when the form submits I want to be able to access those values server side, as well as store the sequence numbers from the db so the emails can be updated on the final submit. In a repeater, I'd store the sequence number in a hidden field and then loop through the items, but I can't do that when I'm not using a repeater. I'd like to avoid using the update panel because of the large network traffic. I suppose I could use request.forms collection, but how would I store the sequence numbers in such a way that the users couldn't see them? Am I totally off base?

A: 

Why couldn't you use hidden fields anyway? As long as they are inside the form element it does not matter where they are, does it?

Say, for example, that your emails fields have name attribute set like "email_1", "email_2", etc. You could set name for your hidden fields like "seq_email_1", "seq_email_2"...

And then you can iterate through Request.Form and read emails and sequence numbers:

foreach (string key in Request.Form.AllKeys) {
    if (key.StartsWith("email_")) {
        string email = Request.Form[key];
        int seq = int.Parse(Request.Form["seq_" + key]);

        // process email with seq number
    }
}
Damir Zekić
I'm going to put the seq1, seq2, etc. at the start rather than at the end of my field names so that the email records are only processed once and to avoid acceptions. The way you have it, your if would run both for an email and a key.
Chris Westbrook
Whoops, you're totally right, I've overlooked that. I'll edit the answer
Damir Zekić