brgdotnet
asked on
Question about data binding between two controls on an asp.net web form
Hello, I am working with data binding on an asp.net web form. The form consist sof a drop down box on an Asp.net web page. It also contains a grid control. The way the code works is that when someone selects a value from the list box, the data associated with the listbox item is displayed in a grid. What I don't understand is how the selected item from the drop down listbox will trigger the re-population of the grid control with new data. I see that the drop down listbox has auto postback set to true, but I don't think that is the "tie that binds" the two controls together so to speak. Can someone help me to understand? Where is the link between the two controls that causes the grid to be re-populated with new data?
In the code snippet below is the .aspx code. And here are some scripts to create and populate a test database for this code.
USE [EmployeeCities]
GO
/****** Object: Table [dbo].[Employees] Script Date: 04/17/2009 20:08:19 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Employees](
[EmployeeID] [int] NULL,
[FirstName] [nvarchar](50) COLLATE SQL_Latin1_General_CP1_CI_ AS NULL,
[LastName] [nvarchar](50) COLLATE SQL_Latin1_General_CP1_CI_ AS NULL,
[Title] [nvarchar](50) COLLATE SQL_Latin1_General_CP1_CI_ AS NULL,
[City] [nvarchar](50) COLLATE SQL_Latin1_General_CP1_CI_ AS NULL
) ON [PRIMARY]
-- Use these scripts to populate the new database with some test data
insert into Employees values (1,'Romney','Smiles','Soft ware Developer','Atlanta')
insert into Employees values (1,'Allen','Vpors','Techni cian','Pho enix')
insert into Employees values (1,'Dan','Goberg','Dentist ','Chicago ')
In the code snippet below is the .aspx code. And here are some scripts to create and populate a test database for this code.
USE [EmployeeCities]
GO
/****** Object: Table [dbo].[Employees] Script Date: 04/17/2009 20:08:19 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Employees](
[EmployeeID] [int] NULL,
[FirstName] [nvarchar](50) COLLATE SQL_Latin1_General_CP1_CI_
[LastName] [nvarchar](50) COLLATE SQL_Latin1_General_CP1_CI_
[Title] [nvarchar](50) COLLATE SQL_Latin1_General_CP1_CI_
[City] [nvarchar](50) COLLATE SQL_Latin1_General_CP1_CI_
) ON [PRIMARY]
-- Use these scripts to populate the new database with some test data
insert into Employees values (1,'Romney','Smiles','Soft
insert into Employees values (1,'Allen','Vpors','Techni
insert into Employees values (1,'Dan','Goberg','Dentist
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Def.aspx.cs" Inherits="Def" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:SqlDataSource ID="sourceEmployees" runat="server"
ConnectionString="<%$ ConnectionStrings:EmployeeCitiesConnectionString %>"
SelectCommand="SELECT EmployeeID, FirstName, LastName, Title, City FROM Employees WHERE City=@City">
<SelectParameters>
<asp:ControlParameter ControlID="lstCities" Name="City" PropertyName="SelectedValue" />
</SelectParameters>
</asp:SqlDataSource>
<asp:SqlDataSource ID="sourceEmployeeCities" runat="server" ProviderName="System.Data.SqlClient" ConnectionString="<%$ ConnectionStrings:EmployeeCitiesConnectionString %>" SelectCommand="SELECT DISTINCT City FROM Employees">
</asp:SqlDataSource>
<asp:DropDownList ID="lstCities" runat="server" DataSourceID="sourceEmployeeCities"
DataTextField="City" Width="205px" AutoPostBack="True">
</asp:DropDownList><br />
<br />
<asp:GridView ID="GridView1" runat="server" DataSourceID="sourceEmployees"
CellPadding="4" Font-Names="Verdana" Font-Size="Small" ForeColor="#333333"
GridLines="None" AutoGenerateColumns="False">
<FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
<RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
<PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
<HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:BoundField DataField="EmployeeID" HeaderText="EmployeeID"
SortExpression="EmployeeID" />
<asp:BoundField DataField="FirstName" HeaderText="FirstName" SortExpression="FirstName" />
<asp:BoundField DataField="LastName" HeaderText="LastName" SortExpression="LastName" />
<asp:BoundField DataField="Title" HeaderText="Title" SortExpression="Title" />
<asp:BoundField DataField="City" HeaderText="City" SortExpression="City" />
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER