Solved

Datagrid Template Column Radio Button (get access to the rb control)

Posted on 2004-04-29
11
35,387 Views
Last Modified: 2011-08-18
Hi.  I have a radio button in a template column of a datagrid.  All I need to know is how to get access to this to be able to set the radio button as checked or unchecked (depending on the results of a query).  

I'm trying this:  

  Dim rbactionneeded As RadioButton = CType(e.Item.FindControl("rbactionneeded"), RadioButton)

                                If ansval = "Y" Then    'result from stored procedure
                                    rbactionneeded.Checked = True
                                  End if

(very simplified).  I'm getting an error "object reference not set to an instance of an object" right when it hits rbactionneeded.checked=true.  I tried add the word "new" when declaring, but didn't work.  I'm certain the rb in my grid is called rbactionnneeded because I use this to insert into the database.

This code is in the itemdatabound for the given datagrid.

Any suggestions?  Am I doing this incorrectly?  I am awarding 500 points because it's very urgent!  Thanks!
0
Comment
Question by:numten
  • 6
  • 4
11 Comments
 
LVL 28

Expert Comment

by:mmarinov
ID: 10953864
can you post the html code for the datagrid
you can try before that

CType(e.Item.Cells(index_of_radio_button_column).FindControl("rbactionneeded"), RadioButton)

B..M
0
 

Author Comment

by:numten
ID: 10953895
Here is the html for the datagrid.  It's pretty complex, I use 5 template columns- I really really need help!  :)  Thanks so much!

