Reputation: 5962
in my web application , there is datalist
, which is inside another datalist
.
i want to access that datalist
. for this purpose i written this code
DataList dl = (DataList)dlPro_Details.FindControl("dlFeatures");
but when i try to run the application , then it throw Object reference not set to an instance of an object.
then set the debug the point to it , the i found my object
dl is null
.
i have also tried to use ItemDataBound
protected void dlPro_Details_ItemDataBound(object sender, DataListItemEventArgs e)
{
int id = 8;
string getFeatures = "";
con = new SqlConnection(str);
con.Open();
SqlCommand cmd2 = new SqlCommand("select * from Products where Pro_id=" + id + "", con);
SqlDataReader dr = cmd2.ExecuteReader();
while (dr.Read())
{
getFeatures = dr.GetValue(11).ToString();
}
string se = ",";
List<string> l1 = new List<string>();
string[] featurs = getFeatures.Split(se.ToCharArray());
for (int i = 0; i < featurs.Length; i++)
{
l1.Add(featurs[i]);
}
DataList dl = (DataList)dlPro_Details.FindControl("dlFeatures");
dl.DataSource = l1;
dl.DataBind();
}
dlPro_Details is my parent datalist
this is my complete code
<asp:DataList ID="dlPro_Details" runat="server" OnItemDataBound="dlPro_Details_ItemDataBound">
<ItemTemplate>
<table>
<tr>
<td>
<div class="pro_img">
<asp:Image ID="proImg" runat="server" Width="230" Height="300" ImageUrl='<%#Eval("Image") %>' />
</div>
<div class="rating">
give rate<br />hello
</div>
</td>
<td style="vertical-align:top">
<div class="Pro_name">
<asp:Label ID="lbl" runat="server" Text='<%#Eval("Name") %>' Font-Bold="True"></asp:Label>
</div>
<div class="otherInfo">
<table style="width:100%;padding:8px 12px 0 12px;">
<tr>
<td>
<asp:Label ID="lblWarenty" runat="server" Text='<%#"Warranty : "+Eval("Warranty") %>'></asp:Label>
</td>
<td>
<asp:Label ID="lblBrand" runat="server" Text='<%#"By : "+Eval("Brand") %>'></asp:Label>
</td>
<td style="float:right">
Rating
</td>
</tr>
</table>
</div>
<div class="features">
<div class="feat_Head">
<asp:Label ID="lblFeatHead" runat="server" Text="Features"></asp:Label>
</div>
<div class="feat_containt">
<asp:DataList ID="dlFeatures" runat="server" RepeatColumns="5" RepeatDirection="Vertical">
<ItemTemplate>
<asp:Label ID="lblFeatures" runat="server" Text="Label"></asp:Label>
</ItemTemplate>
</asp:DataList>
</div>
</div>
</td>
</tr>
</table>
</ItemTemplate>
</asp:DataList>
Upvotes: 0
Views: 785
Reputation: 11985
You will find the reference to the current item using one of the arguments, and from there you can access any nested control:
protected void dlPro_Details_ItemDataBound(object sender, DataListItemEventArgs e)
{
if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
var innerDL = e.Item.FindControl("dlFeatures") as DataList;
if(innerDL != null)
{
//do something
}
}
}
Upvotes: 2