-1

I have a checkboxlist in a formview itemtemplate. In edit mode I want to select the items that are in a datatable. I cannot find how to do this. I am using VB.NET.

ASP.NET code:

<asp:FormView ID="rdf" runat="server" Width="100%" DataSourceID="sqldatasource1" OnItemCreated="rdf_ModeChanging" rendermode="Lightweight" onrowupdating="updatebutton_click" DataKeyNames="applicationid" >

        <ItemTemplate>
            <fieldset style="width: 97%">
                <div>
                    <br />
                    <asp:Label runat="server" ID="FiscalOrgNameLabel2" Text="Fiscal Organization:   " Style="font-weight: bold; font-size: 15px;"></asp:Label><asp:Label Text='<%# Eval("FiscalOrgName") %>' runat="server" ID="FiscalOrgNameLabel1" />
                </div>

                <br />
           <asp:panel id="cntyPanel" runat="server" >
                     <asp:label ID="countyLabel"  runat="server" style="font-weight: bold; font-size: 15px; "
                    text="County(s): (if applicable)">
               <asp:DataList ID="dlCounty" runat="server" DataSourceID="sqlCounty" Style="font-weight: normal;" >
                   <ItemTemplate>
                       <li>
                           <%# Eval("county") %>
                       </li>
                   </ItemTemplate>
               </asp:DataList>

                         </asp:label>
               </asp:panel>

               <asp:panel id="IHEPanel"  runat="server" >
                   <asp:label ID="instLabel"  runat="server" style="font-weight:bold; font-size: 15px; "
                       text="Institution(s): (if applicable)">
                       <asp:datalist ID="dlIHE" runat="server" DataSourceID="sqlIHE" style="font-weight:normal;">
                           <ItemTemplate>
                               <li >
                                   <%# Eval("Institute") %>

                               </li>
                           </ItemTemplate>
                       </asp:datalist>
                   </asp:label>
               </asp:panel>
                    <asp:SqlDataSource ID="sqlCounty" runat="server" ConnectionString='<%$ ConnectionStrings:COSIGrantsDB %>' SelectCommand="SELECT dbo.County.County as county, dbo.Application_County.ApplicationId, dbo.Application_County.CountyId
              FROM dbo.County INNER JOIN
              dbo.Application_County ON dbo.County.CountyID = dbo.Application_County.CountyId where applicationid=@appid">
                        <SelectParameters>
                            <asp:QueryStringParameter QueryStringField="appid" Name="appid"></asp:QueryStringParameter>
                        </SelectParameters>
                    </asp:SqlDataSource>
                    <asp:SqlDataSource runat="server" ID="sqlGetBehalf" ConnectionString='<%$ ConnectionStrings:COSIGrantsDB %>' SelectCommand="getBehalf" SelectCommandType="StoredProcedure">
                        <SelectParameters>
                            <asp:QueryStringParameter QueryStringField="appid" Name="appid" Type="Int32"></asp:QueryStringParameter>
                            <asp:QueryStringParameter QueryStringField="apptypeid" Name="apptypeid" Type="Int32"></asp:QueryStringParameter>
                        </SelectParameters>
                    </asp:SqlDataSource>

                <asp:SqlDataSource runat="server" ID="sqlIHE"  ConnectionString='<%$ ConnectionStrings:COSIGrantsDB %>' selectcommand="SELECT dbo.Application_IHE.ApplicationID, dbo.IHE.Institute
FROM dbo.Application_IHE INNER JOIN
              dbo.IHE ON dbo.Application_IHE.IHE = dbo.IHE.iheID where applicationid = @appid" SelectCommandType="text">
                    <SelectParameters>
                         <asp:QueryStringParameter QueryStringField="appid" Name="appid" Type="Int32"></asp:QueryStringParameter>
                    </SelectParameters>
                </asp:SqlDataSource> 

                <div>

                    <div class="rdfCommandButtons">
                        <hr class="rdfHr" />

                        <telerik:RadButton runat="server" ButtonType="SkinnedButton" RenderMode="Lightweight" CommandName="Edit" Text="Edit" ID="EditButton" ToolTip="Edit" CausesValidation="False" />

                    </div>
            </fieldset>
        </ItemTemplate>



        <EditItemTemplate>
               <fieldset style="width: 97%"> <br />
                   <asp:Label ID="editApptypeId" runat="server" Text='<%# Eval("applicationtype") %>'></asp:Label>
                  <div style="display:table; width:100%">
                 <div style="display:table-row;">
                     <div style="display:table-cell;">
                        Fiscal Organizaton:
                    </div>
                    <div class="cell textcell">
                        <asp:TextBox ID="fiscalOrg" runat="server" Text='<%# Bind("FiscalOrgName") %>'></asp:TextBox>
                        </div>
                     </div>
                      </div>
                           <div id="cntyPanel" visible="true" runat="server">
                     <div runat="server" style="font-weight: bold; font-size: 15px;">
                    County(s):
                        <asp:CheckBoxList ID="ckCounty" runat="server" RepeatColumns="8" DataTextField="county" DataValueField="countyid"  ></asp:CheckBoxList>

                         <br />
                         </div>

               <div id="IHEPanel" visible="false" runat="server">
                   <div runat="server" style="font-weight:bold; font-size: 15px;">
                       Institution(s):
                       <asp:datalist ID="dlIHE" runat="server" DataSourceID="sqlIHE" style="font-weight:normal;">
                           <ItemTemplate>
                               <li >
                                   <%# Eval("Institute") %>

                                   </li>
                           </ItemTemplate>
                       </asp:datalist>
                   </div>
               </div>

                    <asp:SqlDataSource runat="server" ID="sqlGetBehalf" ConnectionString='<%$ ConnectionStrings:COSIGrantsDB %>' SelectCommand="getBehalf" SelectCommandType="StoredProcedure">
                        <SelectParameters>
                            <asp:QueryStringParameter QueryStringField="appid" Name="appid" Type="Int32"></asp:QueryStringParameter>
                            <asp:QueryStringParameter QueryStringField="apptypeid" Name="apptypeid" Type="Int32"></asp:QueryStringParameter>
                        </SelectParameters>
                    </asp:SqlDataSource>
                </div>
                <asp:SqlDataSource runat="server" ID="sqlIHE"  ConnectionString='<%$ ConnectionStrings:COSIGrantsDB %>' selectcommand="SELECT dbo.Application_IHE.ApplicationID, dbo.IHE.Institute
