How to hide or show Panel from Database if data exist in Database using C# in ASP.Net

Last Reply on Jan 02, 2017 05:05 AM By Indresh

Posted on Jan 01, 2017 10:53 PM

I have posts made by users, but my challenge is this, if there is no post made by users the broken image control and lable still shows on page making my page to look horrible. So the idea is to check if PostId in database exist if not exist hide panel 1 and panel2, the reason for hidding panel is because i have to put these control in panels so that it will be easier for me to hid them all.

 

System.Text.StringBuilder sb = new System.Text.StringBuilder();
    SqlConnection conne = new SqlConnection(ConfigurationManager.ConnectionStrings["con"].ToString());
    string constrr = ConfigurationManager.ConnectionStrings["conn"].ConnectionString;
    string con;
    DataTable dt;
    // int id;
    // private string constr = ConfigurationManager.ConnectionStrings["con"].ConnectionString;
    static string username = string.Empty;
    static int Id = 0;
    protected void Page_Load(object sender, EventArgs e)
    {

        if (!this.IsPostBack)
    {
        if (!this.IsPostBack)
        
            {
                username = this.Page.User.Identity.Name;
             
                DataTable dt = GetProfile();
                               
                        GetMergedAll.DataSource = dt;
                ViewState["DataTable"] = dt;
                GetMergedAll.DataSource = dt;
                GetMergedAll.DataBind();              
              
               // if (dt.Rows.Count > 0)
                {
                    
                }
            }
        }
      
    }
    public DataTable GetProfile()
    {
        SqlConnection constr = new SqlConnection(ConfigurationManager.ConnectionStrings["conn"].ConnectionString);
        SqlDataAdapter adp = new SqlDataAdapter("GetUserPOSTS", constr);
    adp.SelectCommand.CommandType = CommandType.StoredProcedure;
   // adp.SelectCommand.Parameters.AddWithValue("@Email", username);
    adp.SelectCommand.Parameters.AddWithValue("@UserName", username);
    adp.SelectCommand.Parameters.AddWithValue("@Id", Id);
    DataTable dt = new DataTable();
        //
    adp.Fill(dt);
    return dt;

        
    }

here is the two panels PANEL 1 PANEL2

Posted on Jan 02, 2017 05:05 AM

Hi micah,

I have certain modified in your code. So please refer the below code.

HTML

<div>
    <asp:DataList ID="GetMergedAll" runat="server" DataKeyName="Id" OnItemCommand="GetMergedAll_ItemCommand"
        OnItemDataBound="GetMergedAll_ItemDataBound">
        <ItemTemplate>
            <asp:Label ID="lblShareId" Text='<%#Eval("ShareId")%>' runat="server" />
            <asp:Panel ID="hidIfNULL" runat="server">
                <asp:Label ID="Label34" runat="server" CssClass="fa fa-rotate-right" ForeColor="#99CC00"></asp:Label>
                <asp:Label ID="Label36" runat="server" Text='<%#DataBinder.Eval(Container.DataItem,"Name") %>'
                    ForeColor="#999999" />
                <asp:Label ID="Label38" runat="server" Text=" passed this" ForeColor="#999999"></asp:Label>
                <asp:Label ID="Label35" runat="server" CssClass="fa fa-level-down "></asp:Label>
            </asp:Panel>
        </ItemTemplate>
    </asp:DataList>
</div>

C#

protected void Page_Load(object sender, EventArgs e)
{

    if (!this.IsPostBack)
    {
        username = this.Page.User.Identity.Name;
        DataTable dt = GetProfile();
        ViewState["DataTable"] = dt;
        GetMergedAll.DataSource = dt;
        GetMergedAll.DataBind();
    }
}

public DataTable GetProfile()
{
    SqlConnection constr = new SqlConnection(ConfigurationManager.ConnectionStrings["conn"].ConnectionString);
    SqlDataAdapter adp = new SqlDataAdapter("GetUserPOSTS", constr);
    adp.SelectCommand.CommandType = CommandType.StoredProcedure;
    // adp.SelectCommand.Parameters.AddWithValue("@Email", username);
    adp.SelectCommand.Parameters.AddWithValue("@UserName", username);
    adp.SelectCommand.Parameters.AddWithValue("@Id", Id);
    DataTable dt = new DataTable();
    //
    adp.Fill(dt);
    return dt;
}

protected void GetMergedAll_ItemDataBound(object sender, DataListItemEventArgs e)
{
    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
    {
        if (!string.IsNullOrEmpty((e.Item.FindControl("lblShareId") as Label).Text))
        {
            (e.Item.FindControl("hidIfNULL") as Panel).Visible = true;
        }

        else
        {
            (e.Item.FindControl("hidIfNULL") as Panel).Visible = false;
        }
    }
}