ASP.NET 2.0 GridView Item Template CheckBox Radio Button

A

ABHIJIT B

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;
}
}
 
J

Jon

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
 
A

ABHIJIT B

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();
}
}
}


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

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top