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 DataGrid Radio Button Single Selection

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
ABHIJIT B - 08 Feb 2008 20:35 GMT
Hi,

I am using datagrid in my PopUp window page.It has radio button for
selecting row.
I want to allow single selection at a time.I am able to solve problem
using belowcode.
Currently I am facing is I am allowing Paging for datagrid.

Now I am disabling Paging and allow scrolling.If I enable scrolling
and user selects 25 row.The PopUp window focus is not on 25 row it
shows first row.In this case row 25 is selected.The Popup window goes
up.

My problem is focus should be on selected row.Is this possible in
JavScript.

Kindly help me.

ASPX code:

<asp:datagrid id="dgADUserList" runat="server" Width="720px"
BackColor="White" AutoGenerateColumns="False" CellPadding="4"
Height="22px" BorderWidth="1px"
                               BorderStyle="None"
BorderColor="#CC9966" CssClass="listItems"> <%-- PageSize="5"
AllowPaging="true"> OnPageIndexChanged="dgADUserList_PageIndexChanged"
>--%>
                               <Columns>
                                   <asp:TemplateColumn
HeaderText="Select">
                                   <ItemTemplate>
                                       <asp:RadioButton
id="rdbSelect" runat="server" AutoPostBack="True"
OnCheckedChanged="SelectOnlyOne"></asp:RadioButton>
                                   </ItemTemplate>
                                   </asp:TemplateColumn>
                                   <asp:BoundColumn
DataField="LoginName" SortExpression="LoginName"
HeaderText="LoginName"></asp:BoundColumn>
                                   <asp:BoundColumn
DataField="FirstName" SortExpression="FirstName"
HeaderText="FirstName"></asp:BoundColumn>
                                   <asp:BoundColumn
DataField="LastName" SortExpression="LastName" HeaderText="LastName"></
asp:BoundColumn>
                               </Columns>
                               <ItemStyle CssClass="row-1"
HorizontalAlign="Center" BorderColor="Black" ForeColor="Black" />
                               <SelectedItemStyle
BackColor="LightSkyBlue" ForeColor="Black" BorderStyle="Solid"
BorderWidth="1px" HorizontalAlign="Center" VerticalAlign="Middle" />
                               <PagerStyle BackColor="#2461BF"
ForeColor="White" HorizontalAlign="Center" VerticalAlign="Middle"/>
                               <HeaderStyle BackColor="ButtonFace"
Font-Bold="True" ForeColor="Black" BorderColor="Black"
HorizontalAlign="Center" />
                               <AlternatingItemStyle CssClass="row-2"
BackColor="LightGray" />
                       </asp:datagrid>

.CS code:

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 dgADUserList.Items)
       {
           rb = (RadioButton)(i.FindControl("rdbSelect"));
           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();
           }
       }
   }

Regards,
Abhijit B
S. Justin Gengo - 10 Feb 2008 13:43 GMT
Abhijit,

There are a few ways to do what you need here. The first and easiest way
would be to turn on maintaining scroll position of the window right in .net
itself. In asp.net 1.0 there was a way of maintaining your page position
called: SmartNavigation, but it was very buggy. In .net 2.0 it has been
replaced with a javascript method for doing the same thing and you can turn
it on via the: "MaintainScrollPositionOnPostback" property of the page. I
would suggest trying that first.

If that doesn't work for some reason, it may be because your gridview is
actually contained inside another page element such as a scrolling div. If
that's the case then I have a javascript that will scroll a page to any
control even if that control is inside a div (you have to specify the name
of the div). It's available for free as part of my free component library
here: http://www.aboutfortunate.com/Component-Library.aspx

However, my code requires that you input the control to scroll to and that
might be difficult for a specific grid view row. So the final but hardest
method would be for you to always set the scroll position into a hidden
input field yourself via javascript. Here's an article on how to do that:
http://aspnet.4guysfromrolla.com/articles/111704-1.aspx

I hope the first method (the one built into .net) works for you it will be
the easiest.

Signature

Sincerely,

S. Justin Gengo, MCP

Free code and component libraries at:
http://www.aboutfortunate.com

> Hi,
>
[quoted text clipped - 103 lines]
> Regards,
> Abhijit B

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.