views:

21

answers:

3

I have the following in an aspx page:

<td colspan="2">
    <% DisplayParties(); %>
</td>

In the code behind for the aspx page, i have this (e.g. I build HTML for the checkboxes):

public void DisplayParties() {
    var s = new StringBuilder();
    s.Append("<input type=\"checkbox\" id=\"attorney\" value=\"12345\"/>");
    s.Append("<input type=\"checkbox\" id=\"attorney\" value=\"67890\"/>");
    s.Append("<input type=\"checkbox\" id=\"adjuster\" value=\"125\"/>");
    Response.WriteLine(s.ToString());
}

Not my proudest moment, but whatever. The problem is that when this page posts back via some event on the page, I never get these tags in the Request.Form collection.

Is this simply how ASP.NET works (e.g. only server-side control post back) or am I missing something simple.

My understanding was that a postback should bring back all the form variables.

A: 

I'm not exactly sure, but I think you are missing the form tag.

<form method="post" action="PageName.aspx"> 

   //your code

</form>
jWoose
There is a form tag, since everything else posts back (and I do see it as well in the "View Source" window in the browser.
AngryHacker
+1  A: 

You need to use the name attribute instead of ID (and don't duplicate attorney as a name):

public void DisplayParties() {
    var s = new StringBuilder();
    s.Append("<input type=\"checkbox\" name=\"attorney\" value=\"12345\"/>");
    s.Append("<input type=\"checkbox\" name=\"other_attorney\" value=\"67890\"/>");
    s.Append("<input type=\"checkbox\" name=\"adjuster\" value=\"125\"/>");
    Response.WriteLine(s.ToString());
}

Note that this will send the appropriate name/value only when the corresponding checkbox is checked. I.e., if the attorney and the adjuster checkboxes are checked, somewhere in the POST you will have: ...attorney=12345&adjuster=125...

Gonzalo
+1  A: 

That's not the way you should do things in ASP.NET, there are server controls that are meant to make our lives easier as programmers and to also seperate the code from the UI. But at any rate, to get it work the way you have done it, Request.Form is going to pull things by name. Add name fields to your controls.

Anthony Pegram
I could not upvote you more, since I agree 100%. Unfortunately the pattern has been layed down by someone, I am just following it.
AngryHacker
Ah. Very well, then.
Anthony Pegram