0

I used a linkbutton in repeater which on click shows data in a label.Now i want that clicking again the same linkbutton hide that data,means same button for showing and hiding data. there is a database with a table which contains ques-description,date,sub. by and ans.
On page load only question appears.

Now this is the design code:

protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
       {  
            if (e.CommandName == "showanswers")  
            {  
                Control control;  
                control = e.Item.FindControl("date");
                if(control!=null)
                control.Visible = true;
                control = e.Item.FindControl("subby");
                if(control!=null)
                control.Visible = true;
                control = e.Item.FindControl("ans");
                if(control!=null)
                control.Visible = true;
            }

And this is the html code i used:

<asp:Repeater ID="Repeater1" runat="server"  
            onitemcommand="Repeater1_ItemCommand">

            <ItemTemplate>
                <table>
                        <b>Question<%#Container.ItemIndex + 1%>:</b><%#Eval("qstdsc") %><br />
                        <asp:linkbutton ID="Button1" Text="Ans." commandname="showanswers" runat ="server" /><br />
                    </table>
                <table>
                        <asp:Label id="date" Text='<%# Eval("qstdat")%>' Visible="false" runat="server"/>
                    </table>
                    <table>    
                    <asp:Label id="subby" runat="server" Text='<%# Eval("qstsubby")%>' Visible="false" />
                       </table>
                <table>
                         <asp:Label id="ans" runat="server" Text='<%# Eval("qstans")%>' Visible="false" />
                </table>

             </ItemTemplate>
        </asp:Repeater>  

But i don't know how to hide data again clicking the same linkbutton. Is it possible with a single button?

user2069465
  • 47
  • 1
  • 2
  • 8

1 Answers1

0

What hinders you to check if the label is visible and hide/show it accordingly?

protected void lnkBtnShowDataLabel_Click(Object sender, EventArgs e)
{
    lblData.Visible = !lblData.Visible;
}
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
  • Since you have shown now that you're using the repeater's `ItemCommand` event, you can use your code and replace `control.Visible = true;` with `control.Visible = !control.Visible;`. This should just switch visibility. – Tim Schmelter Feb 14 '13 at 15:33
  • @user2069465: When do you databind the repeater, is it possible that you're doing it on **every** postback? Does your code work, so can you make them visible at all? Is the `Repeater1_ItemCommand` triggered at all, have you set a breakpoint there? – Tim Schmelter Feb 14 '13 at 15:55
  • oh yes sir now it's working after using postback :p Thank you so much sir :) – user2069465 Feb 14 '13 at 17:01