You may want to implement some functionality on changing index of a drop down list which is provided within a grid for multi records. I too had to do the same and searched over internet for some suitable solution. Though there are more than one solutions but the solution I am providing is most easy of them and requires no tricks.
The Solution
Note the RED BOLD code of HTML view, which means you will have to set EnableViewState="false" for the griview and also you will have to set AutoPostBack="true" for the dropdownlist.
Here is your GridView code (The HTML)
<asp:GridView runat="server" ID="gvTest" Width="50%" AutoGenerateColumns="false" EnableViewState="false">
<Columns>
<asp:BoundField DataField="Gender" HeaderText="Gender" HeaderStyle-HorizontalAlign="Left" />
<asp:TemplateField >
<ItemTemplate>
<asp:DropDownList ID="ddlTest" AutoPostBack="true" runat="server" OnSelectedIndexChanged="ddlTest_SelectedIndexChanged">
<asp:ListItem Value="Male">Male</asp:ListItem>
<asp:ListItem Value="Female">Female</asp:ListItem>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
And here is Code behind
1. C#
2. VB.Net
The Solution
Note the RED BOLD code of HTML view, which means you will have to set EnableViewState="false" for the griview and also you will have to set AutoPostBack="true" for the dropdownlist.
Here is your GridView code (The HTML)
<asp:GridView runat="server" ID="gvTest" Width="50%" AutoGenerateColumns="false" EnableViewState="false">
<Columns>
<asp:BoundField DataField="Gender" HeaderText="Gender" HeaderStyle-HorizontalAlign="Left" />
<asp:TemplateField >
<ItemTemplate>
<asp:DropDownList ID="ddlTest" AutoPostBack="true" runat="server" OnSelectedIndexChanged="ddlTest_SelectedIndexChanged">
<asp:ListItem Value="Male">Male</asp:ListItem>
<asp:ListItem Value="Female">Female</asp:ListItem>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
And here is Code behind
1. C#
protected
void Page_Load(object sender, System.EventArgs e)
{
try {
FillGridView();
} catch
(Exception ex) {
Response.Write(ex.Message);
}
}
private
void FillGridView()
{
try {
DataTable dt = new DataTable();
dt.Columns.Add("Gender");
DataRow dr = null;
dr = dt.NewRow();
dr["Gender"] = "I
am a ";
dt.Rows.Add(dr);
gvTest.DataSource = dt;
gvTest.DataBind();
} catch
(Exception ex) {
Response.Write(ex.Message);
}
}
protected
void ddlTest_SelectedIndexChanged(object sender, System.EventArgs e)
{
try {
Response.Write("You selected
" + ((DropDownList)sender).SelectedItem.Value);
} catch
(Exception ex) {
Response.Write(ex.Message);
}
}
2. VB.Net
Protected Sub Page_Load(sender As
Object, e As
System.EventArgs) Handles
Me.Load
Try
FillGridView()
Catch
ex As Exception
Response.Write(ex.Message)
End Try
End Sub
Private Sub FillGridView()
Try
Dim
dt As New DataTable
dt.Columns.Add("Gender")
Dim
dr As DataRow
dr = dt.NewRow
dr("Gender")
= "I am a "
dt.Rows.Add(dr)
gvTest.DataSource = dt
gvTest.DataBind()
Catch
ex As Exception
Response.Write(ex.Message)
End Try
End Sub
Protected Sub ddlTest_SelectedIndexChanged(sender As Object, e As System.EventArgs)
Try
Response.Write("You selected " & CType(sender, DropDownList).SelectedItem.Value)
Catch
ex As Exception
Response.Write(ex.Message)
End Try
End Sub
Thank you sooooooooooooo Much... I got it!!
ReplyDeleteThank you
ReplyDelete