Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
i have one datalist control call DataList1 and another datalist control call DataList3 inside DataList1. Now i have one image inside datalist3 for which i have to set visibility at runtime.
i have written the following code on PageLoad:

C#
//search for records in DataList3
       if (((DataList)(DataList1.Items[0].FindControl("DataList3"))).Items.Count <= 0)
       {
           //if no records found than display Image20 instead of DataList3
           ((DataList)(DataList1.Items[0].FindControl("DataList3"))).Visible = false;
           ((System.Web.UI.WebControls.Image)(DataList1.Items[0].FindControl("Image40"))).Visible = true;
       }
       else
       {
           //if records found than display DtaList3 instead of Image20
           ((DataList)(DataList1.Items[0].FindControl("DataList3"))).Visible = true;
           ((System.Web.UI.WebControls.Image)(DataList1.Items[0].FindControl("Image40"))).Visible = false;
       }


but i don't find any required results. both image and datalist are displaying in both case.
any guidance will be helpful.


my whole pageload

C#
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Data.Sql;
using System.IO;

public partial class free_user_ReceivedInterestPending : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        

        //search for records in DataList3
        if (((DataList)(DataList1.Items[0].FindControl("DataList3"))).Items.Count <= 0)
        {
            //if no records found than display Image20 instead of DataList3
            ((DataList)(DataList1.Items[0].FindControl("DataList3"))).Visible = false;
            ((System.Web.UI.WebControls.Image)(DataList1.Items[0].FindControl("Image40"))).Visible = true;
        }
        else
        {
            //if records found than display DtaList3 instead of Image20
            ((DataList)(DataList1.Items[0].FindControl("DataList3"))).Visible = true;
            ((System.Web.UI.WebControls.Image)(DataList1.Items[0].FindControl("Image40"))).Visible = false;
        }
        
        
        
    }
    
}



datalist1 binding

