大家好,我在将变量从 Site.Master 转移到另一个窗口时遇到了一点问题,目的是使搜索功能可以从整个 asp.net Web 应用程序中访问...
与 Site.Master 相关的代码:
<div class="search">
<asp:Label ID="LandID" runat="server" Text="LandID"/>
<asp:TextBox ID="txtSearch" runat="server" />
<asp:Label ID="CarBrand" runat="server" Text="CarBrand" />
<asp:TextBox ID="txtSearch2" runat="server" />
<asp:Label ID="Model" runat="server" Text="Model"/>
<asp:TextBox ID="txtSearch3" runat="server" />
<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");
}
}