Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / General / February 2008

Tip: Looking for answers? Try searching our database.

ASP.NET 2.0 GridView Item Template CheckBox Radio Button

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
ABHIJIT B - 04 Feb 2008 19:20 GMT
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;
       }
   }
Jon - 04 Feb 2008 20:51 GMT
> Hi,
>
[quoted text clipped - 72 lines]
>         }
>     }

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
ABHIJIT B - 05 Feb 2008 19:00 GMT
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 4 Feb, 19:20,ABHIJITB<abhijitbavdhan...@gmail.com> wrote:
>
[quoted text clipped - 91 lines]
>
> - Show quoted text -

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.