In this article I will explain with an example, how to get Selected Value from DropDownList in GridView in ASP.Net using C# and VB.Net.
The DropDownList inside GridView will be assigned with an OnSelectedIndexChanged event handler and when an Item is selected in the DropDownList, the Selected Value is displayed using JavaScript Alert Message Box.
HTML Markup
The HTML Markup consists of an ASP.Net GridView containing two BoundField columns and a TemplateField column.
The TemplateField columns consist of a DropDownList which has been assigned an OnSelectedIndexChanged event handler and the AutoPostBack property is set to True.
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="Id" HeaderText="Customer Id" ItemStyle-Width="80" />
<asp:BoundField DataField="Name" HeaderText="Name" ItemStyle-Width="120" />
<asp:TemplateField HeaderText="Country" ItemStyle-Width="120">
<ItemTemplate>
<asp:DropDownList ID = "ddlCountries" runat="server" AutoPostBack = "true" OnSelectedIndexChanged = "SelectedIndexChanged" SelectedValue='<%# Eval("Country") %>'>
<asp:ListItem Text = "United States" Value = "United States"></asp:ListItem>
<asp:ListItem Text = "India" Value = "India"></asp:ListItem>
<asp:ListItem Text = "France" Value = "France"></asp:ListItem>
<asp:ListItem Text = "Russia" Value = "Russia"></asp:ListItem>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Namespaces
You will need to import the following namespace.
C#
VB.Net
Binding the ASP.Net GridView control
The GridView is populated with a dynamic DataTable with some dummy data inside the Page Load event.
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[3] { new DataColumn("Id"), new DataColumn("Name"), new DataColumn("Country") });
dt.Rows.Add(1, "John Hammond", "United States");
dt.Rows.Add(2, "Mudassar Khan", "India");
dt.Rows.Add(3, "Suzanne Mathews", "France");
dt.Rows.Add(4, "Robert Schidner", "Russia");
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
VB.Net
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Dim dt As New DataTable()
dt.Columns.AddRange(New DataColumn(2) {New DataColumn("Id"), New DataColumn("Name"), New DataColumn("Country")})
dt.Rows.Add(1, "John Hammond", "United States")
dt.Rows.Add(2, "Mudassar Khan", "India")
dt.Rows.Add(3, "Suzanne Mathews", "France")
dt.Rows.Add(4, "Robert Schidner", "Russia")
GridView1.DataSource = dt
GridView1.DataBind()
End If
End Sub
Get Selected Value from DropDownList in GridView
When an Item is selected (changed) in the DropDownList, first the DropDownList is referenced and its ID is determined.
Then the updated (changed) Selected Text of the DropDownList is displayed using JavaScript Alert Message Box.
C#
protected void SelectedIndexChanged(object sender, EventArgs e)
{
//Reference the DropDownList.
DropDownList dropDownList = sender as DropDownList;
//Get the ID of the DropDownList.
string id = dropDownList.ID;
//Display the Selected Text of DropDownList.
ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", "alert('" + dropDownList.SelectedItem.Text + "');", true);
}
VB.Net
Protected Sub SelectedIndexChanged(sender As Object, e As EventArgs)
'Reference the DropDownList.
Dim dropDownList As DropDownList = CType(sender, DropDownList)
'Get the ID of the DropDownList.
Dim id As String = dropDownList.ID
'Display the Text of DropDownList.
ClientScript.RegisterClientScriptBlock(Me.GetType(), "alert", "alert('" & dropDownList.SelectedItem.Text & "');", True)
End Sub
Screenshot
Demo
Downloads