使用两个下拉列表构建一个链接到SQL数据源的简单页面。我是ASP.NET的新手,所以请原谅我的简单新手错误。至少我在学习。无论如何,对象是有2个下拉列表,使用常见的Northwind数据库选择姓氏和公司名称。我使用empID和custID作为sql参数。我最初并不想让页面加载网格,所以我隐藏了它并创建了一个清晰的按钮。当我运行页面时,它将提取正确的数据,但当我选择不同的姓氏时,页面会提取原始数据。有人能帮助我理解我的错误在哪里吗?
asp.net code:
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<link href="StyleSheet.css" rel="stylesheet" type="text/css" />
</head>
<body>
<header><h1>Welcome to my First SQL Data Source Page</h1></header>
<h3>Select a Value from both drop Down Lists to find the orders that employee made</h3>
<form id="form1" runat="server">
<div>
<asp:DropDownList ID="DropDownList1" runat="server" AppendDataBoundItems="True" AutoPostBack="True" DataSourceID="SqlDataSource1" DataTextField="LastName" DataValueField="EmployeeID" BackColor="#00CCFF" Height="25px" Width="200px">
<asp:ListItem>--Select Last Name--</asp:ListItem>
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>" SelectCommand="SELECT [LastName], [EmployeeID] FROM [Employees]"></asp:SqlDataSource>
<br />
<br />
<asp:DropDownList ID="DropDownList2" runat="server" AppendDataBoundItems="True" AutoPostBack="True" DataSourceID="SqlDataSource2" DataTextField="CompanyName" DataValueField="CustomerID" BackColor="#00CCFF" Height="25px" Width="200px">
<asp:ListItem>--Select Company Name--</asp:ListItem>
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>" SelectCommand="SELECT [CompanyName], [CustomerID] FROM [Customers]"></asp:SqlDataSource>
<br />
<br />
<asp:SqlDataSource ID="SqlDataSource3" runat="server" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>" SelectCommand="SELECT * FROM ORDERS
WHERE
EmployeeID = @P1
AND
CustomerID = @P2
">
<SelectParameters>
<asp:ControlParameter ControlID="DropDownList1" Name="P1" PropertyName="SelectedValue" />
<asp:ControlParameter ControlID="DropDownList2" Name="P2" PropertyName="SelectedValue" />
</SelectParameters>
</asp:SqlDataSource>
<br />
<br />
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="OrderID" DataSourceID="SqlDataSource3"
EmptyDataText="There were no orders by this employee">
<alternatingrowstyle backcolor="#0066FF"
forecolor="DarkBlue"
font-italic="true"/>
<Columns>
<asp:BoundField DataField="OrderID" HeaderText="OrderID" InsertVisible="False" ReadOnly="True" SortExpression="OrderID" />
<asp:BoundField DataField="CustomerID" HeaderText="CustomerID" SortExpression="CustomerID" />
<asp:BoundField DataField="EmployeeID" HeaderText="EmployeeID" SortExpression="EmployeeID" />
<asp:BoundField DataField="OrderDate" HeaderText="OrderDate" SortExpression="OrderDate" />
<asp:BoundField DataField="RequiredDate" HeaderText="RequiredDate" SortExpression="RequiredDate" />
<asp:BoundField DataField="ShippedDate" HeaderText="ShippedDate" SortExpression="ShippedDate" />
<asp:BoundField DataField="ShipVia" HeaderText="ShipVia" SortExpression="ShipVia" />
<asp:BoundField DataField="Freight" HeaderText="Freight" SortExpression="Freight" />
<asp:BoundField DataField="ShipName" HeaderText="ShipName" SortExpression="ShipName" />
<asp:BoundField DataField="ShipAddress" HeaderText="ShipAddress" SortExpression="ShipAddress" />
<asp:BoundField DataField="ShipCity" HeaderText="ShipCity" SortExpression="ShipCity" />
<asp:BoundField DataField="ShipRegion" HeaderText="ShipRegion" SortExpression="ShipRegion" />
<asp:BoundField DataField="ShipPostalCode" HeaderText="ShipPostalCode" SortExpression="ShipPostalCode" />
<asp:BoundField DataField="ShipCountry" HeaderText="ShipCountry" SortExpression="ShipCountry" />
</Columns>
<HeaderStyle BackColor="#0066FF" ForeColor="White" />
</asp:GridView>
<br />
<br />
<br />
<asp:Button ID="Button1" runat="server" Text="Search" BackColor="#00CCFF" BorderStyle="Inset" EnableTheming="True" Height="30px" Width="150px" />
<br />
<br />
<br />
<asp:Button ID="Button2" runat="server" Text="Clear" BackColor="#00CCFF" BorderStyle="Inset" Height="30px" Width="150px" />
</div>
</form>
</body>
</html>
Visual Basic代码:
Partial Class _Default
Inherits System.Web.UI.Page
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
GridView1.Visible = False
End Sub
Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
GridView1.Visible = True
End Sub
Protected Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
GridView1.Visible = False
End Sub
End Class
答案 0 :(得分:1)
使用Page.IsPostBack
方法检查是新增加载还是响应用户操作。仅在第一次加载页面时隐藏。
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
If Not IsPostBack() Then GridView1.Visible = False
End Sub