Template Columns in DataGrids

J

JimB

Hello All,

This is my problem. I have a datagrid with some columns
the first being and ID column, the next column is text, a
question, the third is a template column(ItemTemplate) This is an unbound
column.
The template is a radiobuttonlist with 3 values, 1)Yes, 2)
No, 3)N/A.
This is what I need to accomplish.
1.) Collect the value of the radiobutton selected for each
row in the grid along with it's corresponding ID(column 1).
2.) If the useser selects "Yes" in the template column,
pop_up another window with more questions and collect
answers to then and their ID value.

The problem is that I can't get an event to fire when the
radio button is clicked. I tried the "ItemCommand"
eventbut that only works for buttons, linked buttons and
hyperlinks. Is there a way to do what I want to do?
Please help!!!!! Thanks...... Jim
 
G

Giorgio Parmeggiani

Hi Jim

[CUT]
The template is a radiobuttonlist with 3 values, 1)Yes, 2)
No, 3)N/A.
[CUT]

The problem is that I can't get an event to fire when the
radio button is clicked. I tried the "ItemCommand"
eventbut that only works for buttons, linked buttons and
hyperlinks. Is there a way to do what I want to do?

I think you can use the ItemCreated event and follow this these footsteps:

- Find the RadioButtonList
- Add the value of your table key (id) in one attribute of RadioButtonList
- Create an event for SelectedIndexChanged

Here a little example (I suppose that your template column is in the first
position therefore Cells[0]):

protected void DataGrid_ItemCreated(object sender,
System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType ==
ListItemType.AlternatingItem)
{
if(e.Item.Cells[0].Controls[1] is RadioButtonList)
{
DataGrid dg = sender as DataGrid;
RadioButtonList radioList = e.Item.Cells[4].Controls[1] as
RadioButtonList;
radioList.AutoPostBack = true;
radioList.Attributes.Add("Key",
dg.DataKeys[e.Item.ItemIndex].ToString());
radioList.SelectedIndexChanged +=new
EventHandler(radioList_SelectedIndexChanged);
}
}
}

Ciao
Giorgio
 
J

Jim Benson

Thanks I will have to try that. I am using VB so I just have to
translate your code and see what happens. Thanks again I will let you
know if it works
 
J

Jim Benson

I understand what you are trying to do in your code, however, the
problem is that since the object(radiobuttonlist) is in the template
column, you cannot access it directly. Ex in your code:

radiolist.autopostback = TRUE


You cannot reference this object directly

Any Ideas?

Thanks
 
G

Giorgio Parmeggiani

Hi
I understand what you are trying to do in your code, however, the
problem is that since the object(radiobuttonlist) is in the template
column, you cannot access it directly. Ex in your code:

radiolist.autopostback = TRUE


You cannot reference this object directly

My example was wrong, i think that you can
to reference the radiobutton list with this piece of code (if the column is
in the first position):

[C#]
RadioButtonList radioList = e.Item.Cells[0].Controls[1] as RadioButtonList;

[VB.NET]
Dim radioList as RadioButtonList
radioList = CType(e.Item.Cells(0).Controls(1), RadioButtonList)

Ciao
Giorgio
 

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,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top