FROM dbo.Application_IHE INNER JOIN
              dbo.IHE ON dbo.Application_IHE.IHE = dbo.IHE.iheID where applicationid = @appid" SelectCommandType="text">
                    <SelectParameters>
                         <asp:QueryStringParameter QueryStringField="appid" Name="appid" Type="Int32"></asp:QueryStringParameter>
                    </SelectParameters>
                </asp:SqlDataSource> 

        </EditItemTemplate>

        <EmptyDataTemplate>
            <div>
                <div class="rdfEmpty">There are no items to be displayed.</div>
            </div>
        </EmptyDataTemplate>


    </asp:FormView>

VB.NET:

Protected Sub rdf_itemcommand(sender As Object, e As FormViewCommandEventArgs) Handles rdf.ItemCommand
    Dim county As CheckBoxList = DirectCast(rdf.FindControl("ckCounty"), CheckBoxList)
    conn.Open()
    Using cmd As New SqlCommand("select * from application_County where applicationid =" + Request.QueryString("appid"), conn)
        Using rdr = cmd.ExecuteReader()
            If rdr.HasRows Then
                Do While rdr.Read
                    For Each item As ListItem In county.Items
                        If item.Value = rdr.GetValue(2) Then
                            item.Selected = True
                        End If

                    Next
                Loop
            End If
        End Using
    End Using

End Sub

I get an error on county:

Object reference not set to an instance of an object.

When I debug it, county checkboxlist is empty. Why can't I find the checkboxlist in the edititemtemplate?

Andrew Morton
  • 24,203
  • 9
  • 60
  • 84
  • 1) You *really* need to use an SQL parameter for `cmd` to avoid [SQL injection attacks](https://www.owasp.org/index.php/SQL_Injection). 2) Where in the code is `county` set? It needs to happen before `rdf_itemcommand` is called. 3) You're only using one column from the database in the VB.NET code so you should explicitly select only that column's name. – Andrew Morton Jun 26 '18 at 10:13
  • Thanks. I will use the SQL Parameter. 2) The county is the checkboxlist that is set in ASP.net from sqldatasource. – Robin Gentry Jun 26 '18 at 16:15
  • What everyone writing code for ASP.NET needs to know: [ASP.NET Page Life Cycle Overview](https://msdn.microsoft.com/en-us/library/ms178472(v=vs.100).aspx). Then you can see if code needs to be adjusted to make sure that each thing is available when it's needed. – Andrew Morton Jun 27 '18 at 08:23
  • Thank you. I didnt know this information. I have read it but I dont understand how to 'put the data source control for the ListBox control in the same template item as the ListBox control itself, and do not set the data binding properties of the ListBox declaratively. Instead, set them programmatically at run time during the RowDataBound event, so that the ListBox control does not bind to its data until the CompanyID information is available.' Can you show an example? – Robin Gentry Jun 27 '18 at 15:54
  • Andrew, I am researching how to do this btw, but not having luck figuring out. – Robin Gentry Jun 27 '18 at 16:46
  • I *think* it means that in `Sub rdf_ModeChanging` you need to do `dlCounty.DataSourceID = "sqlCounty"` and take the `DataSourceID="sqlCounty"` out of the ` – Andrew Morton Jun 27 '18 at 18:24
  • 1
    I think you are correct. I actually did figure it out. I set the checkboxlist in programatically and was able to find the control and set it to the items in the database. THanks. – Robin Gentry Jun 27 '18 at 20:37

1 Answers1

0

Without seeing the code you currently have it is a little difficult to guide you, however if you post the code I would be more than happy to help.

You are going to need a for loop in the code behind the goes through and sets everything to checked.

For each checkBoxItem as listItem in [NameOfCheckBox].Items
   checkBoxItem.selected = true
next
CarleyDiaz
  • 35
  • 5