C#
<asp:DataList ID="DataList1" runat="server" DataKeyField="sr" 
                    DataSourceID="SqlDataSource9" Width="100%" onload="DataList1_Load">
                    <itemtemplate>
                        <table class="style472">
                            style="border: medium solid #669999; font-family: Verdana; font-size: 10pt; height: 281px;" 
                            width="100%">
                            <tr>
                                <td bgcolor="#669999" class="style697" colspan="4" style="vertical-align: top">
                                    
                                    <table class="style472">
                                        <tr>
                                            <td class="style793">
                                                 </td>
                                            <td class="style790">
                                                <asp:Label ID="UserFromLabel" runat="server" style="font-weight: 700" 
                                                    Text='<%# Eval("UserFrom") %>' />
                                            </td>
                                            <td class="style797">
                                                <asp:FormView ID="FormView1" runat="server" DataKeyNames="sr" 
                                                    DataSourceID="SqlDataSource15">
                                                    <edititemtemplate>
                                                        sr:
                                                        <asp:Label ID="srLabel1" runat="server" Text='<%# Eval("sr") %>' />
                                                        <br />
                                                        <asp:LinkButton ID="UpdateButton" runat="server" CausesValidation="True" 
                                                            CommandName="Update" Text="Update" />
                                                         <asp:LinkButton ID="UpdateCancelButton" runat="server" 
                                                            CausesValidation="False" CommandName="Cancel" Text="Cancel" />
                                                    </edititemtemplate>
                                                    <insertitemtemplate>
                                                        <asp:LinkButton ID="InsertButton" runat="server" CausesValidation="True" 
                                                            CommandName="Insert" Text="Insert" />
                                                         <asp:LinkButton ID="InsertCancelButton" runat="server" 
                                                            CausesValidation="False" CommandName="Cancel" Text="Cancel" />
                                                    </insertitemtemplate>
                                                    <itemtemplate>
                                                        UserId: 
                                                        <asp:Label ID="srLabel2" runat="server" CssClass="style696" 
                                                            Text='<%# Eval("sr") %>' />
                                                        <br />
                                                    </itemtemplate>
                                                
                                            </td>
                                            <td class="style794">
                                                 </td>
                                            <td class="style792">
                                                <asp:CheckBox ID="CheckBox5" runat="server" CssClass="style791" 
                                                    Text="Bookmark" />
                                            </td>
                                            <td class="style698">
                                                <asp:CheckBox ID="CheckBox6" runat="server" CssClass="style791" 
                                                    Text="Send Profile to Friend" />
                                            </td>
                                            <td class="style678">
                                                <asp:CheckBox ID="CheckBox7" runat="server" CssClass="style791" 
                                                    Text="Block Profile" />
                                            </td>
                                        </tr>
                                    </table>
                                    
                                </td>
                            </tr>
                            <tr>
                                <td class="style718" style="vertical-align: top">
                                </td>
                                <td class="style801" style="vertical-align: top">
                                    <asp:TextBox ID="TextBox22" runat="server">
                                </td>
                                <td class="style718" style="vertical-align: top">
                                </td>
                                <td class="style718" style="vertical-align: top">
                                     </td>
                            </tr>
                            <tr>
                                <td class="style713" style="vertical-align: top">
                                </td>
                                <td class="style802" style="vertical-align: top">
                                    <asp:Image ID="Image40" runat="server" Height="144px" 
                                        ImageUrl="~/images/contactus1.png" Width="148px" />
                                    <asp:DataList ID="DataList3" runat="server" DataKeyField="sr" 
                                        DataSourceID="SqlDataSource14" Width="150px">
                                        <itemtemplate>
                                            <asp:Image ID="Image39" runat="server" Height="144px" 
                                                ImageUrl='<%# "ImageHandler.ashx?ImID="+ Eval("sr")  %>' Width="148px" />
                                            <br />
                                        </itemtemplate>
                                    
                                    <asp:SqlDataSource ID="SqlDataSource14" runat="server" 
                                        ConnectionString="<%$ ConnectionStrings:ConnectionString %>" 
                                        SelectCommand="SELECT * FROM [tbl_Image1] WHERE ([UserName] = @UserName)">
                                        <SelectParameters>
                                            <asp:ControlParameter ControlID="UserFromLabel" Name="UserName" 
                                                PropertyName="Text" Type="String" />
                                        </SelectParameters>
                                    
                                    <asp:SqlDataSource ID="SqlDataSource15" runat="server" 
                                        ConnectionString="<%$ ConnectionStrings:ConnectionString %>" 
                                        SelectCommand="SELECT [sr] FROM [tbl_FreeUserProfile] WHERE ([UserName] = @UserName)">
                                        <SelectParameters>
                                            <asp:ControlParameter ControlID="UserFromLabel" Name="UserName" 
                                                PropertyName="Text" Type="String" />
                                        </SelectParameters>
                                    
                                </td>
                                <td class="style714" style="vertical-align: top">
                                </td>
                                <td class="style716" style="vertical-align: top">
                                    <asp:DataList ID="DataList4" runat="server" DataKeyField="sr" 
                                        DataSourceID="SqlDataSource10" Width="100%">
                                        <itemtemplate>
                                            <table class="style472">
                                                style="font-family: 'Trebuchet MS'; font-size: 10pt; height: 44px;">
                                                <tr>
                                                    <td class="style719" colspan="3">
                                                         </td>
                                                </tr>
                                                <tr>
                                                    <td class="style805">
                                                        Name</td>
                                                    <td class="style694">
                                                        :</td>
                                                    <td class="style720">
                                                        <asp:Label ID="NameLabel" runat="server" CssClass="style750" 
                                                            Text='<%# Eval("Name") %>' />
                                                         <asp:Label ID="MiddleNameLabel0" runat="server" CssClass="style750" 
                                                            Text='<%# Eval("MiddleName") %>' />
                                                         <asp:Label ID="LastNameLabel1" runat="server" CssClass="style750" 
                                                            Text='<%# Eval("LastName") %>' />
                                                    </td>
                                                </tr>
                                                <tr>
                                                    <td class="style805">
                                                        Age</td>
                                                    <td class="style694">
                                                        :</td>
                                                    <td class="style720">
                                                        <asp:Label ID="AgeLabel" runat="server" CssClass="style710" 
                                                            style="font-size: 9pt" Text='<%# Eval("Age") %>' />
                                                    </td>
                                                </tr>
                                                <tr>
                                                    <td class="style805">
                                                        Religion</td>
                                                    <td class="style694">
                                                        :</td>
                                                    <td class="style720">
                                                        <asp:Label ID="ReligionLabel" runat="server" CssClass="style710" 
                                                            style="font-size: 9pt" Text='<%# Eval("Religion") %>' />
                                                    </td>
                                                </tr>
                                                <tr>
                                                    <td class="style805">
                                                        Caste
                                                    </td>
                                                    <td class="style694">
                                                        :</td>
                                                    <td class="style720">
                                                        <asp:Label ID="CasteLabel" runat="server" CssClass="style710" 
                                                            style="font-size: 9pt" Text='<%# Eval("Caste") %>' />
                                                    </td>
                                                </tr>
                                                <tr>
                                                    <td class="style805">
                                                        Weight</td>
                                                    <td class="style694">
                                                        :</td>
                                                    <td class="style720">
                                                        <asp:Label ID="WeightLabel" runat="server" CssClass="style710" 
                                                            style="font-size: 9pt" Text='<%# Eval("Weight") %>' />
                                                    </td>
                                                </tr>
                                                <tr>
                                                    <td class="style805">
                                                        Height</td>
                                                    <td class="style694">
                                                        :</td>
                                                    <td class="style720">
                                                        <asp:Label ID="HeightLabel" runat="server" CssClass="style710" 
                                                            style="font-size: 9pt" Text='<%# Eval("Height") %>' />
                                                    </td>
                                                </tr>
                                                <tr>
                                                    <td class="style805">
                                                        Education</td>
                                                    <td class="style694">
                                                        :</td>
                                                    <td class="style720">
                                                        <asp:Label ID="EducationLabel" runat="server" CssClass="style710" 
                                                            style="font-size: 9pt" Text='<%# Eval("Education") %>' />
                                                    </td>
                                                </tr>
                                                <tr>
                                                    <td class="style805">
                                                        Occupation</td>
                                                    <td class="style694">
                                                        :</td>
                                                    <td class="style720">
                                                        <asp:Label ID="OccupationLabel" runat="server" CssClass="style710" 
                                                            style="font-size: 9pt" Text='<%# Eval("Occupation") %>' />
                                                    </td>
                                                </tr>
                                                <tr>
                                                    <td class="style805">
                                                        Location</td>
                                                    <td class="style694">
                                                        :</td>
                                                    <td class="style721">
                                                        
                                                        <asp:Label ID="CountryLabel" runat="server" Text='<%# Eval("Country") %>' />
                                                        , <asp:Label ID="stateLabel" runat="server" Text='<%# Eval("state") %>' />
                                                        , <asp:Label ID="cityLabel" runat="server" Text='<%# Eval("city") %>' />
                                                        
                                                    </td>
                                                </tr>
                                                <tr>
                                                    <td class="style705" colspan="3">
                                                        <table class="style472">
                                                            style="font-family: 'Trebuchet MS'; font-size: 10pt; height: 6px;">
                                                            <tr>
                                                                <td class="style810" style="text-align: left">
                                                                    <asp:HyperLink ID="HyperLink34" runat="server" Font-Underline="False" 
                                                                        NavigateUrl='<%# Eval("UserName","~/paid user/viewFullProfile.aspx?UserName={0}") %>' 
                                                                        style="color: #003399">View Full Profile
                                                                </td>
                                                                <td class="style700" style="text-align: left">
                                                                    <asp:HyperLink ID="HyperLink33" runat="server" Font-Strikeout="False" 
                                                                        NavigateUrl='<%# Eval("UserName","~/paid user/sendMessage.aspx?UserName={0}") %>' 
                                                                        style="color: #CC6600">Personal Message
                                                                </td>
                                                                <td class="style704" style="text-align: left">
                                                                     </td>
                                                            </tr>
                                                        </table>
                                                    </td>
                                                </tr>
                                            </table>
                                        </itemtemplate>
                                    
                                    <br />
                                    <asp:TextBox ID="TextBox21" runat="server">
                                </td>
                            </tr>
                            <tr>
                                <td class="style713" style="vertical-align: top">
                                     </td>
                                <td class="style712" colspan="3">
                                    style="vertical-align: top; border-top-style: dashed; border-top-width: thin; border-top-color: #808080;">
                                    Message:
                                    <asp:Label ID="MessageLabel" runat="server" Text='<%# Eval("Message") %>' 
                                        style="font-family: 'Trebuchet MS'" />
                                </td>
                            </tr>
                            <tr>
                                <td class="style807" style="vertical-align: top">
                                </td>
                                <td class="style808" colspan="3">
                                </td>
                            </tr>
                        </table>
                        <asp:SqlDataSource ID="SqlDataSource10" runat="server" 
                            ConnectionString="<%$ ConnectionStrings:ConnectionString %>" 
                            SelectCommand="SELECT * FROM [tbl_FreeUserProfile] WHERE ([UserName] = @UserName)">
                            <SelectParameters>
                                <asp:ControlParameter ControlID="UserFromLabel" Name="UserName" 
                                    PropertyName="Text" Type="String" />
                            </SelectParameters>
                        
                    </itemtemplate>
