Hi ramco1917,
Please refer below sample.
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
User Control
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="WebUserControl.ascx.cs"
Inherits="WebUserControl" %>
<asp:Label ID="lblCountry" runat="server">Welcome to ASPSnippets.</asp:Label>
HTML
<%@ Register Src="~/WebUserControl.ascx" TagPrefix="uc" TagName="MyControl" %>
<asp:DropDownList ID="ddlCountries" runat="server" OnSelectedIndexChanged="OnSelectedIndexChanged"
AutoPostBack="true">
</asp:DropDownList>
<hr />
<uc:MyControl ID="ucMyControl" runat="server" Visible="false" />
Namespaces
C#
using System.Data;
using System.Configuration;
using System.Data.SqlClient;
VB.Net
Imports System.Data
Imports System.Configuration
Imports System.Data.SqlClient
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(conString))
{
using (SqlCommand cmd = new SqlCommand("SELECT * FROM Customers", con))
{
con.Open();
ddlCountries.DataSource = cmd.ExecuteReader();
ddlCountries.DataTextField = "Name";
ddlCountries.DataValueField = "Country";
ddlCountries.DataBind();
con.Close();
}
}
}
}
protected void OnSelectedIndexChanged(object sender, EventArgs e)
{
if (ddlCountries.SelectedItem.Value == "India")
{
ucMyControl.Visible = true;
}
else
{
ucMyControl.Visible = false;
}
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Dim conString As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As SqlConnection = New SqlConnection(conString)
Using cmd As SqlCommand = New SqlCommand("SELECT * FROM Customers", con)
con.Open()
ddlCountries.DataSource = cmd.ExecuteReader()
ddlCountries.DataTextField = "Name"
ddlCountries.DataValueField = "Country"
ddlCountries.DataBind()
con.Close()
End Using
End Using
End If
End Sub
Protected Sub OnSelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
If ddlCountries.SelectedItem.Value = "India" Then
ucMyControl.Visible = True
Else
ucMyControl.Visible = False
End If
End Sub
Screenshot