radio button in colums are not grouped correctly

G

George Paiva

Hello,

I have a datagrid that I dynamically add radio buttons to in the
onItemDataBoud Event. I set each radio button to the same GroupName but
when I run the page the radio button group does not dissallow multiple
selections as it should. My C# code snippet is below.

Any thoughts???

public void ItemDataBoundHandler(Object sender, DataGridItemEventArgs e)
{
RadioButton rb = new RadioButton();
rb.AutoPostBack=true;
rb.GroupName="selItem";
rb.ID=e.Item.Cells[0].Text;
e.Item.Cells[2].Controls.Add(rb);
}



Thanks
Geo
 
A

Alvin Bruney

That will not work. Well i suppose you already figured this out. Add it this
way instead. can't find the code... hold on

if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType ==
ListItemType.AlternatingItem)

{

Label lbl = (Label)e.Item.FindControl("Dig");

if(lbl != null)

lbl.Text = "<input type=radio name='samegroup' value=" +
e.Item.Cells[1].Text + ">";

[snip]

to retrieve the selected radio button do this:

private string GetSelectedItems()

{

string retval = Request.Form["samegroup"];

if(retval == null || String.Empty == retval)

Page.Controls.Add(new LiteralControl("<script>alert('Please select a row to
dig on by enabling a radio button in the grid')</script>"));

else

retval = retval.Trim();


return retval;

}

The returned value from that function will be whatever you put in in the
value attribute of the input tag (e.Item.Cells[1].Text)

You can sorta tell that this is my current project right?
 
G

George Paiva

Thanks Alvin,

I find it interesting that the DataGrid renames the "name" property of the
RadioButton with some prepended text (like gridname:_ctl2:) because this
nullifies the functionality of the HTML control. You would think that the
framework would use its own property to itentify the controls in the grid.
No?

I will use your solution to get around this issue so thanks again.

Geo



Alvin Bruney said:
That will not work. Well i suppose you already figured this out. Add it this
way instead. can't find the code... hold on

if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType ==
ListItemType.AlternatingItem)

{

Label lbl = (Label)e.Item.FindControl("Dig");

if(lbl != null)

lbl.Text = "<input type=radio name='samegroup' value=" +
e.Item.Cells[1].Text + ">";

[snip]

to retrieve the selected radio button do this:

private string GetSelectedItems()

{

string retval = Request.Form["samegroup"];

if(retval == null || String.Empty == retval)

Page.Controls.Add(new LiteralControl("<script>alert('Please select a row to
dig on by enabling a radio button in the grid')</script>"));

else

retval = retval.Trim();


return retval;

}

The returned value from that function will be whatever you put in in the
value attribute of the input tag (e.Item.Cells[1].Text)

You can sorta tell that this is my current project right?

--
Regards,
Alvin Bruney
Got tidbits? Get it here...
http://tinyurl.com/3he3b
George Paiva said:
Hello,

I have a datagrid that I dynamically add radio buttons to in the
onItemDataBoud Event. I set each radio button to the same GroupName but
when I run the page the radio button group does not dissallow multiple
selections as it should. My C# code snippet is below.

Any thoughts???

public void ItemDataBoundHandler(Object sender, DataGridItemEventArgs e)
{
RadioButton rb = new RadioButton();
rb.AutoPostBack=true;
rb.GroupName="selItem";
rb.ID=e.Item.Cells[0].Text;
e.Item.Cells[2].Controls.Add(rb);
}



Thanks
Geo
 
S

Saravana [MVP]

If you check out this support link , you will find the answer why datagrid
is changing the name of the radio button in runtime.
http://support.microsoft.com/default.aspx?scid=kb;EN-US;Q316495



--
Saravana
Microsoft MVP - ASP.NET
www.extremeexperts.com



George Paiva said:
Thanks Alvin,

I find it interesting that the DataGrid renames the "name" property of the
RadioButton with some prepended text (like gridname:_ctl2:) because this
nullifies the functionality of the HTML control. You would think that the
framework would use its own property to itentify the controls in the grid.
No?

I will use your solution to get around this issue so thanks again.

Geo



Alvin Bruney said:
That will not work. Well i suppose you already figured this out. Add it this
way instead. can't find the code... hold on

if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType ==
ListItemType.AlternatingItem)

{

Label lbl = (Label)e.Item.FindControl("Dig");

if(lbl != null)

lbl.Text = "<input type=radio name='samegroup' value=" +
e.Item.Cells[1].Text + ">";

[snip]

to retrieve the selected radio button do this:

private string GetSelectedItems()

{

string retval = Request.Form["samegroup"];

if(retval == null || String.Empty == retval)

Page.Controls.Add(new LiteralControl("<script>alert('Please select a row to
dig on by enabling a radio button in the grid')</script>"));

else

retval = retval.Trim();


return retval;

}

The returned value from that function will be whatever you put in in the
value attribute of the input tag (e.Item.Cells[1].Text)

You can sorta tell that this is my current project right?

--
Regards,
Alvin Bruney
Got tidbits? Get it here...
http://tinyurl.com/3he3b
George Paiva said:
Hello,

I have a datagrid that I dynamically add radio buttons to in the
onItemDataBoud Event. I set each radio button to the same GroupName but
when I run the page the radio button group does not dissallow multiple
selections as it should. My C# code snippet is below.

Any thoughts???

public void ItemDataBoundHandler(Object sender, DataGridItemEventArgs e)
{
RadioButton rb = new RadioButton();
rb.AutoPostBack=true;
rb.GroupName="selItem";
rb.ID=e.Item.Cells[0].Text;
e.Item.Cells[2].Controls.Add(rb);
}



Thanks
Geo
 
A

Alvin Bruney

I find it interesting that the DataGrid renames the "name" property of the
RadioButton with some prepended text (like gridname:_ctl2:) because this
this is because webcontrols which implement inamingcontainer interface -
like the datagrid - must meet the criteria that their html name be unique,
hence the renaming. i'm not sure if they are working on getting an exemption
for the datagrid because this is a sore spot

--
Regards,
Alvin Bruney
Got tidbits? Get it here...
http://tinyurl.com/3he3b
George Paiva said:
Thanks Alvin,

I find it interesting that the DataGrid renames the "name" property of the
RadioButton with some prepended text (like gridname:_ctl2:) because this
nullifies the functionality of the HTML control. You would think that the
framework would use its own property to itentify the controls in the grid.
No?

I will use your solution to get around this issue so thanks again.

Geo



Alvin Bruney said:
That will not work. Well i suppose you already figured this out. Add it this
way instead. can't find the code... hold on

if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType ==
ListItemType.AlternatingItem)

{

Label lbl = (Label)e.Item.FindControl("Dig");

if(lbl != null)

lbl.Text = "<input type=radio name='samegroup' value=" +
e.Item.Cells[1].Text + ">";

[snip]

to retrieve the selected radio button do this:

private string GetSelectedItems()

{

string retval = Request.Form["samegroup"];

if(retval == null || String.Empty == retval)

Page.Controls.Add(new LiteralControl("<script>alert('Please select a row to
dig on by enabling a radio button in the grid')</script>"));

else

retval = retval.Trim();


return retval;

}

The returned value from that function will be whatever you put in in the
value attribute of the input tag (e.Item.Cells[1].Text)

You can sorta tell that this is my current project right?

--
Regards,
Alvin Bruney
Got tidbits? Get it here...
http://tinyurl.com/3he3b
George Paiva said:
Hello,

I have a datagrid that I dynamically add radio buttons to in the
onItemDataBoud Event. I set each radio button to the same GroupName but
when I run the page the radio button group does not dissallow multiple
selections as it should. My C# code snippet is below.

Any thoughts???

public void ItemDataBoundHandler(Object sender, DataGridItemEventArgs e)
{
RadioButton rb = new RadioButton();
rb.AutoPostBack=true;
rb.GroupName="selItem";
rb.ID=e.Item.Cells[0].Text;
e.Item.Cells[2].Controls.Add(rb);
}



Thanks
Geo
 

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top