[英]Not to clear textbox on page reload or button click event
I have developed a website in C# asp.net
which contains a Master Page
. 我在
C# asp.net
开发了一个包含Master Page
。 I have designed a top menu with Search bar( txtSearch
) and a Button
( LinkButton
). 我设计了一个带有搜索栏(
txtSearch
)和一个Button
( LinkButton
)的顶部菜单。 When I click on search Button
it redirects to the page where searched data is to be displayed but when it redirects it clear's the txtSearch
. 当我单击
search Button
它将重定向到要显示搜索数据的页面,但是当重定向时,将清除txtSearch
。 What I want is not to clear the txtSearch
on page redirect or page reload. 我想要的是不清除页面重定向或页面重新加载时的
txtSearch
。
Top Menu (Search bar)
顶部菜单(搜索栏)
<asp:TextBox runat="server" ID="txtSearch" TextMode="SingleLine" CssClass="form-control" Placeholder="Search term..." />
<span class="input-group-btn">
<asp:LinkButton runat="server" ID="btnSearch" CssClass="btn btn-default" type="button" OnClick="btnSearch_Click"><span class="glyphicon glyphicon-search"></span></asp:LinkButton>
</span>
I Hope that the question is clear. 我希望这个问题是明确的。
What you can do is.. Store the TextBox
value in Session in onclick()
Function 您可以做的是。将
TextBox
值存储在onclick()
函数中的Session中
protected void btnSubmit_Click(object sender, EventArgs e)
{
Session["TextBoxVal"] = TextBox1.Text;//The particular TextBox that has value
}
and onPageLoad just assign the value to the TextBox
value like this 和onPageLoad只是将值分配给
TextBox
值,就像这样
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
TextBox1.Text = (string)ViewState["TextBoxVal"];
}
//use this as per your needs just an example how to use
if(IsPostBack)
{
TextBox1.Text = (string)ViewState["TextBoxVal"];
}
}
and once it is assigned value to the TexBox
clear the session or it will keep assigning the same value.. do it like this 并将其分配给
TexBox
值后,清除会话,否则它将继续分配相同的值。
Session["TextBoxVal"] = null;
you have to pass data to another page.to pass data you can use various techniques 您必须将数据传递到另一个页面。要传递数据,您可以使用多种技术
pass data using above one method. 使用上述一种方法传递数据。
i will suggest Query string will be best option 我会建议查询字符串将是最佳选择
您必须在页面之间的querystring中传递值以使其持久化。
放在一个会话中:
Session["Sample"] =null;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.