我在DataList中加个一个DroupDownList,然后在绑定时给DroupDownList绑定值,
我有个DroupDownList点击回传事件,这个DroupDownList在一个UpdatePanel局部更新中
但是相应事件的时候,出现未将对象引用到实例,请各位帮忙<td style="height:25px">
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<table width="100%" cellpadding="0" cellspacing="0" border="0">
<tr>
<td style="text-align:left; width:50px">
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" 
onselectedindexchanged="DropDownList1_SelectedIndexChanged">
</asp:DropDownList>
</td>
<td>
<asp:Label ID="txtPirce1" runat="server" Text='<%# GetMoney(Eval("Price1")) %>'></asp:Label>
<asp:Label ID="Label2" runat="server" Text='<%# GetMoney(Eval("Price1")) %>' Visible="false"></asp:Label>
预费:<asp:Label ID="txtFee1" runat="server" Text='<%#Eval("Fee1") %>'></asp:Label>
</td>
</tr>
</table>
</ContentTemplate>
</asp:UpdatePanel>
</td>     
<%--这个是UpdateDatepanel在DataList一列中,里边包含一个DroupDownList和Label控件--%>
//加载绑定DroupDownList,前台可以显示
protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
{
    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
    {
        Label StartCount1 = e.Item.FindControl("txtStartCount1") as Label;
        Label EndCount1 = e.Item.FindControl("txtEndCount1") as Label;
        DropDownList DropDownList4 = e.Item.FindControl("DropDownList1") as DropDownList;
        int end1 = 100;
        if (EndCount1.Text != "不限制")
        {
            end1 = int.Parse(EndCount1.Text);
        }
        int start1 = int.Parse(StartCount1.Text.ToString());
        int x = start1;
        int count1 = end1 - start1 + 1;
        for (int i = x; i < count1; i++)
        {
            string s = start1.ToString();
            DropDownList4.Items.Insert(i - x, new ListItem(s));
            start1++;
        }
        if (StartCount1.Text != "0")
        {
            DropDownList4.Items.Insert(0, new ListItem("0", "0"));
        }
    }
}
//回传DroupDownList,出现未将对象引用到实例。难道是我没取到这个控件?
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{    DropDownList DropDownList4 = DropDownList1.FindControl("DropDownList1") as DropDownList;//未将对象引用到实例
    Label txtPirce1 = DataList1.FindControl("txtPirce1") as Label;
    Label Label2 = DataList1.FindControl("Label2") as Label;
    if (DropDownList4.SelectedItem.Text == "0")
    {
        txtPirce1.Text = Label2.Text;
    }
    else
    {
        txtPirce1.Text = (int.Parse(DropDownList4.SelectedItem.Text) * double.Parse(Label2.Text)).ToString();
    }
}

解决方案 »

  1.   

    UpdatePane up= e.Item.FindControl("UpdatePanel") as UpdatePane
    DropDownList DropDownList4 = up.FindControl("DropDownList1") as DropDownList;要先找到up,再从up找到ddl
      

  2.   

    DropDownList DropDownList4 = sender as DropDownList ;即可
      

  3.   


    必须的可以,
    DropDownList DropDownList4 = sender as DropDownList 也可以
      

  4.   

    以前也遇到过这种情况,相似的。也是两个DropDownList 放在UpdatePane 里面,,第一次用的时候没问题,当显示出来,已修改就报错。
      

  5.   


            protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
            {            DropDownList DropDownList4 = (DropDownList)sender;            Response.Write(DropDownList4.Text);
            }
      

  6.   

    可是我在UpdatePanel中的控件在后台都可以直接能拿出来,这个是什么原因
      

  7.   

    DropDownList1_SelectedIndexChanged事件本身不能通过DropDownList1来获取,因为可能存在多个,他返回的应该是一个DropDownList1组,要获取其中一个,好像要这样DropDownList ddl= (DropDownList)e.Item.FindControl("DropDownList1").Controls[0];