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


Hi

try this example

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="ProductID" DataSourceID="SqlDataSource1" AllowPaging="True" AllowSorting="True">
            <Columns>                          
                <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" />
            </Columns>
        </asp:GridView>
       
               <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">
            <UpdateParameters>
                <asp:Parameter Name="Discontinued" Type="Boolean" />
                <asp:Parameter Name="ProductID" Type="Int32" />
            </UpdateParameters>
 
        </asp:SqlDataSource>

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

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

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

   

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

 
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";
        else
            SqlDataSource1.UpdateParameters[0].DefaultValue = "False";
        SqlDataSource1.UpdateParameters[1].DefaultValue = gvr.Cells[0].Text;
        SqlDataSource1.Update();
        GridView1.DataBind();       
    }

Hope this helps

Good Luck

Advertisements
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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s

%d bloggers like this: