[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 176
  • Last Modified:

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]
/****** Object:  Table [dbo].[Employees]    Script Date: 04/17/2009 20:08:19 ******/
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

-- Use these scripts to populate the new database with some test data

insert into Employees values (1,'Romney','Smiles','Software Developer','Atlanta')
insert into Employees values (1,'Allen','Vpors','Technician','Phoenix')
insert into Employees values (1,'Dan','Goberg','Dentist','Chicago')
<%@ 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>
    <form id="form1" runat="server">
        <asp:SqlDataSource ID="sourceEmployees" runat="server" 
              ConnectionString="<%$ ConnectionStrings:EmployeeCitiesConnectionString %>" 
              SelectCommand="SELECT EmployeeID, FirstName, LastName, Title, City FROM Employees WHERE City=@City">
                <asp:ControlParameter ControlID="lstCities" Name="City" PropertyName="SelectedValue" />
        <asp:SqlDataSource ID="sourceEmployeeCities" runat="server" ProviderName="System.Data.SqlClient" ConnectionString="<%$ ConnectionStrings:EmployeeCitiesConnectionString %>" SelectCommand="SELECT DISTINCT City FROM Employees">
        <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" />
                <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" />

Open in new window

1 Solution
The control parameter for sqldatasource is dropdownlist

 <asp:ControlParameter ControlID="lstCities" Name="City" PropertyName="SelectedValue" />

so when u change the selection in dropdownlist, it change the selectedvalue and pass that parameter value in sqldatasource SELECT command. Also the Autopostback property of dropdownlist is TRUE that causes postback.

Thats how this code is working and gridview data repopulate on dropdown value changes.
brgdotnetAuthor Commented:
Yes, the ControlParameter is the "Tie that binds" the two controls together.

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now