C# 下拉列表中出现错误“;SelectedIndexChanged";事件,同时从RadComboBox中选择新项

C# 下拉列表中出现错误“;SelectedIndexChanged";事件,同时从RadComboBox中选择新项,c#,asp.net,radcombobox,C#,Asp.net,Radcombobox,在我的网页中,RadGrid外有一个RadComboBox,RadGrid内有一个下拉框 下拉列表中的数据根据RadComboBox项目选择进行绑定。 Ex:如果从RadComboBox中选择了“公司”项,则下拉列表中的数据将与“公司”相关(即公司1、公司2、公司3等) HTML代码: <telerik:RadComboBox ID="ddlCompany" runat="server" Height="200" Width="240" DropDownWidth="3

在我的网页中,RadGrid外有一个RadComboBox,RadGrid内有一个下拉框

下拉列表中的数据根据RadComboBox项目选择进行绑定。
Ex:如果从RadComboBox中选择了“公司”项,则下拉列表中的数据将与“公司”相关(即公司1、公司2、公司3等)

HTML代码:

<telerik:RadComboBox ID="ddlCompany" runat="server" Height="200" Width="240"
          DropDownWidth="310" EmptyMessage="- Select Product -" HighlightTemplatedItems="true" CausesValidation="false" Filter="StartsWith" AppendDataBoundItems="true" AllowCustomText="true" AutoPostBack="true" DataTextField="Title" DataValueField="Code" OnSelectedIndexChanged="ddlCompany_SelectedIndexChanged">
</telerik:RadComboBox>

<telerik:RadGrid ID="RGGSTAcCode" runat="server">       
    <Columns> 
         <telerik:GridEditCommandColumn></telerik:GridEditCommandColumn> 

         <telerik:GridTemplateColumn UniqueName="AccountCode" HeaderText="Account Code">
             <ItemTemplate>
                 <asp:Label ID="lblAcCode" Text='<%# Eval("AccountCode") %>' runat="server"></asp:Label>
             </ItemTemplate>
             <EditItemTemplate>
                 <asp:DropDownList ID="ddlAcCode" DataTextField="AccountDescription" DataValueField="AccountCodeID" runat="server"/> 
             </EditItemTemplate>
         </telerik:GridTemplateColumn>
     </Columns>
public DataSet GetCompanyNames()
{
    SqlConnection con = new SqlConnection(strcon);
    SqlCommand cmd = new SqlCommand("General.usp_tbl_BuyerCode_Query", con);
    cmd.CommandType = CommandType.StoredProcedure;
    SqlDataAdapter da = new SqlDataAdapter(cmd);
    DataSet ds = new DataSet();
    try
    {
        con.Open();
        da.Fill(ds);
        con.Close();
    }
    catch (Exception ex)
    {
    }
    return ds;
}

protected void BindComapnyDL()
{
    ddlCompany.DataTextField = "Title";
    ddlCompany.DataValueField = "Code";
    ddlCompany.DataSource = GetCompanyNames();
    ddlCompany.DataBind();

    Session["Comp"] = ddlCompany.SelectedValue.ToString();
}

protected void ddlCompany_SelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
{
    if (ddlCompany.SelectedItem != null)
    {
        SqlConnection con = new SqlConnection(strcon);
        SqlDataAdapter adapter = new SqlDataAdapter();

        adapter.SelectCommand = new SqlCommand("SELECT [AccountCodeID],[AccountCode]+' - '+[AccountDescription] as[AccountDescription] FROM [Sunway_AP].[General].[tbl_AccountCode] (NOLOCK) Where [CompanyCode] = '" + Session["Comp"] + "' order by [AccountCode]+' - '+[AccountDescription]", con);
        con.Open();
        DataTable dt = new DataTable();
        try
        {
            adapter.Fill(dt);
        }
        finally
        {
            con.Close();
        }

        DropDownList list = RGGSTAcCode.FindControl("ddlAcCode") as DropDownList;
        list.DataTextField = "AccountDescription";
        list.DataValueField = "AccountCodeID";
        list.DataSource = dt;
        list.DataBind();
    }
    else
    {
        Response.Write("Please select Company first");
    }
}
现在,当我尝试使用“ddlCompany\u SelectedIndexChanged”事件更改公司时

我得到以下错误:
System.NullReferenceException:对象引用未设置为对象的实例
第行:
list.DataTextField=“AccountDescription”


请指出我的代码中的错误。提前感谢

此行包含错误下拉列表未正确找到

DropDownList list=RGGSTAcCode.FindControl(“ddlAcCode”)作为DropDownList

您将下拉列表放置在任何控件中的何处

访问此链接
http://docs.telerik.com/devtools/aspnet-ajax/controls/grid/how-to/operations-with-ms-dropdownlist-in-edititemtemplate-of-gridtemplatecolumn

试试这个

 foreach (GridDataItem item in RGGSTAcCode.EditItems)
{
    DropDownList list = item.FindControl("ddlAcCode") as DropDownList;
    //Now we can do stuff with the "list"
}

请记住,行需要处于编辑模式才能在其EditItemTemplate中找到控件

如果在这一行放置断点,DropDownList list=RGGSTAcCode.FindControl(“ddlAcCode”)作为DropDownList;您将看到该列表为空。这就是错误的原因。@Thanos Markou:谢谢你的回复。是的,您是对的,我使用断点检查了它,发现列表为空。我正在RadGrid“EditItemTemplate”中使用此asp下拉列表,并尝试在RadComboBox(位于RadGrid外部)“SelectedIndexChanged”事件中查找此控件。请让我知道我应该在我的代码中更正什么,以使其按预期工作?谢谢您的答复。我正在Telerik RadGrid“EditItemTemplate”中使用此asp下拉列表,并尝试在RadComboBox(RadGrid之外的)“SelectedIndexChanged”事件中查找此控件。请让我知道我应该在我的代码中更正什么,以使其按预期工作?此外,我检查了建议的链接,但它对我的问题没有帮助。请回复谢谢你的回复。不幸的是,您的解决方案不符合我的要求。我必须在RadComboBox“SelectedIndexChanged”事件(位于Telerik RadGrid外部)上绑定asp下拉列表(位于Telerik RadGrid“EditItemTemplate”内部)。因为asp下拉列表数据基于RadComboBox项选择。请suggest@user3196511如果telerik像我假设的那样工作,edititemtemplate中的控件实际上不存在,直到该行处于编辑模式。如果是这种情况,您将在模板内控件的init事件中绑定它