使用C#从母版页获取文本框值 [英] Get Textbox Value from Masterpage using c#

查看:241
本文介绍了使用C#从母版页获取文本框值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在主页上有一个搜索文本框,如下所示:

I have a search textbox situated on a masterpage like so:

<asp:TextBox ID="frmSearch" runat="server" CssClass="searchbox"></asp:TextBox>
<asp:LinkButton ID="searchGo" PostBackUrl="search.aspx"  runat="server">GO</asp:LinkButton>

搜索页面后面的代码具有以下功能,可以提取文本框值(摘要):

The code behind for the search page has the following to pick up the textbox value (snippet):

if (PreviousPage != null && PreviousPage.IsCrossPagePostBack)
        {
            Page previousPage = PreviousPage;
            TextBox tbSearch = (TextBox)PreviousPage.Master.FindControl("frmSearch");
            searchValue.Text = tbSearch.Text;

            //more code here...
        }

一切正常.但是,如果您实际上在search.aspx上输入了一个值,则不会,但显然不是上一页.我该如何克服自己陷入的困境?

All works great. BUT not if you enter a value whilst actually on search.aspx, which obviously isn't a previous page. How can I get round this dead end I've put myself in?

推荐答案

如果使用

If you use the @MasterType in the page directive, then you will have a strongly-typed master page, meaning you can access exposed properties, controls, et cetera, without the need the do lookups:

<%@ MasterType VirtualPath="MasterSourceType.master" %>

searchValue.Text = PreviousPage.Master.frmSearch.Text;

为了稍微扩展您的想象力,请考虑母版页公开的一个非常简单的属性:

In order to help stretch your imagination a little, consider an extremely simple property exposed by the master page:

public string SearchQuery 
{
    get { return frmSearch.Text; }
    set { frmSearch.Text = value; }
}

那么,无论如何,我们都可以像这样访问它:

Then, through no stroke of ingenuity whatsoever, it can be seen that we can access it like so:

searchValue.Text = PreviousPage.Master.SearchQuery;

或者,

PreviousPage.Master.SearchQuery = "a query";

这篇关于使用C#从母版页获取文本框值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