passing values in asp.net with 2 forms -
i have 2 webpages default.aspx , default2.aspx,
once user enters data in default.aspx ,
default.aspx redirects user default2.aspx request.querystring,
for reason im getting
txtuserid.text not exists in current context default.aspx.cs
default.aspx:
<html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title>querystring example in asp.net</title> </head> <body> <form id="form1" runat="server"> <div><b>querystring example</b></div><br /> <div> <table> <tr> <td><b>userid:</b></td> <td><asp:textbox id="txtuserid" runat="server"/></td> </tr> <tr> <td><b>username:</b></td> <td><asp:textbox id="txtusername" runat="server"/></td> </tr> <tr> <td></td> <td><asp:button id="btnsend" text="send values" runat="server" onclick="btnsend_click"/></td> </tr> </table> </div> </form> </body> </html> default.aspx.cs:
using system; using system.collections.generic; using system.linq; using system.web; using system.web.ui; using system.web.ui.webcontrols; namespace webapplication1 { public partial class webform3 : system.web.ui.page { protected void page_load(object sender, eventargs e) { } protected void btnsend_click(object sender, eventargs e) { response.redirect("default2.aspx?userid=" + txtuserid.text + "&username=" + txtusername.text); } } } default2.aspx:
<html xmlns="http://www.w3.org/1999/xhtml"> <head id="head1" runat="server"> <title>querystring example in asp.net</title> </head> <body> <form id="form1" runat="server"> <div><b>querystring parameter values in default2.aspx page</b></div><br /> <div><b>userid:</b><asp:label id="lbluserid" runat="server"/></div><br /> <div><b>username:</b><asp:label id="lblusername" runat="server"/></div> </form> </body> </html> default2.aspx.cs:
using system; using system.collections.generic; using system.linq; using system.web; using system.web.ui; using system.web.ui.webcontrols; namespace webapplication1 { public partial class webform4 : system.web.ui.page { protected void page_load(object sender, eventargs e) { if (!ispostback) { lbluserid.text = request.querystring["userid"]; lblusername.text = request.querystring["username"]; } } } }
you need make sure .aspx file code behind match page this:
<%@ page language="c#" codebehind="webform2.aspx.cs" inherits="yournamespace.webform2" .... %> and code behind class should match codebehind property:
public partial class webform2 : system.web.ui.page { ... in above example codebehind="webform2.aspx.cs" pointing class webform2
Comments
Post a Comment