<asp:datagrid id="dgviewiereports" runat="server" Width="698px" BackColor="White" Visible="False"
                                                                              AutoGenerateColumns="False" GridLines="Vertical" CellPadding="3" BorderColor="#999999" BorderStyle="None"
                                                                              BorderWidth="1px">
                                                                              <SelectedItemStyle Font-Bold="True" ForeColor="White" BackColor="#008A8C"></SelectedItemStyle>
                                                                              <AlternatingItemStyle BackColor="Gainsboro"></AlternatingItemStyle>
                                                                              <ItemStyle ForeColor="Black" BackColor="#EEEEEE"></ItemStyle>
                                                                              <HeaderStyle Font-Bold="True" ForeColor="White" BackColor="#000084"></HeaderStyle>
                                                                              <FooterStyle ForeColor="Black" BackColor="#CCCCCC"></FooterStyle>
                                                                              <Columns>
                                                                                    <asp:BoundColumn DataField="appln_nm" HeaderText="Application Name"></asp:BoundColumn>
                                                                                    <asp:BoundColumn DataField="appln_id" HeaderText="Application ID">
                                                                                          <HeaderStyle Width="200px"></HeaderStyle>
                                                                                    </asp:BoundColumn>
                                                                                    <asp:TemplateColumn HeaderText="Action Needed">
                                                                                          <ItemTemplate>
                                                                                                <asp:RadioButton id="rbactionneeded" runat="server"></asp:RadioButton>
                                                                                          </ItemTemplate>
                                                                                          <EditItemTemplate>
                                                                                                <asp:TextBox id="TextBox1" runat="server"></asp:TextBox>
                                                                                          </EditItemTemplate>
                                                                                    </asp:TemplateColumn>
                                                                                    <asp:TemplateColumn HeaderText="In Progress">
                                                                                          <ItemTemplate>
                                                                                                <asp:RadioButton id="rbin_progress" runat="server" GroupName="IE"></asp:RadioButton>
                                                                                          </ItemTemplate>
                                                                                    </asp:TemplateColumn>
                                                                                    <asp:TemplateColumn HeaderText="On Hold">
                                                                                          <ItemTemplate>
                                                                                                <asp:RadioButton id="rbon_hold" runat="server" GroupName="IE"></asp:RadioButton>
                                                                                          </ItemTemplate>
                                                                                    </asp:TemplateColumn>
                                                                                    <asp:TemplateColumn HeaderText="Complete">
                                                                                          <ItemTemplate>
                                                                                                <asp:RadioButton id="rbcomplete" runat="server" GroupName="IE"></asp:RadioButton>
                                                                                          </ItemTemplate>
                                                                                    </asp:TemplateColumn>
                                                                                    <asp:TemplateColumn HeaderText="Comments">
                                                                                          <ItemTemplate>
                                                                                                <P>
                                                                                                      <asp:Label id="lblcomments" runat="server">Comments:</asp:Label>
                                                                                                      <asp:DataGrid id="dgcomments" runat="server" Visible="False" BackColor="White" BorderWidth="1px"
                                                                                                            BorderStyle="None" BorderColor="#999999" CellPadding="3" GridLines="Vertical">
                                                                                                            <SelectedItemStyle Font-Bold="True" ForeColor="White" BackColor="#008A8C"></SelectedItemStyle>
                                                                                                            <AlternatingItemStyle BackColor="Gainsboro"></AlternatingItemStyle>
                                                                                                            <ItemStyle ForeColor="Black" BackColor="#EEEEEE"></ItemStyle>
                                                                                                            <HeaderStyle Font-Bold="True" ForeColor="White" BackColor="#000084"></HeaderStyle>
                                                                                                            <FooterStyle ForeColor="Black" BackColor="#CCCCCC"></FooterStyle>
                                                                                                            <PagerStyle HorizontalAlign="Center" ForeColor="Black" BackColor="#999999" Mode="NumericPages"></PagerStyle>
                                                                                                      </asp:DataGrid>
                                                                                                      <asp:Button id="btnaddcomments" runat="server" CssClass="btn" Text="Add Comments..." Visible="False"
                                                                                                            CommandName="Open Comments Panel"></asp:Button>
                                                                                                      <asp:Panel id="pnladdcomments" runat="server">
                                                                                                            <P>Comments:&nbsp;
                                                                                                                  <asp:TextBox id="txtcomments" runat="server" Height="100px" Width="200px" TextMode="MultiLine"></asp:TextBox></P>
                                                                                                            <P></P>
                                                                                                            <P>Submitter:&nbsp;
                                                                                                                  <uc1:JJEDSLookup id="jlcommentssubmitter" runat="server"></uc1:JJEDSLookup></P>
                                                                                                            <P>
                                                                                                      </asp:Panel></P>
                                                                                          </ItemTemplate>
                                                                                    </asp:TemplateColumn>
                                                                        </Columns>
                                                                              <PagerStyle HorizontalAlign="Center" ForeColor="Black" BackColor="#999999" Mode="NumericPages"></PagerStyle>
                                                                        </asp:datagrid>
0
 

Author Comment

by:numten
ID: 10953935
Nope.  didn't work.  :(   Still, Object not set to an instance of the object.
0
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 28

Expert Comment

by:mmarinov
ID: 10953979
where do you try to access his control, because it is in the EditItemTemplate and it is accessible only when you are in edit mode
another think : if you can debug the application check how many and which are the controls in e.Item.Cells(2)

B..M
0
 

Author Comment

by:numten
ID: 10955468
I don't really understand what edititemtemplate is.  I never used templates with controls before.  How do I make it accessible in edit mode in edititemtemplate????? (I have no clue what that is).  

When I debug, there is one control in the e.item.cells(2) and it is the radio button (rbactionneeded).  (If you help me through this I will award double the points to you for all your help!).  

If this helps, here is the vb code (the full vb code) that I'm using.  I was hesitant to post it because it could be confusing.  Here's an explanation of what I'm trying to do:   (please don't get scared away by my code!  )

************************************************************

I have a datagrid that has 4 radio buttons per row in template columns.  Each radio button is a seperate question (rb1=question1, rb2=quest2) etc... I am successfully able to submit the answers of each row  and each row's radio button questions into the database.  Now the problem is, I need to populate the datagrid and then using the datagrid results I also need to populate the answer of the radio buttons.  The results that populate the datagrid and radio buttons are two seperate stored procedures:

