1
votes

This is the problem that bothers me for the past few days

I have a page with couple user controls.

On that page, there is a grid. If a user click on a particular row, a control (li) on the user control is shown or hidden based on the condition.

What makes things complicate is the user control and grid grid are all inside the update panel, but another user control is outside updaet panel

In the rowcommand event, I assigned a Session variable, dontshow. Based on the criteria, dontshow variable will be assigned to true or false. Then the control on the user control will be shown or hidden based on dontshow condition

So far, I am encountering the problem.

let's say (li) control was shown, but after the user click gridview, dontshow is set to true. (li) control should be hidden. No it does not. I have to click other button to do postback to make it happen

The same is true for the usercontrol outside the update panel. The logic is similar

Over the past few days, I have found out that I can't set visible property of controls in user control at page_load because that happens after rowcommand event. I moved the code to pre_render event

I even tried to use different control such as placeholder, panel, etc. That still does not work I also trace the code. The pre_render event comes after row command. dontshow variable is false. The (li) control visible is set to false. However, after rendering, I can still see the control

What should I do?

Below are the snapshot of the codes

Thank you for the help

main.aspx

.....
<%@ Register Src="../usercontrol1.ascx" TagPrefix="uctest1" TagName="ctrl1" %>
<%@ Register Src="../usercontrol2.ascx" TagPrefix="uctest2" TagName="ctrl2" %>
...

<ctrl1:uctest1runat="server" ID="test1" />
<asp:UpdatePanel ID="updatepantest1" runat="server" UpdateMode="Always">
   <ContentTemplate>

      <uctest2:ctrl2runat="server" UpdateMode="Conditional" ID="test2" />
....

     <asp:GridView ...>
        //grid where rowcommand was executed
     </asp:GridView>
   </ContentTemplate>
</asp:UpdatePanel>

main.aspx code behind

protected void gridview_RowCommand(object sender, GridViewCommandEventArgs e)
{
  if (e.CommandName == "something")
  {
      .....
      if (x = 1)
        session["dontshow"] = true
      else
        session["dontshow"] = false
  }
}

user control aspx (usercontrol1)

....

 <ul>
  <li id="li1" runat="server">
    <asp:label>this is test</asp:label>
  </li>
  <li >
    <asp:label>this is test</asp:label>
  </li>
  <li >
    <asp:label>this is test</asp:label>
  </li>
</ul>

.... user control code behind (usercontrol1)

protected void Page_PreRender(object sender, EventArgs e)
{
    bool dontshow = false;
    if (Session["dontshow"] != null)
    {
       dontshow = (bool)Session["dontshow"]
    }
    if (dontshow)
      li1.visible = false
     else
      li1.visible = true
}

user control aspx (usercontrol2)

....

<asp:PlaceHolder ID="placeholder1" runat="server">
<asp:label ID="label1" runat="server"></asp:label>
</asp:PlaceHolder>

.... user control code behind (usercontrol2)

protected void Page_PreRender(object sender, EventArgs e)
{
    bool dontshow = false;
    if (Session["dontshow"] != null)
    {
       dontshow = (bool)Session["dontshow"]
    }
    if (dontshow)
    {
      label1.visible =false;
    }
     else
     {
       label1.visible =true;
       label1.Text ="this is test";

     }
}
1
Instead of setting the Visible property in Page_PreRender why don't you do it in gridview_RowCommand? Then you won't need to use a session variable. - Sam
Thanks, I tried to do it on RowCommand, and it still did not work. By the way, I need to store in Session because when we navigate to other pages, we will know the status of conditions to determine whether to show or not. Any other suggestions? - user12345
Can you show some code how you did it? - Sam
As you suggested, I moved li1.visible = false (or true) statements to gridview_RowCommand. That did not work. I don't know why. It looks like it was too late to set visible after page_load event. Eventually, I wrote the jquery to change the visible property. I have to do in row_command. That kinds of defeat the purpose. I wanted to do it in user control. So far it has been working. I will cross my finger - user12345
Ok. If "li1" control is not part of the gridview try something like below in "li1" user control's code behind. protected override void OnLoad(EventArgs e) { base.OnLoad(e); if (Session["dontshow"] != null) { if ((bool)Session["dontshow"]) li1.visible = false else li1.visible = true } } - Sam

1 Answers

1
votes

You could do it in your Row command like this

protected void gridview_RowCommand(object sender, GridViewCommandEventArgs e)
{
  if (e.CommandName == "something")
  {
      .....

    var viewRow = (GridViewRow)(((ImageButton)e.CommandSource).NamingContainer);
    HtmlGenericControl li1 = (HtmlGenericControl)viewRow.FindControl("li1");

      if (x = 1)
    {
            session["dontshow"] = true;
        li1.Visible = true;
    }
      else
    {
            session["dontshow"] = false;
        li1.Visible = false;
    }
  }
}

Or, if you want to hide the control during data bound you could do it like this

protected void gridview_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        HtmlGenericControl li1 = (HtmlGenericControl)e.Row.FindControl("li1");

    if (dontshow)
            li1.visible = false;
        else
            li1.visible = true;
    }
}

Hope this helps :-)