7

I have a simple form with some plain html input like bellow using ASP.NET Web Forms (not MVC)

<table id="tbl_SchoolDetails">
    <tbody id="tbody_SchoolDetails">
        <tr>
            <td>
            School Name
        </td>
        <td>
            <input id="SchoolDetails_SchoolName" type="text" value="<%= ViewModel.School.Name %>" />
        </td>
    </tr>
    <tr>
        <td>
            Head Teacher
        </td>
        <td>
            <input id="SchoolDetails_HeadTeacher_Name" type="text" value="<%= ViewModel.School.HeadTeacher.Name %>" />
        </td>
    </tr>
    <tr>
        <td>
            Head Teacher Email
        </td>
        <td>
            <input id="SchoolDetails_HeadTeacher_Email" type="text" value="<%= ViewModel.School.HeadTeacher.Email %>" />
        </td>
    </tr>
    <tr>
        <td>
            Regent/Placement Contact
        </td>
        <td>
            <input id="SchoolDetails_Regent_Name" type="text" value="<%= ViewModel.School.Regent.Name %>" />
        </td>
    </tr>
</tbody>
</table>

When I do a post back to the server the values of the text boxes are not contained in the Request.Form element. Is there some reason for this that I am missing. I am reluctant to use asp.net controls as the page is later going to require a fair amount of javascript for changing the ui and other stuff.

I know I could easily do this with MVC but unfortunatly a change to this is not an option at this time.

Cheers Colin G

2
  • This html is inside a <form> element, yes? Commented Jan 12, 2009 at 15:58
  • You do know that Request.Form is a collection of the name/value pairs the browser sent on the request. the only sends them when a form.submit() is done, either thru javascript (say a asp.net control postback), or submit button pressed? Commented Jan 12, 2009 at 16:06

4 Answers 4

8

Give the input both an id and a name and see if that doesn't solve your problem.

<input id="SchoolDetails_SchoolName"
       name="SchoolDetails_SchoolName"
       type="text"
       value="<%= ViewModel.School.Name %>" />

The id property only identifies the tag within the DOM. The name property allows the tag to be posted back as part of a form. An ASP.NET control, or an HTML element with runat=server, automatically gets both the name and id properties assigned.

Sign up to request clarification or add additional context in comments.

Comments

2

HTML elements are not part of the ASP.NET lifecycle. An easy way to start would be to add the runat="server" attribute to your INPUT elements.

2 Comments

It will solve it, but not because the problem with the ASP.NET lifecycle. The problem is that the inputs have no names and so never get submitted by the client with the form.
<input /> elements will make it into Request.Form regardless of runat="server". They just need to be named (ie name="..." as opposed to having only id="..." as posted).
0

@tvanfosson Cheers that worked just as I wanted.

@Bullins I really didn't want to use runat="server" as it causes the ID's of the controls to end up like ctl00_ContentPlaceHolder1_ctl00_SchoolDetails_Address_Postcode which makes it difficult to achieve what i am looking for.

Comments

0

@Colin Use ClientIDMode="Static" attribute and your server-side element name will remain the same on the client side even when you have runat="server" on the element. Hope this helps

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.