0

我使用 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”函数后具有旧值...

帮我。

4

1 回答 1

0

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

于 2013-08-13T06:32:16.940 回答