Home > ASP.Net > Handling Database updates with a RadioButtonList inside an ASP.NET GridView

Handling Database updates with a RadioButtonList inside an ASP.NET GridView


try this example

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="ProductID" DataSourceID="SqlDataSource1" AllowPaging="True" AllowSorting="True">
                <asp:BoundField DataField="ProductID" HeaderText="ProductID" ReadOnly="True" SortExpression="ProductID" />
                <asp:BoundField DataField="ProductName" HeaderText="ProductName" SortExpression="ProductName" />
                <asp:BoundField DataField="QuantityPerUnit" HeaderText="QuantityPerUnit" SortExpression="QuantityPerUnit" />
                <asp:BoundField DataField="UnitPrice" HeaderText="UnitPrice" SortExpression="UnitPrice" />
               <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
            SelectCommand="SELECT [ProductID], [ProductName], [QuantityPerUnit], [UnitPrice], [Discontinued] FROM [Products]"
            UpdateCommand="UPDATE [Products] SET [Discontinued] = @Discontinued WHERE [ProductID] = @ProductID">
                <asp:Parameter Name="Discontinued" Type="Boolean" />
                <asp:Parameter Name="ProductID" Type="Int32" />

In your web.config, add a connection string as shown below:

            <add name="NorthwindConnectionString" connectionString="Data Source =(local);Integrated Security = SSPI; Initial Catalog=Northwind;"/>

Now add a <asp:TemplateField> to the GridView Column collection and add a RadioButtonList inside the ItemTemplate as shown below:


                <asp:RadioButtonList AutoPostBack="true" ID="rbl" runat="server"
                    Enabled="true" SelectedIndex='<%#Convert.ToInt32(DataBinder.Eval(Container.DataItem , "Discontinued"))%>'
                    <asp:ListItem Value="0">No</asp:ListItem>
                    <asp:ListItem Value="1">Yes</asp:ListItem>                       

As you can observe, the SelectedIndex is bound to the ‘Discontinued’ column.
We also have a ‘OnSelectedIndexChanged’ event where we will handle the database code as shown below:

    protected void rbl_SelectedIndexChanged(object sender, System.EventArgs e)
        RadioButtonList rBtnList = (RadioButtonList)sender;   
            GridViewRow gvr = (GridViewRow)rBtnList.Parent.Parent;
        if(rBtnList.SelectedValue == "1")
            SqlDataSource1.UpdateParameters[0].DefaultValue = "True";
            SqlDataSource1.UpdateParameters[0].DefaultValue = "False";
        SqlDataSource1.UpdateParameters[1].DefaultValue = gvr.Cells[0].Text;

Hope this helps

Good Luck

Categories: ASP.Net
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: