• home
  • Email: trainings@synapseindia.careers

Blogs

back

Understanding Complex Data Binding in ASP.NET Web Forms Part-II

Mar 10, 2016

Data Binding can be achieved in various forms with various controls available in ASP.NET WebForms. In earlier article, we came to know about binding table column data to DropDownList control. In earlier article we were fetching all values of employee id field into DropDownList. Take an example where we need to fetch only filtered data into DropDownList , than how it can be achieved? In this situation , we will pass parameters into SqlDataSource as given below. In this example, we have taken a TextBox, Button and DropDownList. On first request , DropDownList will display the cities of UttarPradesh as default written into TextBox, Have a look into the code below, similarly you can perform same task on click of button:

 
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!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></title>
</head>
<body>
<form id="form1" runat="server">
<div>

<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:myconnection %>"
SelectCommand="select * from info where state=@st">
<SelectParameters>
<asp:ControlParameter ControlID="TextBox1" Name="st" PropertyName="Text" />
</SelectParameters>

</asp:SqlDataSource>

</div>
<asp:DropDownList ID="DropDownList1" runat="server">
</asp:DropDownList>
<asp:TextBox ID="TextBox1" runat="server">UttarPradesh</asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Button" />
</form>
</body>
</html>

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
       

        DropDownList1.DataSourceID = "SqlDataSource1";
        DropDownList1.DataTextField = "city";
      
    }
    
}

Above example works fine for fetching the filtered data from server. SqlDataSource provide various other ways also for filtering data like from querystring, control, default parameter and cookies etc. We will come to know about other approaches in coming articles. SynapseIndia is providing many standard modules in it's training program started on one of it's SynapseIndia technologies like .NET. We have been working on various technologies and simultaneosuly providing a good platform to freshers to learn .NET from the best development team and at our development premises.  

SynapseIndia (CEO: Shamit Khemka)