c# - TextBox in master page cleared when redirect another page -
i have master page this:
public partial class site1 : system.web.ui.masterpage { public string mytext { { return textbox1.text; } set { textbox1.text = value; } } }
site1.master :
@ master language="c#" autoeventwireup="true" codebehind="site1.master.cs" inherits="project1.site1" %> <!doctype html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> <asp:contentplaceholder id="head" runat="server"> </asp:contentplaceholder> </head> <body> <form id="form1" runat="server"> <div> <asp:textbox id="textbox1" runat="server"></asp:textbox> <asp:contentplaceholder id="contentplaceholder1" runat="server"> </asp:contentplaceholder> </div> </form> </body> </html>
and default.aspx page this:
<%@ page title="" language="c#" masterpagefile="~/site1.master" autoeventwireup="true" codebehind="default.aspx.cs" inherits="project1.default" %> <%@ mastertype virtualpath="~/site1.master" %> <asp:content id="content1" contentplaceholderid="head" runat="server"> </asp:content> <asp:content id="content2" contentplaceholderid="contentplaceholder1" runat="server"> <asp:textbox id="testtextbox" runat="server"></asp:textbox> <asp:button id="button1" runat="server" text="ok" onclick="button1_click" /> </asp:content>
and access textbox1 control in default.aspx.cs page this:
protected void button1_click(object sender, eventargs e) { this.master.mytext = testtextbox.text; response.redirect("~/page2.aspx"); }
my problem in last line , when go page2.aspx mytext cleared! , don't show in page2.aspx. should do?
you have multiple possibilities:
1) append next url
add param, in example below, eg val
store text of textbox.
protected void button1_click(object sender, eventargs e) { this.master.mytext = testtextbox.text; response.redirect("~/page2.aspx?val=" + testtextbox.text); }
you can value in masterpage like:
private string _val { { return request["val"] != null ? request["val"].tostring() : ""; } }
you can append property textbox like:
this.mytextbox.text = _val;
2) store in session
create session object.
session["valtext"] = testtextbox.text;
get value of session in masterpage using property:
private string _val { { return session["valtext"] != null ? (string)session["valtext"] : ""; } }
you can append property textbox like:
this.mytextbox.text = _val;
Comments
Post a Comment