我在asp.net项目的主页中有一个搜索框,但我不知道两件事:
1)如何将结果传输到新页面(因为它在母版页中)
2)结果如何展示?
对于第一个问题,我尝试使用答案的DataTable创建一个会话,并在新页面(在母版页上构建)的页面加载函数上检查会话!=null,但它不能很好地工作,因为页面加载函数在主页面函数之前。
对于第二个问题,我尝试这样做:
users += "<div class='divusers'>";
users += "<h5>" + dt.Rows[0][0].ToString() + "</h5>";
users += "<h5>" + dt.Rows[0][1].ToString() + "</h5>";
users += "</div>";
但是,有没有更好的方式来显示结果呢?
发布于 2017-04-21 22:03:15
你不需要转移结果,你只需要发送搜索字符串到页面上,在主页上点击搜索按钮尝试一下
protected void SearchButton1_Click(object sender, EventArgs e)
{
Response.Redirect("~/SearchPage.aspx?searchTerm="+txtSearchter.Text);
}
在结果页面的页面加载上
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
string SearchTerm = Request.QueryString["searchTerm"];
//Do database search and bind to a gridview
}
}
发布于 2017-04-21 19:05:10
在母版页上,您可以放置一个控件来显示结果。
<div class="divusers">
<asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate>
<h5><%# Eval("columnName") %></h5>
</ItemTemplate>
</asp:Repeater>
</div>
然后,在使用Mater的页面的aspx上,使用FindControl定位母版页上的控件并将数据绑定到该控件。
protected void SearchButton1_Click(object sender, EventArgs e)
{
Repeater rep = this.Master.FindControl("Repeater1") as Repeater;
rep.DataSource = DataTable;
rep.DataBind();
}
发布于 2017-04-21 19:43:44
最好在对话框(弹出窗口)中显示结果,而不是将其转移到新页面
<GridView>
<Columns>
<asp:BoundField DataField="FirstName" HeaderText="First Name" ReadOnly="True" Visible="True" />
//other Boundfields
<asp:TemplateField HeaderText="Search">
<ItemTemplate>
<asp:Button Id="Searchbtn" runat="server" Text="Search" onClick="Searchbtn_Onclick"/>
</ItemTemplate>
</asp:TemplateField>
</columns>
</GridView>
代码隐藏
protected void Searchbtn_Onclick(object sender, EventArgs e)
{
//bind the gridview
ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "MyFun1", "javascript:ShowDialog(" + "'Result " + "');", true);
}
然后在脚本中
script type="text/javascript">
function ShowDialog() {
$("#dialog").dialog({
autoOpen: false,
hide: "puff",
show: "slide",
width: "70%",
modal: true,
close: function(event, ui) {
$("#dialog").find("form").remove();
$("#dialog").dialog('destroy');
}
});
$("#dialog").dialog("open");
}
https://stackoverflow.com/questions/43540542
复制相似问题