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";
}
}