Question about data binding between two controls on an web form

Posted on 2009-04-17
Last Modified: 2012-05-06
Hello, I am working with data binding on an web form. The form consist sof a drop down box on an 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" "">

<html xmlns="">

<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

Question by:brgdotnet
    LVL 6

    Accepted 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.
    LVL 2

    Author Closing Comment

    Yes, the ControlParameter is the "Tie that binds" the two controls together.

    Featured Post

    Find Ransomware Secrets With All-Source Analysis

    Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

    Join & Write a Comment

    Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
    In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
    It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
    how to add IIS SMTP to handle application/Scanner relays into office 365.

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now