SP1 for datagrid is retrieve all applications:
So it returns:
Appln_id and Appln Name
1   App 1
2  App 2

SP2 uses the given rows Appln_ID and then populates the answers (using that appln_id)   into the RB controls:
1  App 1    RB1=Checked            RB2=Not Checked....
2  App 2   RB1= Not Checked      RB2= Not Checked .....

Right now, I first populate the datagrid with the appln_id and appln names.  Once the datagrid is filled with that information I go back and do a loop through each row, passing in the rows appln_id to help populate the rb answers (each rb is a different question id so I have to loop through each rb too).  I cannot seem to get the rb's to work.  I keep getting the error "object reference not set to an instance of an object".  I ran the sp and I am certain that the values I"m passing in return an answer (to eliminate all problems).

    Private Sub dgviewiereports_itemdatabound(ByVal sender As System.Object, ByVal e As DataGridItemEventArgs) Handles dgviewiereports.ItemDataBound

****STORED PROCEDURE CALL TO GET DATAGRID POPULATED **********************
        Dim cmdG_GET_IMPCG_EVNT As OracleCommand = DBHelper.CreateSPCommand(xxxxx.GET_DATAGRID_FILLED")
        Dim pimpcg_evnt_nm_in As New OracleParameter("impcg_evnt_nm_in", OracleDbType.Varchar2, CAP.decode(ddlviewIEReports), ParameterDirection.Input)
        Dim pCUR_impcg_evnt_info As New OracleParameter("CUR_impcg_evnt_info", OracleDbType.RefCursor, DBNull.Value, ParameterDirection.Output)
        cmdG_GET_IMPCG_EVNT.Parameters.Add(pimpcg_evnt_nm_in)
        cmdG_GET_IMPCG_EVNT.Parameters.Add(pCUR_impcg_evnt_info)

        Dim dsFillDatagrid As New DataSet
        DBHelper.FillDataSet(dsFillDatagrid, cmdG_GET_IMPCG_EVNT)
        dgviewiereports.DataSource = dsFillDatagrid
        dgviewiereports.databind()

'**************START OF LOOP TO FIND OUT THE NUMBER OF ROWS IN THE DATAGRID ******************
        Dim i As Integer
        For i = 0 To dsFillDatagrid.Tables(0).Rows.Count   'count number rows in DG
            If dsFillDatagrid.Tables(0).Rows.Count > 0 Then          'if there are results... continue
               
'************* START AT ROW 1, NOW LOOP THROUGH EACH QUESTION TO GET THE ANSWERS TO EACH RADIO BUTTON SINCE EACH ARE A DIFFERENT QUESTION ID IN THE DB. ***********
                Dim i2 As Integer
                For i2 = 103 To 106   'questionid for radio buttons. RB1=question id 103 in DB, rb2=question id 102 etc...

'**************STORED PROCEDURE CALL TO GET EACH RADIO BUTTON ANSWER ************
                    Dim cmdg_impcg_evnt_appln_quest As OracleCommand = DBHelper.CreateSPCommand("XXX.get_RADIO_BUTTON_POPULATED")
                    Dim pimpcg_evnt_nm As New OracleParameter("impcg_evnt_nm_in", OracleDbType.Varchar2, ddlviewIEReports.SelectedValue, ParameterDirection.Input)
                    Dim pappln_id As New OracleParameter("appln_id_in", OracleDbType.Int16, 1005, ParameterDirection.Input)
                    Dim pappln_portfl_quest_id As New OracleParameter("appln_portfl_quest_id_in", OracleDbType.Int16, i2, ParameterDirection.Input)
                    Dim presults As New OracleParameter("CUR_impcg_evnt_info", OracleDbType.RefCursor, DBNull.Value, ParameterDirection.Output)

                    cmdg_impcg_evnt_appln_quest.Parameters.Add(pimpcg_evnt_nm)
                    cmdg_impcg_evnt_appln_quest.Parameters.Add(pappln_id)
                    cmdg_impcg_evnt_appln_quest.Parameters.Add(pappln_portfl_quest_id)
                    cmdg_impcg_evnt_appln_quest.Parameters.Add(presults)
                    DBHelper.RunSP(cmdg_impcg_evnt_appln_quest)

                    Dim dsRBbind As New DataSet
                    DBHelper.FillDataSet(dsRBbind, cmdg_impcg_evnt_appln_quest)
                    Dim dr As DataRow
                    Dim i3 As Integer

'****iF THE GIVEN ROW, GIVEN RADIO BUTTON HAS AN ANSWER THAT BIND IT TO THE RADIO BUTTON -----  THIS IS WHERE THE PROBLEM OCCURS!!!!!!!!!! ***************************

                    If dsRBbind.Tables(0).Rows.Count > 0 Then
                        For i3 = 0 To dsRBbind.Tables(0).Rows.Count - 1
                            dr = dsRBbind.Tables(0).Rows(i3)
                            Dim ansval As String = dr.Item("ans_val").ToString

                            If i2 = 103 Then
                                Dim rbactionneeded As RadioButton = CType(e.Item.FindControl("rbactionneeded"), RadioButton)
                                If ansval = "Y" Then
                                    rbactionneeded.Checked = True  '******this is where it shoots the error, object reference not set to instance of object **********
                                ElseIf dr("ans_val") = "N" Then
                                    rbactionneeded.Checked = False
                                Else
                                    rbactionneeded.Checked = False
                                End If
                            ElseIf i2 = 104 Then

                            ElseIf i2 = 105 Then

                            ElseIf i2 = 106 Then

                            End If
                        Next
                    End If

                    DBHelper.DisposeSPCommand(cmdg_impcg_evnt_appln_quest)
                Next
            End If
        Next
    End Sub
'*****************************************
0
 
LVL 28

Accepted Solution

by:
mmarinov earned 500 total points
ID: 10956785
ok here is the problem

in datagrid's event ItemDataBound you have 4 different types of the items ( Header, Item, ALternatingItem and Footer )
so your error is because the first Item that the .net meets is Header and there is no radibutton in it and
Dim rbactionneeded As RadioButton = CType(e.Item.FindControl("rbactionneeded"), RadioButton)
will return nothing
so the scenario is this

If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItem.AlternatingItem THen
'Here you do your code
End If

B>.M
0
 

Author Comment

by:numten
ID: 10959369
Why can't everyone answer as quick and as precise as you!  I truly appreciate your assistance!  This worked perfectly!  Would you be able to post your answer in another post I opened so you can get an extra 500 pts?  The forumn is located here:  http://www.experts-exchange.com/Programming/Programming_Languages/Dot_Net/Q_20972622.html#10956814

Thanks so so so so so so much!  You saved so much time!
0
 

Author Comment

by:numten
ID: 10961549
Sorry about posting in 2 forums.  

I had the above code working before, but now when I debug it hits that line of code if e.itemtype = list.........  and it evaluates it all the time as not matching either criteria and it skips right to the end if.  Any idea why, I didn't change any code except adding that line.

Also, when you say add that line, where exactly should I be adding it- the first line of the itemdatabound subroutine?  

Thanks again!
0
 
LVL 28

Expert Comment

by:mmarinov
ID: 10964058
so the problem is that you actually changed something
e.itemtype doesn't exist
it MYST be
e.Item.ItemType

the structure should be

 Private Sub dgviewiereports_itemdatabound(ByVal sender As System.Object, ByVal e As DataGridItemEventArgs) Handles dgviewiereports.ItemDataBound

If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItem.AlternatingItem THen
'Here you do your code
End If

end sub

B..M
0
 

Author Comment

by:numten
ID: 10964097
I rebooted and now it works (for some reason).  Thanks again!
0
 
LVL 4

Expert Comment

by:jppampin
ID: 11140108
I'm agree with mmarinov. Che the ItemType before the object asociation.

0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question