views:

497

answers:

2

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?

+1  A: 

Check this link hope this will help you http://asimsajjad.blogspot.com/2009/09/raising-dropdownlist.html

Asim Sajjad
Thanks, that method seems to do the trick. Much appreciated.
ghills
+1  A: 

I noticed one thing in your code that you have called GridView2.DataBind() on page load....try putting it in Page Prerender because when an event is fired it calls page load and your gridview will get refreshed before the event is handled....

however it is not always that it causes issues

Pankaj Kumar
Very good point, when I tried using the other solution this actually did pose a problem. I used your suggestion and it worked great. Thanks!
ghills