locked
visible attribute of panel and label controls RRS feed

  • Question

  • User-191253613 posted

    I have a page where I have multiple button events that give access to a customer database. Each button event offers a different search criteria to access customer info. This page is not the problem. The problem comes in the results page where I have multiple gridview controls that receive the info. Actually I just have two at the moment; I stopped there because of my problem, which I've tried everything I can think of to solve.

    Everything works fine except that the visible attributes of the panel and label controls are not working properly. When I have just one GridView control, everything works fine. When I put the second GridView control, the panel and label controls that I have set to "false" in my code behind are becoming visible. I've tried so many variations, but nothing works. I tried try/catch, if statements, but with those coding statements it seems top be worse. Even with one GridView control, the labels I want to become visible, don't when using statements. 

    I don't know if I made this clear for anyone to help me, but if anyone has a suggestion, I am all ears!!! 

    Thanks so much for any help!

    Here's my results.aspx page code that receives the request...

    <asp:Panel ID="plLastName" Visible="false" CssClass="ParaText" runat="server">
    <div class="welcomeHeader" runat="server">
       <h4><asp:Label ID="lblSearchCriteria1" runat="server" Visible="false" Text="Here are your search results for 'Last Name'..."></asp:Label></h4>
    </div>

    <br />

    <asp:GridView ID="GridView1" HeaderStyle-BackColor="#006699" runat="server" AutoGenerateColumns="False">
       <Columns>
          <asp:BoundField DataField="FirstName" HeaderText="First Name" HeaderStyle-CssClass="ParaTextWhite" SortExpression="FirstName" />
          <asp:BoundField DataField="LastName" HeaderText="Last Name" HeaderStyle-CssClass="ParaTextWhite" SortExpression="LastName" />
          <asp:BoundField DataField="CustomerId" HeaderText="Customer Id" HeaderStyle-CssClass="ParaTextWhite" InsertVisible="False" ReadOnly="True" SortExpression="CustomerId" />
          <asp:BoundField DataField="StreetAddress" HeaderText="Street Address" HeaderStyle-CssClass="ParaTextWhite" SortExpression="StreetAddress" />
          <asp:BoundField DataField="City" HeaderText="City" HeaderStyle-CssClass="ParaTextWhite" SortExpression="City" />
          <asp:BoundField DataField="State" HeaderText="State" HeaderStyle-CssClass="ParaTextWhite" SortExpression="State" />
          <asp:BoundField DataField="ZipCode" HeaderText="Zip Code" HeaderStyle-CssClass="ParaTextWhite" SortExpression="ZipCode" />
          <asp:BoundField DataField="Phone" HeaderText="Phone" HeaderStyle-CssClass="ParaTextWhite" SortExpression="Phone" />
          <asp:BoundField DataField="Email" HeaderText="Email" HeaderStyle-CssClass="ParaTextWhite" SortExpression="Email" />
       </Columns>
    </asp:GridView>


    <asp:Label ID="lblErrorMessage1" CssClass="ParaTextRed" Visible="false" runat="server" Text="<img src='../Images/design/status-red-check-unavailable.jpg' style='vertical-align:middle;' /> <span class='ParaTextRed'>Cannot Find Customer! Please be sure you have typed the last name correctly.</span>"></asp:Label>
    </asp:Panel>


    <asp:Panel ID="plUserName" Visible="false" CssClass="ParaText" runat="server">
    <div class="welcomeHeader" runat="server">
       <h4><asp:Label ID="lblSearchCriteria2" runat="server" Visible="false" Text="Here are your search results for 'Username'..."></asp:Label></h4>
    </div>

    <br />

    <asp:GridView ID="GridView2" HeaderStyle-BackColor="#006699" runat="server" AutoGenerateColumns="False">
       <Columns>
          <asp:BoundField DataField="FirstName" HeaderText="First Name" HeaderStyle-CssClass="ParaTextWhite" SortExpression="FirstName" />
          <asp:BoundField DataField="LastName" HeaderText="Last Name" HeaderStyle-CssClass="ParaTextWhite" SortExpression="LastName" />
          <asp:BoundField DataField="CustomerId" HeaderText="Customer Id" HeaderStyle-CssClass="ParaTextWhite" InsertVisible="False" ReadOnly="True" SortExpression="CustomerId" />
          <asp:BoundField DataField="StreetAddress" HeaderText="Street Address" HeaderStyle-CssClass="ParaTextWhite" SortExpression="StreetAddress" />
          <asp:BoundField DataField="City" HeaderText="City" HeaderStyle-CssClass="ParaTextWhite" SortExpression="City" />
          <asp:BoundField DataField="State" HeaderText="State" HeaderStyle-CssClass="ParaTextWhite" SortExpression="State" />
          <asp:BoundField DataField="ZipCode" HeaderText="Zip Code" HeaderStyle-CssClass="ParaTextWhite" SortExpression="ZipCode" />
          <asp:BoundField DataField="Phone" HeaderText="Phone" HeaderStyle-CssClass="ParaTextWhite" SortExpression="Phone" />
          <asp:BoundField DataField="Email" HeaderText="Email" HeaderStyle-CssClass="ParaTextWhite" SortExpression="Email" />
       </Columns>
    </asp:GridView>


    <asp:Label ID="lblErrorMessage2" CssClass="ParaTextRed" Visible="false" runat="server" Text="<img src='../Images/design/status-red-check-unavailable.jpg' style='vertical-align:middle;' /> <span class='ParaTextRed'>Cannot Find Customer! Please be sure you have typed in the username correctly.</span>"></asp:Label>
    </asp:Panel>

    -----------------------

    Now here is my code behind (I've noted in the code with comments that all the codes below that designate the Visible attribute work as I have coded them, but all become visible even when I code them to be false either in the main page or the code behind...

    protected void Page_Load(object sender, EventArgs e)
    {
       DataTable dt1 = GetData1();
       GridView1.DataSource = dt1;
       GridView1.DataBind();

       DataTable dt2 = GetData2();
       GridView2.DataSource = dt2;
       GridView2.DataBind();
    }

    private string GetCache1()
    {
    string cachedString1;
    cachedString1 = (string)Cache["cacheLastName"];
    return cachedString1;
    }

    private string GetCache2()
    {
       string cachedString2;
       cachedString2 = (string)Cache["cacheUserName"];
       return cachedString2;
    }


    private DataTable GetData1()
    {
       string lastname = GetCache1();

       using (SqlConnection con = new SqlConnection(connectionString))
       {
          using (SqlCommand cmd1 = new SqlCommand("SELECT CustomerId,FirstName,LastName,StreetAddress,City,State,ZipCode,Phone,Email FROM Customers WHERE LastName='" +           lastname + "'", con))
          {
             plLastName.Visible = true; //**This works
             lblSearchCriteria1.Visible = true; //**This Works
             DataTable dt1 = new DataTable();
             SqlDataAdapter da1 = new SqlDataAdapter(cmd1);
             da1.Fill(dt1);
             if (dt1.Rows.Count == 1)
             {
               return dt1;
             }
             else

             {
                lblErrorMessage1.Visible = true; //**This Works
                return dt1;
             }
          }
       }
    }

    private DataTable GetData2()
    {
       string username = GetCache2();

       using (SqlConnection con = new SqlConnection(connectionString))
       {
          using (SqlCommand cmd2 = new SqlCommand("SELECT CustomerId,FirstName,LastName,UserName,StreetAddress,City,State,ZipCode,Phone,Email FROM Customers WHERE                 UserName='" + username + "'", con))
         {
            plUserName.Visible = true; //**This works
            lblSearchCriteria2.Visible = true; //**This works
            DataTable dt2 = new DataTable();
            SqlDataAdapter da2 = new SqlDataAdapter(cmd2);
            da2.Fill(dt2);
            if (dt2.Rows.Count == 1)
            {
               return dt2;
            }
            else
            {
               lblErrorMessage2.Visible = true; //**This works
               return dt2;
             }
         }
       }
    }

    Friday, January 17, 2014 7:49 PM

Answers

  • User-417640953 posted

    protected void Page_Load(object sender, EventArgs e)
    {
       DataTable dt1 = GetData1();
       GridView1.DataSource = dt1;
       GridView1.DataBind();

       DataTable dt2 = GetData2();
       GridView2.DataSource = dt2;
       GridView2.DataBind();
    }

    Hi Russ08,

    Thanks for the post.

    From your code, I see you set the panels and labels Visible as false in the designed page.

    If you did not change it as true in the code behind, it will not shown in page. However, I see you call the methods

    GetData1() and GetData2() in the page load. So that will cause the panels and labels visible when first page load or postback.

    In GetData1() method:

     plLastName.Visible = true; //**This works
             lblSearchCriteria1.Visible = true; //**This Works

    In GetData2() method:

    plUserName.Visible = true; //**This works
            lblSearchCriteria2.Visible = true; //**This works

    Please check your code and try it again, you can also set break point and dubug it see what happened.

    If I misunderstand your issue, please feel free to back thanks.

    Best Regards!

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, January 20, 2014 3:42 AM