Hi BugHunter,
Refer below sample.
HTML
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script src="http://ajax.aspnetcdn.com/ajax/jquery.ui/1.8.9/jquery-ui.js" type="text/javascript"></script>
<link href="http://ajax.aspnetcdn.com/ajax/jquery.ui/1.8.9/themes/start/jquery-ui.css"
rel="stylesheet" type="text/css" />
<script type="text/javascript">
function ShowPopup() {
$("#dialog").dialog({
title: "Popup",
width: 450,
buttons: {
Ok: function () {
$(this).dialog('close');
}
},
modal: true
}).parent().appendTo($("form:first"));
}
</script>
<asp:ScriptManager ID="src" runat="server">
</asp:ScriptManager>
<div id="dialog" style="display: none">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title" id="myModalLabel">
<span style="text-align: center">Masters Data</span></h4>
</div>
<div class="modal-body">
<asp:DropDownList ID="ddlMaster" runat="server" AutoPostBack="true" OnSelectedIndexChanged="GetValue_OnSelectedIndexChanged">
</asp:DropDownList>
<asp:Label ID="lblCountry" runat="server" />
</div>
</div>
</div>
</div>
<asp:GridView ID="GridView1" ShowHeader="false" runat="server" AutoGenerateColumns="false"
OnRowDataBound="GridView1_RowDataBound">
<Columns>
<asp:BoundField DataField="Country" HeaderText="Country" />
<asp:TemplateField>
<ItemTemplate>
<table>
<tr>
<td>
<asp:Label ID="lblCountry" runat="server" Text='<%# Eval("Country") %>' Visible="false" />
<asp:DropDownList ID="ddlMaster_1" runat="server">
<asp:ListItem Value="0" Text="--Select (File1)--" />
</asp:DropDownList>
<asp:CheckBox ID="CheckBox1" Text="" ToolTip="Select From Masters" runat="server"
OnCheckedChanged="open" AutoPostBack="true" />
</td>
</tr>
</table>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Namespaces
C#
using System.Data;
using System.Configuration;
using System.Data.SqlClient;
VB.Net
Imports System.Data.SqlClient
Imports System.Data
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("SELECT * FROM Employees", con))
{
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
}
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DropDownList ddlCountries = (e.Row.FindControl("ddlMaster_1") as DropDownList);
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("SELECT * FROM Employees", con))
{
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
ddlCountries.DataSource = dt;
ddlCountries.DataTextField = "Country";
ddlCountries.DataValueField = "Country";
ddlCountries.DataBind();
ddlCountries.Items.Insert(0, new ListItem("Please select"));
string country = (e.Row.FindControl("lblCountry") as Label).Text;
ddlCountries.Items.FindByValue(country).Selected = true;
}
}
}
}
protected void open(object sender, EventArgs e)
{
CheckBox chk = (sender) as CheckBox;
GridViewRow row = (GridViewRow)chk.NamingContainer;
string id = (row.FindControl("ddlMaster_1") as DropDownList).SelectedItem.Text;
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("SELECT DISTINCT Country FROM Employees", con))
{
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
ddlMaster.DataSource = dt;
ddlMaster.DataTextField = "Country";
ddlMaster.DataValueField = "Country";
ddlMaster.DataBind();
ddlMaster.Items.Insert(0, new ListItem("Please select"));
}
}
ClientScript.RegisterStartupScript(this.GetType(), "Popup", "ShowPopup();", true);
}
protected void GetValue_OnSelectedIndexChanged(object sender, EventArgs e)
{
lblCountry.Text = ddlMaster.SelectedItem.Text;
ClientScript.RegisterStartupScript(this.GetType(), "Popup", "ShowPopup();", true);
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As SqlConnection = New SqlConnection(constr)
Using cmd As SqlCommand = New SqlCommand("SELECT * FROM Employees", con)
Dim da As SqlDataAdapter = New SqlDataAdapter(cmd)
Dim dt As DataTable = New DataTable()
da.Fill(dt)
GridView1.DataSource = dt
GridView1.DataBind()
End Using
End Using
End If
End Sub
Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then
Dim ddlCountries As DropDownList = (TryCast(e.Row.FindControl("ddlMaster_1"), DropDownList))
Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As SqlConnection = New SqlConnection(constr)
Using cmd As SqlCommand = New SqlCommand("SELECT * FROM Employees", con)
Dim da As SqlDataAdapter = New SqlDataAdapter(cmd)
Dim dt As DataTable = New DataTable()
da.Fill(dt)
ddlCountries.DataSource = dt
ddlCountries.DataTextField = "Country"
ddlCountries.DataValueField = "Country"
ddlCountries.DataBind()
ddlCountries.Items.Insert(0, New ListItem("Please select"))
Dim country As String = (TryCast(e.Row.FindControl("lblCountry"), Label)).Text
ddlCountries.Items.FindByValue(country).Selected = True
End Using
End Using
End If
End Sub
Protected Sub open(ByVal sender As Object, ByVal e As EventArgs)
Dim chk As CheckBox = TryCast((sender), CheckBox)
Dim row As GridViewRow = CType(chk.NamingContainer, GridViewRow)
Dim id As String = (TryCast(row.FindControl("ddlMaster_1"), DropDownList)).SelectedItem.Text
Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As SqlConnection = New SqlConnection(constr)
Using cmd As SqlCommand = New SqlCommand("SELECT DISTINCT Country FROM Employees", con)
Dim da As SqlDataAdapter = New SqlDataAdapter(cmd)
Dim dt As DataTable = New DataTable()
da.Fill(dt)
ddlMaster.DataSource = dt
ddlMaster.DataTextField = "Country"
ddlMaster.DataValueField = "Country"
ddlMaster.DataBind()
ddlMaster.Items.Insert(0, New ListItem("Please select"))
End Using
End Using
ClientScript.RegisterStartupScript(Me.GetType(), "Popup", "ShowPopup();", True)
End Sub
Protected Sub GetValue_OnSelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
lblCountry.Text = ddlMaster.SelectedItem.Text
ClientScript.RegisterStartupScript(Me.GetType(), "Popup", "ShowPopup();", True)
End Sub
Screenshot