0

我是 ASP.NET 的新手

我有详细信息视图的代码 Page1.aspx:

                <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 中的第二个下拉列表

请帮忙。

4

1 回答 1

0

我会尝试在这里使用 FindControl 调用,因为您有一个标签和一个组合框,它们都具有相同的名称。所以在 OnSelectedIndexChanged 做类似的事情


var control = (DropDownList) yourDetailsView.FindControl("VendorBUName");
if(control!=null)
{
   //do your stuff on the drop down
}
于 2012-10-08T02:49:10.843 回答