Search code examples
asp.netpostwebformsrequest-object

How to programmatically distinguish element/control types from ASP.NET Request.Form key/value collection?


I have a simple ASP.NET web form as below:

<form id="form1" runat="server">
    <asp:TextBox ID="txt" runat="server"></asp:TextBox>
    <asp:DropDownList ID="ddl" runat="server">
        <asp:ListItem Text="X" Value="X"></asp:ListItem>
        <asp:ListItem Text="Y" Value="Y"></asp:ListItem>
        <asp:ListItem Text="Z" Value="Z"></asp:ListItem>
    </asp:DropDownList>
    <asp:Button ID="btn" runat="server" Text="Button" />
</form>

Request.Form contains the following key/value pairs:

[0] _VIEWSTATE
[1] _EVENTVALIDATION
[2] txt
[3] ddl
[4] btn

How do I differentiate the button (btn) from Textbox value (txt) or DropDown List value (ddl)? Or do I need to somehow come up with a naming convention? I am trying to iterate Request.Form object and save form values into a hashtable for later use.

Thanks.


Solution

  • You can't. To the server it's a simple name:value collection.

    Why not let the framework take care of this for you?

    In the codebehind you can retrieve the values via their properties:

    ddl.SelectedText
    txt.Text