Create a new Web page and switch to Design view.
In the AJAX Extensions tab of the toolbox, double-click the ScriptManager control to add it to the page.
Double-click the UpdatePanel control in the toolbox to add an UpdatePanel control to the page.
.png)
Click inside the UpdatePanel control, and then in the Data tab of the toolbox, double-click the DataList control.
In the DataList Tasks panel, from the Choose Data Source list, select <New data source…>.
Note: |
|---|
If you do not see the DataList Tasks panel, right-click the DataList control and select Show Smart Tag. |
The Data Source Configuration wizard is displayed.
Select Database, accept the default name SqlDataSource1, and then click OK.
In the Which data connection should your application use to connect to the database list, select NorthwindConnectionString, and then click Next.
Under How would you like to retrieve data from your database, select Specify columns from a table or view, select Products from the list, and in the Columns list, select ProductID and ProductName.
Click the WHERE button.
The Add WHERE Clause dialog box is displayed.
In the Columns list, select CategoryID and in the Source list, select None.
In the Parameter properties section of the dialog box, in the Value text box, enter 1.
Click Add to add the WHERE clause to the SQL statement.
Click OK to close the Add WHERE Clause dialog box.
.png)
Click Next and then click Finish to close the wizard.
Switch to Source view and confirm that the SqlDataSource control resembles the following example:
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
SelectCommand="SELECT [ProductName], [ProductID] FROM [Alphabetical list of products] WHERE ([CategoryID] = @CategoryID)">
<SelectParameters>
<asp:Parameter DefaultValue="1" Name="CategoryID" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
SelectCommand="SELECT [ProductName], [ProductID] FROM [Alphabetical list of products] WHERE ([CategoryID] = @CategoryID)">
<SelectParameters>
<asp:Parameter DefaultValue="1" Name="CategoryID" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
Switch to Design view.
Select the DataList control, and in the DataList Tasks panel, click Edit Templates.
Add two Button controls to the UpdatePanel control outside the DataList control.
.Set the first button's ID property to Category1Button and its Text property to Category 1. Set the second button's ID property to Category2Button and its Text property to Category 2.
.png)
Select the UpdatePanel control, and in the Properties window, set the UpdateMode property to Conditional and set the ChildrenAsTriggers property to false.
In the Triggers box, click the ellipsis (…) button and in the UpdatePanel Trigger Collection Editor dialog box, add each category button as an asynchronous postback trigger.
.png)
Set the Click event handler for the first button to Category1Button_Click and set the Click event handler for the second button to Category2Button_Click.
Switch to Source view and create the following event handlers for the two buttons:
Protected Sub Category1Button_Click(ByVal sender As Object, ByVal e As System.EventArgs)
SqlDataSource1.SelectParameters(0).DefaultValue = "1"
End Sub
Protected Sub Category2Button_Click(ByVal sender As Object, ByVal e As System.EventArgs)
SqlDataSource1.SelectParameters(0).DefaultValue = "2"
End Sub
protected void Category1Button_Click(object sender, EventArgs e)
{
SqlDataSource1.SelectParameters[0].DefaultValue = "1";
}
protected void Category2Button_Click(object sender, EventArgs e)
{
SqlDataSource1.SelectParameters[0].DefaultValue = "2";
}
The event handler code sets the CategoryID parameter of the SelectParameters collection in the SqlDataSource control, based on which button was pressed. This enables users to toggle between two categories.
Save your changes and press CTRL+F5 to view the page in a browser.
Click Category 2 and verify that the page displays new information but does not refresh the whole page.
Note: |
|---|
Do not close the page. |
<%@ Page Language="VB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
Protected Sub Category1Button_Click(ByVal sender As Object, ByVal e As System.EventArgs)
SqlDataSource1.SelectParameters(0).DefaultValue = "1"
End Sub
Protected Sub Category2Button_Click(ByVal sender As Object, ByVal e As System.EventArgs)
SqlDataSource1.SelectParameters(0).DefaultValue = "2"
End Sub
</script>
<html >
<head runat="server">
<title>Products Display</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server" ChildrenAsTriggers="False" UpdateMode="Conditional">
<ContentTemplate>
<asp:Button ID="Category1Button" runat="server" Text="Category 1" OnClick="Category1Button_Click" />
<asp:Button ID="Category2Button" runat="server" OnClick="Category2Button_Click" Text="Category 2" />
<asp:DataList ID="DataList1" runat="server" DataKeyField="ProductID" DataSourceID="SqlDataSource1"
Width="231px">
<ItemTemplate>
ProductName:
<asp:Label ID="ProductNameLabel" runat="server" Text='<%# Eval("ProductName") %>'>
</asp:Label><br />
ProductID:
<asp:Label ID="ProductIDLabel" runat="server" Text='<%# Eval("ProductID") %>'></asp:Label><br />
<br />
</ItemTemplate>
</asp:DataList>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
SelectCommand="SELECT [ProductName], [ProductID] FROM [Alphabetical list of products] WHERE ([CategoryID] = @CategoryID)">
<SelectParameters>
<asp:Parameter DefaultValue="1" Name="CategoryID" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Category1Button" />
<asp:AsyncPostBackTrigger ControlID="Category2Button" />
</Triggers>
</asp:UpdatePanel>
</div>
</form>
</body>
</html>
<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
protected void Category1Button_Click(object sender, EventArgs e)
{
SqlDataSource1.SelectParameters[0].DefaultValue = "1";
}
protected void Category2Button_Click(object sender, EventArgs e)
{
SqlDataSource1.SelectParameters[0].DefaultValue = "2";
}
</script>
<html >
<head runat="server">
<title>Products Display</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server" ChildrenAsTriggers="False" UpdateMode="Conditional">
<ContentTemplate>
<asp:Button ID="Category1Button" runat="server" Text="Category 1" OnClick="Category1Button_Click" />
<asp:Button ID="Category2Button" runat="server" OnClick="Category2Button_Click" Text="Category 2" />
<asp:DataList ID="DataList1" runat="server" DataKeyField="ProductID" DataSourceID="SqlDataSource1"
Width="231px">
<ItemTemplate>
ProductName:
<asp:Label ID="ProductNameLabel" runat="server" Text='<%# Eval("ProductName") %>'>
</asp:Label><br />
ProductID:
<asp:Label ID="ProductIDLabel" runat="server" Text='<%# Eval("ProductID") %>'></asp:Label><br />
<br />
</ItemTemplate>
</asp:DataList>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
SelectCommand="SELECT [ProductName], [ProductID] FROM [Alphabetical list of products] WHERE ([CategoryID] = @CategoryID)">
<SelectParameters>
<asp:Parameter DefaultValue="1" Name="CategoryID" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Category1Button" />
<asp:AsyncPostBackTrigger ControlID="Category2Button" />
</Triggers>
</asp:UpdatePanel>
</div>
</form>
</body>
</html>