如何在vb.net中的edititemtemplate中预先选择复选框列表项

时间:2018-06-21 20:38:54

标签: vb.net checkboxlist formview edititemtemplate

我在formview项目模板中有一个复选框。在编辑模式下,我想选择数据表中的项目。我找不到该怎么做。我正在使用VB.NET。

ASP.NET代码:

<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

我在县区遇到错误:

  

对象引用未设置为对象的实例。

当我调试它时,县复选框为空。为什么在edititemtemplate中找不到复选框列表?

1 个答案:

答案 0 :(得分:0)

如果看不到您当前拥有的代码,引导您会有些困难,但是,如果您发布代码,我将非常乐意为您提供帮助。

您将需要遍历后面的代码中的for循环并将所有内容设置为选中。

For each checkBoxItem as listItem in [NameOfCheckBox].Items
   checkBoxItem.selected = true
next