Template Columns in DataGrids

Discussion in 'ASP .Net Datagrid Control' started by JimB, Nov 6, 2003.

  1. JimB

    JimB Guest

    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
     
    JimB, Nov 6, 2003
    #1
    1. Advertising

  2. 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
     
    Giorgio Parmeggiani, Nov 9, 2003
    #2
    1. Advertising

  3. JimB

    Jim Benson Guest

    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

    *** Sent via Developersdex http://www.developersdex.com ***
    Don't just participate in USENET...get rewarded for it!
     
    Jim Benson, Nov 10, 2003
    #3
  4. JimB

    Jim Benson Guest

    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



    *** Sent via Developersdex http://www.developersdex.com ***
    Don't just participate in USENET...get rewarded for it!
     
    Jim Benson, Nov 10, 2003
    #4
  5. 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
     
    Giorgio Parmeggiani, Nov 10, 2003
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jim Bancroft
    Replies:
    3
    Views:
    1,884
    Bob Weiner
    May 2, 2005
  2. Replies:
    0
    Views:
    355
  3. Jim Bancroft

    binding ArrayLists to DataGrids-- how to name the columns?

    Jim Bancroft, Apr 30, 2005, in forum: ASP .Net Datagrid Control
    Replies:
    3
    Views:
    137
    Bob Weiner
    May 2, 2005
  4. Juergen

    Fixing columns and header in datagrids

    Juergen, Jun 24, 2004, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    137
    Juergen
    Jun 24, 2004
  5. Vinki

    template columns and bound columns in gridview

    Vinki, Apr 25, 2007, in forum: ASP .Net Web Controls
    Replies:
    2
    Views:
    891
    Vinki
    Apr 25, 2007
Loading...

Share This Page