ASP.NET C# - 根据第一个下拉列表的选择刷新第二个DropdownList

时间:2012-10-08 02:02:34

标签: asp.net

我是ASP.NET新手

我有这个代码Page1.aspx用于DetailsView:

                <InsertItemTemplate>
                    <asp:DropDownList id="VendorName" datasourceid="VendorSqlDataSource"
                        datatextfield="VendorName" DataValueField="VendorID"  
                        SelectedValue='<%# Bind("VendorID") %>' runat="server" AutoPostBack="true" />
                </InsertItemTemplate>
                <EditItemTemplate>
                    <asp:DropDownList id="VendorName" datasourceid="VendorSqlDataSource"
                    datatextfield="VendorName"  DataValueField="VendorID"  
                    SelectedValue='<%# Bind("VendorID") %>'   runat="server"  OnSelectedIndexChanged="dllVendor_OnSelectedIndexChanged"  AutoPostBack="true" />
                </EditItemTemplate>
            </asp:TemplateField>

        <asp:TemplateField HeaderText="Vendor BU">
                <ItemTemplate>
                    <asp:Label ID="VendorBUName" runat="Server" style="text-align:left; width:100%" Text='<%# Eval("VendorBUName")%>' Width="70%"/>
                </ItemTemplate>
                <InsertItemTemplate>
                    <asp:DropDownList id="VendorBUName" datasourceid="VendorBUSqlDataSource"
                        datatextfield="VendorBUName" DataValueField="VendorBUID"  
                        SelectedValue='<%# Bind("VendorBUID") %>' runat="server"/>
                </InsertItemTemplate>
                <EditItemTemplate>
                    <asp:DropDownList id="VendorBUName" datasourceid="VendorBUSqlDataSource"
                    datatextfield="VendorBUName"    DataValueField="VendorBUID"  
                    SelectedValue='<%# Bind("VendorBUID") %>'   runat="server"/>
                </EditItemTemplate>
            </asp:TemplateField>

我正在尝试使用Page1.aspx.cs后面的代码来刷新此代码中的第二个下拉列表:

(VendorBUName"), but keeps getting a error "".
    protected void OnSelectedIndexChanged(object VendorID)
    {
        string queryString = "SELECT VendorBUID, VendorBUName From MDF_VendorBU WHERE VendorID = @VendorID";
        System.Data.SqlClient.SqlConnection connection = new System.Data.SqlClient.SqlConnection("ConnectionString");
        System.Data.SqlClient.SqlCommand command = new System.Data.SqlClient.SqlCommand(queryString, connection);
        command.Parameters.AddWithValue("@VendorID", VendorID);
        connection.Open();

        DataTable dt = new DataTable();
        SqlDataAdapter ad = new SqlDataAdapter(command);
        ad.Fill(dt);

        /*
        VendorBUName.Items.Clear();
        if (dt.Rows.Count > 0)
        {

            VendorBUName.DataSource = dt;
            VendorBUName.DataTextField = "VendorBUName";
            VendorBUName.DataValueField = "VendorBUID";
            VendorBUName.DataBind();
        }
        */

        connection.Close(); 


    }

    //protected void dllVendor_OnSelectedIndexChanged(object sender, System.EventArgs e)
    //{ 
        //OnSelectedIndexChanged(VendorName.SelectedValue.ToString());
    //}

我不断得到“名称'VendorBUName'在当前上下文中不存在”,看起来后面的代码无法识别Page1.aspx中的第二个下拉列表

请帮忙。

1 个答案:

答案 0 :(得分:0)

我会尝试在这里使用FindControl,因为你有一个标签和一个同名的组合框。 因此在OnSelectedIndexChanged中执行类似

的操作

var control = (DropDownList) yourDetailsView.FindControl("VendorBUName");
if(control!=null)
{
   //do your stuff on the drop down
}