开发者

How can I get a custom made set of checkboxes return values in the postback?

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开发者_JS百科 post back) or am I missing something simple.

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


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...


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.


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

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

   //your code

</form>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