繁体   English   中英

如何在使用sqldatasource进行绑定时为dropdownlist设置初始值

[英]How to set initial value for dropdownlist while using sqldatasource as binding

我有2个dropdownlist ,它们绑定到SQLDataSource并且2nd dropdownlist值基于1st dropdownlist 问题是默认情况下dropdownlist显示的是实际值。 我在dpsem中放置了初始值,因此它可以正常工作。但是当我在dpsubj dropdownlist执行相同操作时,它也会显示以前的值。

如果我选择dpsem--4,则dpsubj应为A,B,C;如果我选择dpsem--6,则dpsubj应为D,E,F。 但它显示的是A,B,C,D,E,F ...

我知道这是与自动回发相关的问题...

     <asp:DropDownList ID="dpsem" runat="server" Height="28px" 
                   Width="99px" AutoPostBack="True" DataSourceID="selectsemester" 
                   DataTextField="sem_no" DataValueField="sem_no" AppendDataBoundItems="true">
                  <asp:ListItem Selected="True" Value="0">-select</asp:ListItem>

            </asp:DropDownList>

     <asp:DropDownList ID="dpsubj" runat="server" Height="28px" 
                   Width="99px" AutoPostBack="True" DataSourceID="Selectscode" 
                   DataTextField="scode" DataValueField="scode" AppendDataBoundItems="true">
                  <asp:ListItem Selected="True" Value="0">-select</asp:ListItem>

            </asp:DropDownList>

 <asp:SqlDataSource ID="Selectscode" runat="server" 
        ConnectionString="<%$ ConnectionStrings:AttendenceManagementConnectionString %>" 
        SelectCommand="SELECT DISTINCT [scode] FROM [Semester_Wise_Subject] WHERE (([branch_name] = @branch_name) AND ([sem_no] = @sem_no))">
        <SelectParameters>
            <asp:ControlParameter ControlID="lbdept" DefaultValue="IT" Name="branch_name" 
                PropertyName="Text" Type="String" />
            <asp:ControlParameter ControlID="dpsem" DefaultValue="6" Name="sem_no" 
                PropertyName="SelectedValue" Type="Int32" />
        </SelectParameters>
    </asp:SqlDataSource>



    <asp:SqlDataSource ID="selectsemester" runat="server" 
        ConnectionString="<%$ ConnectionStrings:AttendenceManagementConnectionString %>" 
        SelectCommand="SELECT DISTINCT [sem_no] FROM [Batch_year]">
    </asp:SqlDataSource>

您应该使用dpsubj.Items.Clear() ..然后再次将过滤后的数据加载到selectedindexchanged ,它将首先删除dpsubj下拉列表中的所有先前记录,然后使用代码将值加载到dpsubj中

我相信您必须在第一个下拉列表上创建一个SelectedIndexChanged事件,并让该事件在第二个下拉列表上引起数据绑定。 同样,如果您不希望第二个下拉菜单显示数据,则默认情况下也摆脱默认参数值。

如果您正在使用ajax做任何事情,您还需要确保第一个下拉列表也会更新脚本管理器中的第二个下拉列表。

设置AppendDataBoundItems="false"dpsubj

由于将此属性设置为true,因此在数据绑定之前不会清除项目。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM