Click here to Skip to main content
15,910,358 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
C#
protected void btnAnswer_Click(object sender, EventArgs e)
   {
       LinkButton lnk = (LinkButton)sender;
       GridViewRow grv = (GridViewRow)lnk.Parent.Parent;
       int rowir = grv.RowIndex;
       if (((Panel)rptQuestion.Rows[rowir].FindControl("Panel1")).Visible == true)
       {
           ((Panel)rptQuestion.Rows[rowir].FindControl("Panel1")).Visible = false;
           lnk.Text = "Show Answer";
       }
       else
       {
           ((Panel)rptQuestion.Rows[rowir].FindControl("Panel1")).Visible = true;
           lnk.Text = "Hide Answer";
       }
   }


What I have tried:

Slow working .please suggest me for faster working...!
Posted
Updated 31-May-16 16:24pm
Comments
Peter Leow 31-May-16 22:00pm    
You have repeated this piece of code 3 times:
(Panel)rptQuestion.Rows[rowir].FindControl("Panel1")

There is nothing wrong written here which can cause noticable performance decrease. Yes, there is scope of optimization though.
1. As @Peter Leow has mentioned in his comments, try not to repeat creating the same object. Try to reuse the object. Something like following-
C#
protected void btnAnswer_Click(object sender, EventArgs e)
   {
       LinkButton lnk = (LinkButton)sender;
       GridViewRow grv = (GridViewRow)lnk.Parent.Parent;
       int rowir = grv.RowIndex;
       Panel pnl=(Panel)rptQuestion.Rows[rowir].FindControl("Panel1");
       if (pnl.Visible) //"==true" not required as the expression evaluates to either true or false only
       {
           pnl.Visible = false;
           lnk.Text = "Show Answer";
       }
       else
       {
           pnl.Visible = true;
           lnk.Text = "Hide Answer";
       }
   }

2. You might be noticing a postback when you click on the link button. Put a update panel to do the rendering asynchronously.
3. Instead of handling such small requests in the server (C#) code, do it with Javascript to do the job in the client side only. So, there will be no postback and will show the result in much less time than the C# code.

These are my suggestions. There can be many more.

Thanks
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 31-May-16 22:40pm    
I don' think reusing of the object and other improvements will do much. The main performance loss is traffic between server and client side on every click.
—SA
Suvendu Shekhar Giri 31-May-16 22:41pm    
Agree. Such request should be handled in the client side.
Karthik_Mahalingam 31-May-16 23:46pm    
exactly
It is really slow because you pass all the processing via the server side. Imagine how much time would it take to postback HTTP request on every small and fast clicks, even in best-case scenario, how much of useless traffic it takes.

In this context, such things as calling FindControl("Panel1") again and again looks like a small (but dirty) flaw :-).

By the way, this is unrelated to the performance, but awful for the maintainability of your code: names like "Panel1". Never ever use auto-generated names; they are not intended to be used as is and fail to meed (good) Microsoft naming conventions, by apparent reasons. After all, what to you think you are giving the refactoring engine for? Give everything semantically sensible names.

Now, what to do? Do all UI navigation detail, hiding and showing, toggling, sliding, and everything like that which happens on the same page exclusively using JavaScript. This is not so hard as it may seem. You can prototype all such effects on a purely local HTML+CSS+JavaScript applications and even debug all the code, either via Visual Studio or other debuggers.

For example, visibility manipulations are described in my past answer: How to show and hide div[^].

—SA
 
Share this answer
 
v2
Comments
Suvendu Shekhar Giri 31-May-16 22:26pm    
5ed. Detailed explanation and perfect sugestion.
Sergey Alexandrovich Kryukov 31-May-16 22:38pm    
Thank you, Suvendu Shekhar.
—SA
Mahesh@Dev 2-Jun-16 22:29pm    
Thanks Sergey & Suvendu...I use data-toggle for this concept

data-toggle="collapse" data-target='<%# "#demo" +Container.DataItemIndex%>'

But text of link button "Show Answer" Or "Hide Answer" how can i swap on toggle.
please suggest me..

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900