ASP.NET 2.0 GridView Item Template CheckBox Radio Button

Discussion in 'ASP .Net' started by ABHIJIT B, Feb 4, 2008.

  1. ABHIJIT B

    ABHIJIT B Guest

    Hi,

    I am using GridView control in one of my ASPNET 2.0 web page.
    The problem I am facing is GridView should allow only single row
    selection only.
    Aslo I want to know How I can do sam eusing Radio/Option button.

    Thanks in Advance

    <asp:GridView ID="gvSearchUsersList" runat="server" TabIndex="5"
    PageSize="5" AllowPaging="True" AutoGenerateColumns="False"
    OnPageIndexChanging="gvSearchUsersList_PageIndexChanging"
    AllowSorting="True" GridLines="None" Width="717px"
    ForeColor="#333333">
    <Columns>
    <asp:TemplateField
    HeaderText="Select">

    <ItemTemplate>
    <asp:CheckBox ID="chkSelect"
    AutoPostBack="true" runat="server"
    OnCheckedChanged="chkSelect_CheckedChanged" />
    </ItemTemplate>
    </asp:TemplateField>
    <asp:BoundField DataField="LoginName"
    HeaderText="Login Name" />
    <asp:BoundField DataField="FirstName"
    HeaderText="First Name" />
    <asp:BoundField DataField="LastName"
    HeaderText="Last Name" />
    </Columns>
    <PagerSettings
    Mode="NextPreviousFirstLast" />
    <RowStyle CssClass="row-1"
    HorizontalAlign="Center" />
    <SelectedRowStyle
    BackColor="InactiveCaptionText" ForeColor="Black" BorderStyle="Solid"
    BorderWidth="1px" HorizontalAlign="Center" VerticalAlign="Middle" />
    <PagerStyle BackColor="#2461BF"
    ForeColor="White" HorizontalAlign="Center" VerticalAlign="Middle"/>
    <HeaderStyle BackColor="#507CD1" Font-
    Bold="True" ForeColor="Black" />
    <AlternatingRowStyle CssClass="row-2"
    BackColor="LightGray" />
    </asp:GridView>
    ---------------------------------------------------
    From .cs file I am binding rows to GridView as given below,

    gvSearchUsersList.DataSource = dtSearchUserList;
    gvSearchUsersList.DataBind();
    dcSearchSucess = true;

    I am getting selected row value as given below,

    protected void chkSelect_CheckedChanged(object sender, EventArgs e)
    {
    try
    {
    CheckBox checkbox = (CheckBox)sender;
    GridViewRow row = (GridViewRow)checkbox.NamingContainer;

    if (checkbox.Checked == true)
    {
    Session["dcSelectedLoginID"] = null;
    Session["dcSelectedLoginID"] =
    row.Cells[1].Text.Trim().ToString();
    hidLoginID.Value =
    row.Cells[1].Text.Trim().ToString();
    }
    }
    catch (Exception ex)
    {
    TraceSUError.Log("Exception occurred : " + ex.Message);
    return;
    }
    }
     
    ABHIJIT B, Feb 4, 2008
    #1
    1. Advertising

  2. ABHIJIT B

    Jon Guest

    On 4 Feb, 19:20, ABHIJIT B <> wrote:
    > Hi,
    >
    > I am using GridView control in one of my ASPNET 2.0 web page.
    > The problem I am facing is GridView should allow only single row
    > selection only.
    > Aslo I want to know How I can do sam eusing Radio/Option button.
    >
    > Thanks in Advance
    >
    > <asp:GridView ID="gvSearchUsersList" runat="server" TabIndex="5"
    > PageSize="5" AllowPaging="True" AutoGenerateColumns="False"
    > OnPageIndexChanging="gvSearchUsersList_PageIndexChanging"
    > AllowSorting="True" GridLines="None" Width="717px"
    > ForeColor="#333333">
    >                             <Columns>
    >                                 <asp:TemplateField
    > HeaderText="Select">
    >
    > <ItemTemplate>
    >                                         <asp:CheckBox ID="chkSelect"
    > AutoPostBack="true" runat="server"
    > OnCheckedChanged="chkSelect_CheckedChanged" />
    >                                     </ItemTemplate>
    >                                 </asp:TemplateField>
    >                                 <asp:BoundField DataField="LoginName"
    > HeaderText="Login Name" />
    >                                 <asp:BoundField DataField="FirstName"
    > HeaderText="First Name" />
    >                                 <asp:BoundField DataField="LastName"
    > HeaderText="Last Name" />
    >                             </Columns>
    >                                 <PagerSettings
    > Mode="NextPreviousFirstLast" />
    >                                 <RowStyle CssClass="row-1"
    > HorizontalAlign="Center" />
    >                                 <SelectedRowStyle
    > BackColor="InactiveCaptionText" ForeColor="Black" BorderStyle="Solid"
    > BorderWidth="1px" HorizontalAlign="Center" VerticalAlign="Middle" />
    >                                 <PagerStyle BackColor="#2461BF"
    > ForeColor="White" HorizontalAlign="Center" VerticalAlign="Middle"/>
    >                                 <HeaderStyle BackColor="#507CD1" Font-
    > Bold="True" ForeColor="Black" />
    >                                 <AlternatingRowStyle CssClass="row-2"
    > BackColor="LightGray" />
    >                         </asp:GridView>
    > ---------------------------------------------------
    > From .cs file I am binding rows to GridView as given below,
    >
    > gvSearchUsersList.DataSource = dtSearchUserList;
    >                     gvSearchUsersList.DataBind();
    >                     dcSearchSucess = true;
    >
    > I am getting selected row value as given below,
    >
    > protected void chkSelect_CheckedChanged(object sender, EventArgs e)
    >     {
    >         try
    >         {
    >             CheckBox checkbox = (CheckBox)sender;
    >             GridViewRow row = (GridViewRow)checkbox.NamingContainer;
    >
    >             if (checkbox.Checked == true)
    >             {
    >                 Session["dcSelectedLoginID"] = null;
    >                 Session["dcSelectedLoginID"] =
    > row.Cells[1].Text.Trim().ToString();
    >                 hidLoginID.Value =
    > row.Cells[1].Text.Trim().ToString();
    >             }
    >         }
    >         catch (Exception ex)
    >         {
    >             TraceSUError.Log("Exception occurred : " + ex.Message);
    >             return;
    >         }
    >     }


    Hello mate,

    Basically, either by Serverside or Client side, you need to loop
    through each row and cell and set the option to false.

    Serverside this would be something like the below

    Get instance to GridView
    foreach row get the column that has the radio button
    if this RadioButton.Checked == true, then RadioButton.Checked = false

    HTH,

    Jon
    www.nantwichonline.com
     
    Jon, Feb 4, 2008
    #2
    1. Advertising

  3. ABHIJIT B

    ABHIJIT B Guest

    Thanks John for your reply.

    I am using DatagRid and following code.Smimilarly I want for GridView
    control.
    Like in Datagrid we have Items collection to loop through each row of
    datagrid.
    Iw ant to know which collection is available for GridView in which I
    can use FindControl property.

    public void SelectOnlyOne( object sender, EventArgs e)
    {
    string m_ClientID = "";
    RadioButton rb = new RadioButton();

    rb = (RadioButton)(sender);
    m_ClientID = rb.ClientID;

    foreach (DataGridItem i in grdReport.Items)
    {
    rb = (RadioButton)(i.FindControl("rdbBatchTime"));
    rb.Checked = false;

    if (m_ClientID == rb.ClientID)
    {
    rb.Checked = true;
    }

    //Get selected value

    if (rb.Checked == true)
    {
    if (i.Cells[1].Text.Trim().ToString() == "&nbsp;")
    hidLoginID.Value = "";
    else
    hidLoginID.Value =
    i.Cells[1].Text.Trim().ToString();

    if (i.Cells[2].Text.Trim().ToString() == "&nbsp;")
    hidFirstName.Value = "";
    else
    hidFirstName.Value =
    i.Cells[2].Text.Trim().ToString();

    if (i.Cells[3].Text.Trim().ToString() == "&nbsp;")
    hidLastName.Value = "";
    else
    hidLastName.Value =
    i.Cells[3].Text.Trim().ToString();
    }
    }
    }


    On Feb 4, 3:51 pm, Jon <> wrote:
    > On 4 Feb, 19:20,ABHIJITB<> wrote:
    >
    >
    >
    >
    >
    > > Hi,

    >
    > > I am using GridView control in one of my ASPNET 2.0 web page.
    > > The problem I am facing is GridView should allow only single row
    > > selection only.
    > > Aslo I want to know How I can do sam eusing Radio/Option button.

    >
    > > Thanks in Advance

    >
    > > <asp:GridView ID="gvSearchUsersList" runat="server" TabIndex="5"
    > > PageSize="5" AllowPaging="True" AutoGenerateColumns="False"
    > > OnPageIndexChanging="gvSearchUsersList_PageIndexChanging"
    > > AllowSorting="True" GridLines="None" Width="717px"
    > > ForeColor="#333333">
    > >                             <Columns>
    > >                                 <asp:TemplateField
    > > HeaderText="Select">

    >
    > > <ItemTemplate>
    > >                                         <asp:CheckBox ID="chkSelect"
    > > AutoPostBack="true" runat="server"
    > > OnCheckedChanged="chkSelect_CheckedChanged" />
    > >                                     </ItemTemplate>
    > >                                 </asp:TemplateField>
    > >                                 <asp:BoundField DataField="LoginName"
    > > HeaderText="Login Name" />
    > >                                 <asp:BoundField DataField="FirstName"
    > > HeaderText="First Name" />
    > >                                 <asp:BoundField DataField="LastName"
    > > HeaderText="Last Name" />
    > >                             </Columns>
    > >                                 <PagerSettings
    > > Mode="NextPreviousFirstLast" />
    > >                                 <RowStyle CssClass="row-1"
    > > HorizontalAlign="Center" />
    > >                                 <SelectedRowStyle
    > > BackColor="InactiveCaptionText" ForeColor="Black" BorderStyle="Solid"
    > > BorderWidth="1px" HorizontalAlign="Center" VerticalAlign="Middle" />
    > >                                 <PagerStyle BackColor="#2461BF"
    > > ForeColor="White" HorizontalAlign="Center" VerticalAlign="Middle"/>
    > >                                 <HeaderStyle BackColor="#507CD1" Font-
    > > Bold="True" ForeColor="Black" />
    > >                                 <AlternatingRowStyle CssClass="row-2"
    > > BackColor="LightGray" />
    > >                         </asp:GridView>
    > > ---------------------------------------------------
    > > From .cs file I am binding rows to GridView as given below,

    >
    > > gvSearchUsersList.DataSource = dtSearchUserList;
    > >                     gvSearchUsersList.DataBind();
    > >                     dcSearchSucess = true;

    >
    > > I am getting selected row value as given below,

    >
    > > protected void chkSelect_CheckedChanged(object sender, EventArgs e)
    > >     {
    > >         try
    > >         {
    > >             CheckBox checkbox = (CheckBox)sender;
    > >             GridViewRow row = (GridViewRow)checkbox.NamingContainer;

    >
    > >             if (checkbox.Checked == true)
    > >             {
    > >                 Session["dcSelectedLoginID"] = null;
    > >                 Session["dcSelectedLoginID"] =
    > > row.Cells[1].Text.Trim().ToString();
    > >                 hidLoginID.Value =
    > > row.Cells[1].Text.Trim().ToString();
    > >             }
    > >         }
    > >         catch (Exception ex)
    > >         {
    > >             TraceSUError.Log("Exception occurred : " + ex.Message);
    > >             return;
    > >         }
    > >     }

    >
    > Hello mate,
    >
    > Basically, either by Serverside or Client side, you need to loop
    > through each row and cell and set the option to false.
    >
    > Serverside this would be something like the below
    >
    > Get instance to GridView
    > foreach row get the column that has the radio button
    > if this RadioButton.Checked ==  true, then RadioButton.Checked = false
    >
    > HTH,
    >
    > Jonwww.nantwichonline.com- Hide quoted text -
    >
    > - Show quoted text -
     
    ABHIJIT B, Feb 5, 2008
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John Criswell
    Replies:
    1
    Views:
    1,417
    John Criswell
    Jul 24, 2003
  2. Replies:
    3
    Views:
    715
    Eliyahu Goldin
    Feb 2, 2005
  3. =?Utf-8?B?U3VyZXNo?=

    radio button List item in Datagrid

    =?Utf-8?B?U3VyZXNo?=, Feb 4, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    467
    =?Utf-8?B?U3VyZXNo?=
    Feb 4, 2005
  4. Scott D Johnson
    Replies:
    1
    Views:
    1,131
    Scott D Johnson
    Sep 27, 2006
  5. Hiten
    Replies:
    1
    Views:
    702
    Vidar Petursson
    May 26, 2004
Loading...

Share This Page