I have a GridView control which I want to have 2 visible fields, a label and a dropdown. Instead of using a RowEditTemplate, I was hoping to just define the ItemTemplate to be the dropdown value.
On the event that a dropdown is changed, I want to execute some code where I can get the integer key for that gridview row, and the new selected value of the dropdown. Something like the following:
<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False"
DataKeyNames="pk_id" onrowdeleting="GridView2_RowDeleting"
onrowcommand="GridView2_RowCommand">
<Columns>
<asp:BoundField DataField="pk_id" Visible="False" />
<asp:BoundField DataField="Column1" HeaderText="Column1" />
<asp:TemplateField HeaderText="Column2">
<ItemTemplate>
<asp:DropDownList ID="DropDownList3" runat="server"
DataSourceID="SqlDataSource" DataTextField="name"
DataValueField="id2"
SelectedValue='<%# Bind("id2") %>'
AutoPostBack="True">
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource" runat="server"
ConnectionString="<%$ ConnectionStrings:Main %>"
SelectCommand="get_TestData" SelectCommandType="StoredProcedure">
</asp:SqlDataSource>
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowDeleteButton="True" />
</Columns>
</asp:GridView>
CODE BEHIND
protected void Page_Load(object sender, EventArgs e)
{
...
GridView2.DataSource = new Person(id).GetDataSet();
GridView2.DataBind();
}
protected void GridView2_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
// Call delete function with the key
// Delete(key);
}
protected void GridView2_RowCommand(object sender, GridViewCommandEventArgs e)
{
// figure out which row sent it
// UpdateRecord(key, newvalue)
}
The dropdown is populated and value selected properly, but I can not get the deletion or updating by dropdown to work properly. I have tried implementing these functions, but the RowCommand does not seem to fire when the dropdown triggers a postback. Even if it does trigger, I'm not sure how to get the values I need. Is there a better way I should be going about this?