Find RadioButtonList in ASP.Net GridView SelectedIndexChanging and RowCommand event using C# and VB.Net

Last Reply 5 months ago By pandeyism

Posted 5 months ago

Dear sir, I have a grid view and I dragged one radio button list in the item template and a checkout button. While the write code in button cs file. I could not get radiobuttonlist1 in the cs file. Have anything add in the radiobuttonlist1 control or gridview.

The button is a button control this is cart checkout button when user select radiobuttonlist selected value and checkout and then insert data to database.kindly help me.

                <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" ShowFooter="True" 
                    onselectedindexchanging="GridView1_SelectedIndexChanging" Width="399px" 
                    onrowcommand="GridView1_RowCommand">
                    <Columns>
                        <asp:BoundField DataField="itemcategory" HeaderText="Category" />
                        <asp:BoundField DataField="itemname" HeaderText="Item Name" />
                        <asp:TemplateField HeaderText="Image">
                            <ItemTemplate>
                                <asp:Image ID="Image3" runat="server" Height="50px" 
                                    ImageUrl='<%# Eval("image") %>' Width="50px" />
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:BoundField DataField="price" HeaderText="Price"  />
                        <asp:TemplateField HeaderText="Size">
                            <ItemTemplate>
                                <br />
                                <asp:RadioButtonList ID="rblsize" runat="server" RepeatDirection="Horizontal">
                                    <asp:ListItem>6</asp:ListItem>
                                    <asp:ListItem>7</asp:ListItem>
                                    <asp:ListItem>8</asp:ListItem>
                                    <asp:ListItem>9</asp:ListItem>
                                    <asp:ListItem>10</asp:ListItem>
                                </asp:RadioButtonList>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Quantity">
                            <FooterTemplate>
                                <asp:Label ID="Label44" runat="server" Text="Sub Total"></asp:Label>
                            </FooterTemplate>
                            <ItemTemplate>
                                <asp:TextBox ID="txtQuantity" runat="server" Height="30px" keyup="on()"   
                                    Text='<%# Eval("obqty") %>' Width="40px"></asp:TextBox>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Total">
                            <FooterTemplate>
                                <asp:Label ID="lblGrandTotal" runat="server" Font-Bold="True" 
                                    ForeColor="#E3BAC0" keyup="on()" Text="0.0"></asp:Label>
                            </FooterTemplate>
                            <ItemTemplate>
                                <asp:Label ID="lblTotal" runat="server"></asp:Label>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:CommandField ShowSelectButton="True" />
                    </Columns>
                </asp:GridView>

 

You are viewing reply posted by: pandeyism 5 months ago.
Posted 5 months ago Modified on 5 months ago

Hi rajeesh,

Refer below sample.

HTML

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" OnSelectedIndexChanging="GridView1_SelectedIndexChanging"
    OnRowCommand="GridView1_RowCommand">
    <Columns>
        <asp:BoundField DataField="itemcategory" HeaderText="Category" />
        <asp:BoundField DataField="itemname" HeaderText="Item Name" />
        <asp:BoundField DataField="price" HeaderText="Price" />
        <asp:TemplateField HeaderText="Size">
            <ItemTemplate>
                <br />
                <asp:RadioButtonList ID="rblsize" runat="server" RepeatDirection="Horizontal">
                    <asp:ListItem>6</asp:ListItem>
                    <asp:ListItem>7</asp:ListItem>
                    <asp:ListItem>8</asp:ListItem>
                    <asp:ListItem>9</asp:ListItem>
                    <asp:ListItem>10</asp:ListItem>
                </asp:RadioButtonList>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:CommandField ShowSelectButton="True" />
    </Columns>
</asp:GridView>

Namespaces

C#

using System.Data;

VB.Net

Imports System.Data

Code

C#

protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        DataTable dt = new DataTable();
        dt.Columns.AddRange(new DataColumn[] { new DataColumn("Id", typeof(int)), new DataColumn("itemcategory", typeof(string)), new DataColumn("itemname", typeof(string)), new DataColumn("price", typeof(int)) });
        dt.Rows.Add(1, "Stationary", "Pen", 10);
        dt.Rows.Add(2, "Stationary", "Pencile", 10);
        dt.Rows.Add(3, "Stationary", "Eraser", 10);
        GridView1.DataSource = dt;
        GridView1.DataBind();
    }
}

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
    // Code for rowcommand
    int index = Convert.ToInt32(e.CommandArgument);
    GridViewRow gvRow = GridView1.Rows[index];
    RadioButtonList rblSize = gvRow.FindControl("rblsize") as RadioButtonList;
}

protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
{
    // Code for selectedindexchanging
    int index = e.NewSelectedIndex;
    RadioButtonList rblSize = GridView1.Rows[index].FindControl("rblsize") as RadioButtonList;
}

VB.Net

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    If Not Me.IsPostBack Then
        Dim dt As DataTable = New DataTable()
        dt.Columns.AddRange(New DataColumn() {New DataColumn("Id", GetType(Integer)), New DataColumn("itemcategory", GetType(String)), New DataColumn("itemname", GetType(String)), New DataColumn("price", GetType(Integer))})
        dt.Rows.Add(1, "Stationary", "Pen", 10)
        dt.Rows.Add(2, "Stationary", "Pencile", 10)
        dt.Rows.Add(3, "Stationary", "Eraser", 10)
        GridView1.DataSource = dt
        GridView1.DataBind()
    End If
End Sub

Protected Sub GridView1_RowCommand(ByVal sender As Object, ByVal e As GridViewCommandEventArgs)
    Dim index As Integer = Convert.ToInt32(e.CommandArgument)
    Dim gvRow As GridViewRow = GridView1.Rows(index)
    Dim rblSize As RadioButtonList = TryCast(gvRow.FindControl("rblsize"), RadioButtonList)
End Sub

Protected Sub GridView1_SelectedIndexChanging(ByVal sender As Object, ByVal e As GridViewSelectEventArgs)
    Dim index As Integer = e.NewSelectedIndex
    Dim rblSize As RadioButtonList = TryCast(GridView1.Rows(index).FindControl("rblsize"), RadioButtonList)
End Sub