Hi rakeshkuma,
Use Ajax Timer control inside the UpdatePanel.
Add HiddenField and set the value on DropDownList change.
Then reset the selected value in DropDownList again.
Refer below code.
Database
I have made use of the following table Customers with the schema as follows.
I have already inserted few records in the table.
You can download the database table SQL by clicking the download link below.
Download SQL file
HTML
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
Last Refreshed: <%=DateTime.Now.ToString("hh:mm:ss tt") %>
<hr />
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:DropDownList ID="ddlCustomers" runat="server"
OnSelectedIndexChanged="OnSelectedIndexChanged">
</asp:DropDownList>
<hr />
<asp:Label ID="lblTime" runat="server" />
<asp:HiddenField ID="hfCountry" runat="server" />
<asp:Timer ID="Timer1" runat="server"
OnTick="TimerTick" Interval="2000" />
</ContentTemplate>
</asp:UpdatePanel>
Namespaces
C#
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
VB.Net
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this.BindDropDownList();
}
}
protected void TimerTick(object sender, EventArgs e)
{
this.BindDropDownList();
}
protected void OnSelectedIndexChanged(object sender, EventArgs e)
{
hfCountry.Value = ddlCustomers.SelectedValue;
}
private void BindDropDownList()
{
lblTime.Text = DateTime.Now.ToString("hh:mm:ss tt");
string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
string query = "SELECT Country FROM Customers";
using (SqlConnection con = new SqlConnection(conString))
{
using (SqlCommand cmd = new SqlCommand(query))
{
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.Connection = con;
sda.SelectCommand = cmd;
using (DataTable dt = new DataTable())
{
sda.Fill(dt);
ddlCustomers.DataSource = dt;
ddlCustomers.DataTextField = "Country";
ddlCustomers.DataValueField = "Country";
ddlCustomers.DataBind();
if (!string.IsNullOrEmpty(hfCountry.Value))
{
ddlCustomers.Items.FindByValue(hfCountry.Value).Selected = true;
}
}
}
}
}
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Me.BindDropDownList()
End If
End Sub
Protected Sub TimerTick(ByVal sender As Object, ByVal e As EventArgs)
Me.BindDropDownList()
End Sub
Protected Sub OnSelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
hfCountry.Value = ddlCustomers.SelectedValue
End Sub
Private Sub BindDropDownList()
lblTime.Text = DateTime.Now.ToString("hh:mm:ss tt")
Dim conString As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Dim query As String = "SELECT Country FROM Customers"
Using con As SqlConnection = New SqlConnection(conString)
Using cmd As SqlCommand = New SqlCommand(query)
Using sda As SqlDataAdapter = New SqlDataAdapter()
cmd.Connection = con
sda.SelectCommand = cmd
Using dt As DataTable = New DataTable()
sda.Fill(dt)
ddlCustomers.DataSource = dt
ddlCustomers.DataTextField = "Country"
ddlCustomers.DataValueField = "Country"
ddlCustomers.DataBind()
If Not String.IsNullOrEmpty(hfCountry.Value) Then
ddlCustomers.Items.FindByValue(hfCountry.Value).Selected = True
End If
End Using
End Using
End Using
End Using
End Sub
Screenshot