开发者

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

开发者 https://www.devze.com 2023-03-02 01:30 出处:网络
I am unable to access request form variable. Given below is the snippet of my aspx code. <form id=\"mainmasterform\" runat=\"server\">

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 = R开发者_开发技巧equest.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
0

精彩评论

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