ASP.NET 2.0 DataGrid Radio Button Single Selection

A

ABHIJIT B

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

S. Justin Gengo

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.

--
Sincerely,

S. Justin Gengo, MCP

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



ABHIJIT B said:
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
 

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,754
Messages
2,569,528
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top