在asp.net c#中刷新页面后,下拉列表处于活动状态

本文关键字:下拉列表 活动状态 net asp 刷新 | 更新日期: 2023-09-27 18:27:22

im使用2个下拉列表,每个下拉列表都有一个sqldatasource。我正在通过代码文件设置sqldatasource默认值

我的代码是

<tr>
        <td>
            Vehicle Type
        </td>
        <td>:</td>
        <td>
            <asp:Label ID="lblVehicle" runat="server" Text="" Visible="false"></asp:Label>
            <asp:DropDownList ID="ddVehicleType" Visible="true" runat="server" CssClass="drop" AutoPostBack="True" AppendDataBoundItems="True" 
                DataSourceID="SqlDataSource2" DataTextField="VehicleName" DataValueField="VehicleTransId" OnSelectedIndexChanged="ddVehicleType_SelectedIndexChanged">
                <asp:ListItem Value="0">-Select-</asp:ListItem>
            </asp:DropDownList>
            <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:TAXIConnectionString %>" 
                SelectCommand="SELECT [VehicleName], [VehicleTransId] FROM [VehicleTransaction]">
            </asp:SqlDataSource>
    </tr>
     <tr>
        <td>
            Vehicle Number
        </td>
        <td>:</td>
        <td>
            <asp:DropDownList ID="ddVehicleNumber" runat="server" CssClass="drop" AutoPostBack="false" AppendDataBoundItems="true" DataSourceID="SqlDataSource3" DataTextField="VehicleNumber" DataValueField="VehicleId">
                <asp:ListItem Value="0">-Select-</asp:ListItem>
            </asp:DropDownList>
            <asp:SqlDataSource ID="SqlDataSource3" runat="server" ConnectionString="<%$ ConnectionStrings:TAXIConnectionString %>"
                SelectCommand="SELECT [VehicleNumber], [VehicleId] FROM [VehicleMaster] as Master join VehicleTransaction as Trans 
                on Master.VehicleType=Trans.VehicleTransId
                WHERE ( Trans.VehicleTransId = @Status and [Status] = @Status1) ">
                <SelectParameters>
                    <asp:Parameter  Name="Status" Type="String" />
                    <asp:Parameter DefaultValue="Available" Name="Status1" Type="String" />
                </SelectParameters>
            </asp:SqlDataSource>
        </td>
    </tr>

.cs文件是,

in page_load(){  
if (!Page.IsPostBack)
                    {
                        SqlDataSource3.SelectParameters["Status"].DefaultValue = reader["VehicleId"].ToString();
                        }
 protected void ddVehicleType_SelectedIndexChanged(object sender, EventArgs e)
    {
        string vehiclename = ddVehicleType.SelectedItem.Value;
        SqlConnection con = new SqlConnection(System.Web.Configuration.WebConfigurationManager.ConnectionStrings["TAXIConnectionString"].ConnectionString);
        con.Open();
        string str = "select VehicleTransId from VehicleTransaction where VehicleTransId = '" + vehiclename + "'";
        SqlCommand command = new SqlCommand(str, con);
        SqlDataReader reader = command.ExecuteReader();
        while (reader.Read())
        {
            SqlDataSource3.SelectParameters["Status"].DefaultValue = reader["VehicleTransId"].ToString();
            //ddVehicleNumber.SelectedValue = reader["VehicleTransId"].ToString();
        }
    }

但下拉列表在调用"ddVehicleType_SelectedIndexChanged"函数后有旧值。。。

帮帮我。

在asp.net c#中刷新页面后,下拉列表处于活动状态

设置SelectParameters 后调用ddVehicleNumber.DataBind();