0

大家好,我在将变量从 Site.Master 转移到另一个窗口时遇到了一点问题,目的是使搜索功能可以从整个 asp.net Web 应用程序中访问...

与 Site.Master 相关的代码:

<div class="search">
        <asp:Label ID="LandID" runat="server" Text="LandID"/>
        &nbsp;<asp:TextBox ID="txtSearch" runat="server" />
        &nbsp;<asp:Label ID="CarBrand" runat="server" Text="CarBrand" />
        &nbsp;<asp:TextBox ID="txtSearch2" runat="server" />
        &nbsp;<asp:Label ID="Model" runat="server" Text="Model"/>
        &nbsp;<asp:TextBox ID="txtSearch3" runat="server" />
        &nbsp;<asp:Button ID="cmdSearch" runat="server" Text="Search" Style="width: 100px" OnClick="cmdSearch_Click" />
</div>

Site.Master.Cs 的代码:

public static string Text { get; set; }
public static string Text2 { get; set; }
public static string Text3 { get; set; }
protected void Page_Load(object sender, EventArgs e)
{

}
protected void cmdSearch_Click(object sender, EventArgs e)
{
    Text = txtSearch.Text;
    Text2 = txtSearch2.Text;
    Text3 = txtSearch3.Text;
    Response.Redirect("search.aspx");
}

search.aspx 中的代码:

   <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource1"
    OnSelectedIndexChanged="GridView1_SelectedIndexChanged" DataKeyNames="ID">
    <Columns>
        <asp:CommandField ShowSelectButton="True" />
        <asp:BoundField DataField="ID" HeaderText="ID" SortExpression="ID" InsertVisible="False"
            ReadOnly="True" />
        <asp:BoundField DataField="LandID" HeaderText="LandID" SortExpression="LandID" />
        <asp:BoundField DataField="Bilmaerk" HeaderText="Bilmaerk" SortExpression="Bilmaerk" />
        <asp:BoundField DataField="Model" HeaderText="Model" SortExpression="Model" />
        <asp:BoundField DataField="Variant" HeaderText="Variant" SortExpression="Variant" />
        <asp:BoundField DataField="StartYear" HeaderText="StartYear" SortExpression="StartYear" />
        <asp:BoundField DataField="SlutYear" HeaderText="SlutYear" SortExpression="SlutYear" />
        <asp:BoundField DataField="Volumen" HeaderText="Volumen" SortExpression="Volumen" />
        <asp:BoundField DataField="MaxDrej" HeaderText="MaxDrej" SortExpression="MaxDrej" />
        <asp:BoundField DataField="AntalCylindre" HeaderText="AntalCylindre" SortExpression="AntalCylindre" />
        <asp:BoundField DataField="TopHast" HeaderText="TopHast" SortExpression="TopHast" />
        <asp:BoundField DataField="Acceleration" HeaderText="Acceleration" SortExpression="Acceleration" />
        <asp:BoundField DataField="Beskrivelse" HeaderText="Beskrivelse" SortExpression="Beskrivelse" />
        <asp:BoundField DataField="Effekt" HeaderText="Effekt" SortExpression="Effekt" />
        <asp:BoundField DataField="Picture" HeaderText="Picture" SortExpression="Picture" />
    </Columns>
</asp:GridView>
<selectparameters>
<asp:ControlParameter ControlID="<%=Text1 %>"></asp:ControlParameter>
<asp:ControlParameter ControlID="<%=Text2 %>"></asp:ControlParameter>
<asp:ControlParameter ControlID="<%=Text3 %>"></asp:ControlParameter>
</selectparameters>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:CarConnectionString %>"
    SelectCommand="SELECT * FROM [Bil] WHERE (([LandID] LIKE '%' +@LandID+ '%') AND ([Bilmaerk] LIKE '%' +@Bilmaerk+ '%') AND ([Model] LIKE '%' +@Model+ '%'))">
    <SelectParameters>
        <asp:ControlParameter ControlID="Text1" Name="LandID" PropertyName="Text" Type="String"
            DefaultValue="%" />
        <asp:ControlParameter ControlID="Text2" Name="Bilmaerk" PropertyName="Text" Type="String"
            DefaultValue="%" />
        <asp:ControlParameter ControlID="Text3" Name="Model" PropertyName="Text" Type="String"
            DefaultValue="%" />
    </SelectParameters>
</asp:SqlDataSource>

search.aspx.cs 中的相关代码:

   public partial class search : System.Web.UI.Page
   {
protected string Text1 { get; set; }
protected string Text2 { get; set; }
protected string Text3 { get; set; }

protected void Page_Load(object sender, EventArgs e)
{
    Text1 = SiteMaster.Text;
    Text2 = SiteMaster.Text2;
    Text3 = SiteMaster.Text3;
}
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
    Constructor.SelectedCar = GridView1.SelectedRow.Cells[1].Text;
    Response.Redirect("Selected.aspx");
}
}
4

3 回答 3

0

为什么不使用会话将数据从一页传输到另一页..?它很简单,

//this is how you will create the Session on master page 
{
   Session["text"] = txtearch.Text;
   Session["Text2"] = txtSearch2.Text;
   Session["Text3"] = txtSearch3.Text;
}

on another page just use it like
{
   string a = Session["text"].ToString();
   string b = Session["text2"].ToString();
   string c = Session["text3"].ToString();
}
于 2012-10-23T11:40:58.323 回答
0

尝试使用如下代码的 HTTP GET 方法:

在您的 Site.Master.Cs 上

protected void cmdSearch_Click(object sender, EventArgs e)
{
    Text = txtSearch.Text;
    Text2 = txtSearch2.Text;
    Text3 = txtSearch3.Text;
    Response.Redirect(string.Format("search.aspx?txtSearch={0}&txtSearch2={1}&txtSearch3={2}", Text, Text2, Tex3));
}

在你的 search.aspx.cs

protected void Page_Load(object sender, EventArgs e)
{
    Text1 = Request["txtSearch"];
    Text2 = Request["txtSearch2"];
    Text3 = Request["txtSearch3"];
}

您还可以通过调用将 GET 参数添加到母版页控件:

(Master.FindControl("txtSearch") as TextBox).Text = Request["txtSearch"];
于 2012-10-23T11:55:46.913 回答
0

在基于页面生命周期的主页面加载或页面预渲染时,最初将不同搜索字段的文本框的值分别存储到会话变量中。

然后您可以通过单击搜索按钮访问这些变量,然后您可以继续执行基本步骤,例如进入数据库并搜索相关字段等。

通过使用这些会话变量,您可以在任何页面的任何位置访问它们,除非您更改值....

   public void search(object sender ,EventArgs e)
    {
        string Search = session["search"].toString();
        Search =  Search + "%";
        SqlDataSource1.SelectParameters.Clear();
        SqlDataSource1.SelectParameters.Add("search", Search);
            Search = "%" + Search;//for user name ,user email,languages made the search criteria as anywhere in the word for others it is starting letter
            SqlDataSource1.SelectParameters.Clear();//again we have to clear and add since search is changed
            SqlDataSource1.SelectParameters.Add("search", Search);
            SqlDataSource1.SelectCommand = "SELECT [UserID], [UserName], [UserEmail], [UserContact], [UserAddress], [Gender], [Languages], [Status], [Passwords],[Image] FROM [Users_raghu] where [UserName] like @search";
    }

这是您可以在此处参考的示例我只给了您一个搜索变量在您的情况下您必须采用 3 个会话变量并继续进行

希望这会有所帮助:D

于 2012-10-23T11:51:36.160 回答