1

I have tried for so long to understand why my LinkButton text toggles to say "on" when its clicked, however when I click the button again the text still says "on" even though in my code behind file I set LinkButton.text = "off". I have run the debugger and when I set the text to "off" its like the setter method doesnt actually update that specific LinkButton object, as the next click its like it never got updated.

I am using this to test why my panel inside a gridview template field wont toggle its visibility on and off, as the same thing happens with that where I click a button to make the panel visible which it does, then when I press the button again the panel should disappear but it stays on the screen. My goal is to have each row in a gridrow have a toggle link button that displays a panel that drops down below each row.

So the essence to my problem is, why can I toggle a control on, but cannot toggle it off?.

My markup is essentially the same as below. Note all the actual proper server attributes are left out for sake of clarity.

  • I've tried setting viewstate to false but that doesn't help.
  • The funny part is, I place the LinkButton and Panel outside of the gridview and it does exactly what I expect it to do which is toggle on and off or set the panel to visible or hide.
  • I ready some other posts but I don't fully understand the issue. I have read that some people say its about nesting controls inside other controls but I am just not certain how this is causing an issue.
  • UpdatePanel.Visible = true has no effect
  • http://forums.asp.net/t/1773859.aspx?C+Visible+true+not+working

My other thought is that the button is rendered at a stage that is too late for me to make a change that will be placed on the final html?

Can anyone give me any pointers?

<asp:GridView>
  <Columns>
    <asp:TemplateField>
           <ItemTemplate>
                 <div id="wrapper">
                      <asp:LinkButton ID="LinkButton1" runat="server" 
                       CommandArgument='<%# ((GridViewRow)Container).RowIndex %>' 
                       Text="off" oncommand="changeName"></asp:LinkButton>
                  </div>
           </ItemTemplate>
    </asp:TemplateField>
  </Columns>
</asp:GridView>

code behind

    protected void changeName(object sender, CommandEventArgs e)
    {

        GridViewRow row = MyGridView.Rows[Convert.ToInt32(e.CommandArgument)];

        LinkButton button = (LinkButton)row.FindControl("LinkButton1");


        if (button.Text.Equals("off"))
        {
            button.Text = "on";

        }
        else
        {
            button.Text = "off";
        }
    }
Community
  • 1
  • 1
reversebind
  • 1,216
  • 1
  • 14
  • 18
  • Please add the code where you set `LinkButton.text = "off"` and also the `Page_Load` method. – ekad Sep 27 '14 at 12:22
  • I only had a method made for the click button, no code in the `Page_Load` method. Should I have code in the `Page_Load` also? – reversebind Sep 27 '14 at 23:06
  • so where is the places GridView bind, pageload? – bashkan Sep 27 '14 at 23:09
  • I just did all that part with an sqlDataSource and gridview in design mode so its not in the code behind file that part. I only used the code behind file to put button click events and things like that. – reversebind Sep 27 '14 at 23:19

1 Answers1

0

I solved the issue by removing my 'EnableViewState = "False"` on the gridview.

After reading this article http://forums.asp.net/t/1590532.aspx?Visible+invisible+panel+inside+Gridview

It gave me some ideas on the whole postback process. I am not sure if this is correct but it kind of makes some sense to me?

If my gridview is disabling viewstate and its the parent to all nested controls such as 'Panel' and 'LinkButton' then the state of those nested controls will be lost across postback. So any dynamically made changes I modified in the code behind file would be lost if the changes were not being retained in the viewstate of the gridview.

There is probably a better way of achieving my design but for the moment leaving viewstate on the parent control seemed to fix it.

reversebind
  • 1,216
  • 1
  • 14
  • 18