Posted
Updated 24-Aug-11 19:49pm
v2
Comments
Herman<T>.Instance 24-Aug-11 15:26pm    
too less code shown to be able to see what goes wrong.
All Isee now is Items[0], so only row 1 might be affected
Al Moje 24-Aug-11 21:59pm    
In your client code try to add
UpdatePanel
Sunasara Imdadhusen 25-Aug-11 0:50am    
Please provide complete code for Page_Load and Binding of DataList. then i will give you answer.
patelKhyati 25-Aug-11 1:51am    
i hv updated my que.
patelKhyati 25-Aug-11 1:50am    
i hv updated my que.

Updated Code

protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
{ 
 if ((e.Item.ItemType == ListItemType.Item) || (e.Item.ItemType == ListItemType.AlternatingItem))
            {


   foreach (DataListItem di in InnerDataList.Items) // Your Nested DataList
     { 
        Image Image1 = (Image)di.FindControl("Image39");
 
       if (Image1 != null) 
           { 
            
             if("Your Condition")
		{
                  Image1.Visible= false;
		}
             else
                 {
                   Image1.Visible= true;
	         }
            }
      }
}

 }
 
Share this answer
 
v3
Comments
patelKhyati 25-Aug-11 4:51am    
did this:
protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
{
foreach (DataListItem di in DataList1.Items)
{
Image Image1 = (Image)di.FindControl("Image39");
if (Image1 != null)
{
((System.Web.UI.WebControls.Image)(DataList1.Items[0].FindControl("Image40"))).Visible = false;
}
else
{
((System.Web.UI.WebControls.Image)(DataList1.Items[0].FindControl("Image40"))).Visible = true;
}
}
}

