简体   繁体   中英

Unable to access form variables in ASP.NET on submitting form

I am unable to access request form variable. Given below is the snippet of my aspx code.

<form id="mainmasterform" runat="server">
    <table align="center">
    <tr>
        <td><label for="ownfname">First Name</label></td>
        <td><asp:TextBox id="ownfname" runat="server" TextMode="SingleLine"/></td>
    </tr>
    <tr>
        <td><label for="ownlname">Last Name</label></td>
        <td><asp:TextBox id="ownlname" runat="server" TextMode="SingleLine"/></td>
    </tr>
    <tr>
        <td colspan="2">
            <asp:Button ID="submitowner" runat="server" Text="Submit" onclick="modifyDetails" />
        </td>
    </tr>
</form>

Now onclick of button calls a server method "modifyDetails" which tries to access form variables using Request.Form object.

protected void modifyDetails(object sender, EventArgs e) {
    string fname = Request.Form["ownfname"];
    string lname = Request.Form["ownlname"];
}

It does not work, the strings fname and lname are always null. Please help as I am not able to understand what is wrong over here.

Why not just do the following?

protected void modifyDetails(object sender, EventArgs e) {
    string fname = ownfname.Text;
    string lname = ownlname.Text;
}

That's how ASP.NET is designed to work.

Either don't use the <asp: controls (instead use regular HTML Controls), or set their ClientIDMode property to static. Documentation here

try this

string fname = ownfname.Text
string lname = ownlname.Text

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM