Wrox Programmer Forums
|
ASP.NET 1.x and 2.0 Application Design Application design with ASP.NET 1.0, 1.1, and 2.0.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 1.x and 2.0 Application Design section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 6th, 2003, 12:22 PM
Friend of Wrox
 
Join Date: Nov 2003
Posts: 1,348
Thanks: 0
Thanked 5 Times in 5 Posts
Default Template Columns in DataGrids

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
 
Old November 7th, 2003, 11:36 AM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

How are you binding the datagrid? Are you only binding it on the initial hit to the page? If not, then your events most likely won't fire.

Peter
------------------------------------------------------
Work smarter, not harder.
 
Old November 7th, 2003, 11:49 AM
Friend of Wrox
 
Join Date: Nov 2003
Posts: 1,348
Thanks: 0
Thanked 5 Times in 5 Posts
Default

I post on page_load and if they choose another radio button outside of the datagrid. The problem seems to be that you cannot access an object placed in a Template column unless you select the row or edit it. However I do not want to do either.

 
Old November 7th, 2003, 01:13 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

What do you mean by "post on page_load"? Do you mean you bind on page_load? If that's what you mean, remember that page_load runs every time you hit the page. So you need to make the binding conditional based on "Not IsPostback".

If Not IsPostback Then
    'do data bind here
End If

This will prevent the binding for any postback page hit.

Peter
------------------------------------------------------
Work smarter, not harder.
 
Old November 7th, 2003, 01:19 PM
Friend of Wrox
 
Join Date: Nov 2003
Posts: 1,348
Thanks: 0
Thanked 5 Times in 5 Posts
Default

Yes I meant bind on page load

but you are missing the whole problem

the question is.... How do I get a vaule out of a template column?
 
Old November 7th, 2003, 01:34 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Here's what I use to access a control in a template column...

datagriditem.Cells(i).Controls(0)

where i is the template column index

Alternatively, I beleive this will work:

datagriditem.Cells(i).FindControl("optMyRadioButto n")

You'll need to convert the object to the appropriate type so you can get at the necessary properties:

objMyRadioButton = CType(datagriditem.Cells(i).FindControl("optMyRadi oButton"), RadioButton)

With regards to the original post and getting an event to fire, you need to set up the handler for the radio button. You still need to check that you aren't binding every time you hit the page, cause that will negate any events that *would* happen when the controls are redrawn.

Peter
------------------------------------------------------
Work smarter, not harder.
 
Old November 7th, 2003, 01:48 PM
Friend of Wrox
 
Join Date: Nov 2003
Posts: 1,348
Thanks: 0
Thanked 5 Times in 5 Posts
Default

I can drill down to the control .. dg1.control(0) .. but you cannot get the value .. there is just now way I can see

Also I have tried using the findcontrol("").. but it never seems to find the control
dg1.findcontrol(rblAnswers) .. always returns NOTHING ...

Your suggestion dg1.control().value will work if the row is in edit mode... but not when just viewing .. to me this is very strange... try it and you will see what I mean.

thanks
 
Old November 7th, 2003, 04:26 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Are you doing a postback on each radio button click so that you can pop up the additional questions page on each "Yes" answer?

Peter
------------------------------------------------------
Work smarter, not harder.
 
Old November 7th, 2003, 04:34 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Just figured this out...

<asp:templatecolumn>
    <itemtemplate>
        <asp:radiobuttonlist OnSelectedIndexChanged="checkBoxHandler" autopostback="True" runat="server">
            <asp:listitem value="1">No</asp:listitem>
            <asp:listitem value="2">Yes</asp:listitem>
            <asp:listitem value="3">N/A</asp:listitem>
        </asp:radiobuttonlist>
    </itemtemplate>
</asp:templatecolumn>

Public Sub checkBoxHandler(ByVal sender As Object, ByVal e As System.EventArgs)
    Dim objRadioList As RadioButtonList = CType(sender, RadioButtonList)
    Dim objDataGridItem As DataGridItem = CType(objRadioList.Parent.Parent, DataGridItem)
End Sub

You can reference the control's ancestors so you can eventually work your way back to the datagriditem that the radio button lives in. Then you can do what you need with the datagriditem.

Peter
------------------------------------------------------
Work smarter, not harder.
 
Old November 7th, 2003, 05:12 PM
Friend of Wrox
 
Join Date: Nov 2003
Posts: 1,348
Thanks: 0
Thanked 5 Times in 5 Posts
Default

I will have to look at your code in more depth to fully understand it, but if it works GREAT!!!!!! Thank you forall of your time spent on this... I will try this out shortly and let you know how this goes





Similar Threads
Thread Thread Starter Forum Replies Last Post
Filtering Template columns in Detailsview shashipandey ASP.NET 2.0 Professional 0 September 20th, 2007 04:58 AM
getting values from template columns itemtemplate Laxmikant_it ASP.NET 1.0 and 1.1 Professional 4 February 6th, 2007 04:19 PM
Need to get value of gridview template columns for kmh030 ASP.NET 2.0 Professional 1 January 6th, 2007 11:10 PM
Template Columns in a Datagrid Greg Griffiths ASP.NET 1.0 and 1.1 Basics 1 October 28th, 2004 08:02 AM
Template Columns in DataGrids jbenson001 ASP.NET 1.x and 2.0 Application Design 3 March 12th, 2004 11:01 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.