but results d same
patelKhyati 25-Aug-11 4:52am    
my image39 is in datalist3 under datalist1
patelKhyati 25-Aug-11 4:55am    
and image40 is default image in datalist 1
My basic senario is if user have uploaded any image than datalist3(bind with image from my database) will be displayed but if not than my default image control image40 will be displayed.
patelKhyati 25-Aug-11 6:53am    
i have modified ur code a bit. it worked fine. but just last item of datalist is not updating. if u cn suggest anything. my code is as follow.

protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
{
int i;
////foreach (DataListItem di in DataList1.Items)
for (i=0; i < DataList1.Items.Count; i++)
{
if (((DataList)(DataList1.Items[i].FindControl("DataList3"))).Items.Count <= 0)
{
((System.Web.UI.WebControls.Image)(DataList1.Items[i].FindControl("Image40"))).Visible = true;

}
else
{
((System.Web.UI.WebControls.Image)(DataList1.Items[i].FindControl("Image40"))).Visible = false;
}
}
}
Mahendra.p25 25-Aug-11 7:24am    
Updated My Code check it out
the following code worked but for the first row in datalist1 only.

C#
protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
    {
        foreach (DataListItem di in DataList1.Items)
        {    
            if (((DataList)(DataList1.Items[0].FindControl("DataList3"))).Items.Count <= 0)
                {
                  ((System.Web.UI.WebControls.Image)(DataList1.Items[0].FindControl("Image40"))).Visible = true;
                 }
                else
                {
                    ((System.Web.UI.WebControls.Image)(DataList1.Items[0].FindControl("Image40"))).Visible = false;
                }
         }
            
            
    }



can anyone suggest how to apply this to each row in datalist1
 
Share this answer
 
Comments
Mahendra.p25 25-Aug-11 5:59am    
hi, Updated my Code check it Out.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900