asp.net获取url参数并将其放入文本框值

时间:2016-01-07 16:50:43

标签: asp.net

我有一个母版页,其中有一个带TextBox的表单,当我按回车键时,它会将我重定向到Results.aspx?srch=search_term,我想给TextBox这个搜索过的字词。问题是Results.aspx.cs没有看到主页ID的{​​{1}}。有没有其他方法可以达到这个目的?

这是母版页:

TextBox

这是<%@ Master Language="C#" AutoEventWireup="true" CodeBehind="MasterPage.Master.cs" Inherits="Groups.Site1" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> <link href="css/bootstrap.min.css" rel="stylesheet" /> <link href="css/site.css" rel="stylesheet" /> <script src="js/jq.js"></script> <script src="js/bootstrap.min.js"></script> <asp:ContentPlaceHolder ID="head" runat="server"> </asp:ContentPlaceHolder> </head> <body> <form id="form1" runat="server"> <header> <div class="container"> <div class="row"> <div class="col-sm-2"> <h2><a href="Default.aspx">Groups</a></h2> </div> <div class="col-sm-3"> <asp:Panel runat="server" DefaultButton="Button1"> <div class="form-group search input-group"> <span class="input-group-addon"><span class="glyphicon glyphicon-search"></span></span> <asp:TextBox ID="search" placeholder="Cauta grup..." class="form-control" runat="server"></asp:TextBox> <asp:Button ID="Button1" runat="server" style="display:none" OnClick="btnSearch_Click" /> </div> </asp:Panel> </div> <div class="col-sm-7 text-right"> <ul class="nav navbar-nav"> <% if (Session["uid"] == null) { %> <li><a href="Registration.aspx">Register</a></li> <li><a href="Login.aspx">Login</a></li> <% } else { %> <li class="dropdown"> <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false"><%= Session["uname"] %> <span class="caret"></span></a> <ul class="dropdown-menu"> <li><a href="addgroup.aspx">Add group</a></li> <li role="separator" class="divider"></li> <li><a href="Logout.aspx">Logout</a></li> </ul> </li> <%} %> </ul> </div> </div> </div> </header> <asp:ContentPlaceHolder ID="header" runat="server"> </asp:ContentPlaceHolder> <div class="container"> <asp:ContentPlaceHolder ID="subheader" runat="server"> </asp:ContentPlaceHolder> <asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server"> </asp:ContentPlaceHolder> </div> </form> <footer> <div class="container"> <span>Copyright &copy; 2015 Groups</span> </div> </footer> </body> </html> 后置代码,问题在于Results.aspxsearch.Text = Request.QueryString["srch"];中不存在search

Results.aspx

谢谢!

1 个答案:

答案 0 :(得分:2)

如果要引用主页提供的控件,可以使用Master.FindControl(“...”)进行控制,即:

var myControl = (TextBox)Master.FindControl("search");