【问题标题】:Not to clear textbox on page reload or button click event不清除页面重新加载或按钮单击事件上的文本框
【发布时间】:2017-06-27 05:22:07
【问题描述】:

我在C# asp.net 中开发了一个网站,其中包含Master Page。 我设计了一个带有搜索栏(txtSearch)和ButtonLinkButton)的顶部菜单。 当我单击search Button 时,它会重定向到要显示搜索数据的页面,但是当它重定向时,它会明确显示txtSearch。 我想要的不是清除页面重定向或页面重新加载上的txtSearch

顶部菜单(搜索栏)

<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>

我希望问题很清楚。

【问题讨论】:

    标签: c# asp.net textbox


    【解决方案1】:

    你可以做的是..将TextBox的值存储在onclick()函数中的Session中

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
       Session["TextBoxVal"] = TextBox1.Text;//The particular TextBox that has value
    }
    

    并且 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"];
            }
        }
    

    一旦它被赋值给TexBox 清除会话,否则它将继续分配相同的值.. 这样做

    Session["TextBoxVal"] = null;
    

    【讨论】:

    • 如何将该值从 Search.aspx 页面添加回 txtSearch(母版页)? @Frost_Mourne
    • Same TextBox1.Text = (string)ViewState["TextBoxVal"]; session 可以在解决方案的任何页面中使用。只需提供正确的 TextBox Id,您就可以使用它
    • 我知道 Session 值可以在任何页面中检索,但如何检索文本框(母版页的 txtSearch)??
    • (string)ViewState["TextBoxVal"];这只是给你你之前保存的字符串你可以把它给所有可以保存数据的东西你甚至可以给它 label.Text= (string)ViewState["TextBoxVal"];或任何其他元素
    • 感谢@Frost_Mourne 的帮助,我这样使用它,它工作得很好。 if(Request.QueryString["keyword"] != null && Request.QueryString["keyword"] != "") { txtSearch.Text = Request.QueryString["keyword"].ToString(); }
    【解决方案2】:

    您必须将数据传递到另一个页面。要传递数据,您可以使用各种技术

    • 查询字符串
    • 会话
    • Cookie

    使用上述一种方法传递数据。

    我会建议查询字符串是最好的选择

    【讨论】:

    • link 用于传递数据
    • 感谢您的帮助。它工作 if(Request.QueryString["keyword"] != null && Request.QueryString["keyword"] != "") { txtSearch.Text = Request.QueryString["keyword"].ToString(); }
    【解决方案3】:

    您必须在页面之间的查询字符串中传递值才能持久化它。

    【讨论】:

    • 请为您的答案添加一些解释。或者可以将其作为评论发布。
    • 我在查询字符串中传递了 txtSearch 的值,但是如何从 Search.aspx 页面将该查询字符串的值添加回 txtSearch(母版页)? @Vijay Raheja
    • link 用于传递数据
    • 感谢您的帮助 if(Request.QueryString["keyword"] != null && Request.QueryString["keyword"] != "") { txtSearch.Text = Request.QueryString [“关键字”].ToString(); }
    • 如果您的代码仍未运行,请发布您的代码以便我查看。
    【解决方案4】:

    把它放在一个会话中:

    Session["Sample"] =null;
    

    【讨论】:

    • 虽然这可能有效,但最好添加详细信息来解释您的答案。您可以在这里找到更多提示:How to Answer
    • @ItamarGreen 是对的,你似乎在复制我的答案。因为我已经详细给出了相同的解决方案
    • @Frost_Mourne 我认为你是我看到的第一个拥有 101 个代表不是来自 assoc 的人。奖金XD
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-13
    • 1970-01-01
    • 2018-01-07
    • 2013-10-13
    • 1970-01-01
    相关资源
    最近更新 更多