本文介绍了在数据列表中查找控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试如下所述查找控件n数据列表时

When I tried To Find Control n data List As I Mentioned Below

 Error(Object reference not set to an instance of an object.

我不知道

protected void dlCategory_ItemDataBound(object sender, DataListItemEventArgs e)
    {
        Label Lb = (Label)e.Item.FindControl("LblCat");
        Lb.ForeColor = System.Drawing.Color.Red;
    }



<Datalist>
 <asp:DataList ID="dlSubCategory" runat="server" 
                                    DataSource='<%# GetSubCategory(Convert.ToString(Eval("Category_ID")))%>' 
                                    onitemcreated="dlSubCategory_ItemCreated" 
                                    onitemdatabound="dlSubCategory_ItemDataBound">
                                    <EditItemStyle ForeColor="#CC3300" />
                                    <SelectedItemStyle ForeColor="#CC3300" />
                                    <ItemTemplate>
                                        <div class="buttn_div_sub">
                                            <div class="lm40 tm2 buttn_txt">
                                                <a href='<%# Convert.ToString(Eval("ProductCategory_Id")).Insert(0,"ListView.aspx?ProductCategory_Id=") %>'
                                                    class="buttn_txt">
                                                    <asp:Label ID="Label1" runat="server" Text='<%#DataBinder.Eval(Container.DataItem,"Name") %>'></asp:Label>
                                                </a>
                                            </div>
                                        </div>
                                    </ItemTemplate>
                                </asp:DataList>
                            </ItemTemplate>

推荐答案

protected void dlCategory_ItemDataBound(object sender, DataListItemEventArgs e) 
{
    Label Label1 = e.Item.FindControl("Label1") as Label;
    if (LblCat != null)
    {
        string id = ((System.Data.DataRowView)e.Item.DataItem).Row["ProductCategory_Id"].ToString();

        if (Request.QueryString["ProductCategory_Id"] == id)
        {
            Label1.ForeColor = System.Drawing.Color.Red;
        }
    }
}

这篇关于在数据列表中查找控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-26 19:49