Connecting Tech Pros Worldwide Help | Site Map

C#:ASP.NET FindControl() in .cs v.s. Eval("column_name") in .aspx

  #1  
Old February 7th, 2008, 11:52 AM
Member
 
Join Date: Jan 2008
Posts: 56
Hi all,

First and foremost I am a person who hates to add C# code to aspx file and prefer using only codebehind. I know there are certain scenarios where it is impossible to evade but thats another matter.

I have a repeater with a DataView as a DataSource. In the repeater I display product details from the columns in the dataview. Now there are two ways I tend to use to display data like say the product name:

in the aspx file just add:
Expand|Select|Wrap|Line Numbers
  1. <%#Eval("product_name")%>
or
have a label (lblProductName) in the aspx file and in the cs file on Item_Databound I use FindControl as follows:

Expand|Select|Wrap|Line Numbers
  1. Label lblProductName = (Label) e.Item.FindControl("lblProductName");
  2. lblProductName.Text = Convert.ToString(((DataRowView)e.Item.DataItem)["product_name"]);
  3.  
I personally prefer the second though there is more work involved. It is also good for me since usually I format the data before I display it.

Now my question is: Which method is better keeping in mind speed/overhead?? Or are they identical?


Thanks
  #2  
Old February 7th, 2008, 02:51 PM
Plater's Avatar
Moderator
 
Join Date: Apr 2007
Location: New England
Posts: 7,096
Provided Answers: 3

re: C#:ASP.NET FindControl() in .cs v.s. Eval("column_name") in .aspx


Well I can tell you that this line:
Expand|Select|Wrap|Line Numbers
  1. Label lblProductName = (Label) e.Item.FindControl("lblProductName");
  2.  
is redundant and not required.

If you have a control in your asp page called (ID property) "lblProductName" and the runat = "server", then the control instance is already available to you in your codebehind page.
You could just say:
Expand|Select|Wrap|Line Numbers
  1. lblProductName.Text ="Some text";
  2.  
  #3  
Old February 8th, 2008, 04:41 AM
Newbie
 
Join Date: Feb 2008
Posts: 24

re: C#:ASP.NET FindControl() in .cs v.s. Eval("column_name") in .aspx


Quote:
Originally Posted by pechar
Hi all,

First and foremost I am a person who hates to add C# code to aspx file and prefer using only codebehind. I know there are certain scenarios where it is impossible to evade but thats another matter.

I have a repeater with a DataView as a DataSource. In the repeater I display product details from the columns in the dataview. Now there are two ways I tend to use to display data like say the product name:

in the aspx file just add:
Expand|Select|Wrap|Line Numbers
  1. <%#Eval("product_name")%>
or
have a label (lblProductName) in the aspx file and in the cs file on Item_Databound I use FindControl as follows:

Expand|Select|Wrap|Line Numbers
  1. Label lblProductName = (Label) e.Item.FindControl("lblProductName");
  2. lblProductName.Text = Convert.ToString(((DataRowView)e.Item.DataItem)["product_name"]);
  3.  
I personally prefer the second though there is more work involved. It is also good for me since usually I format the data before I display it.

Now my question is: Which method is better keeping in mind speed/overhead?? Or are they identical?


Thanks
Enable Tracing and do a little performance test yourself .... Tracing gives lots and lots of information
  #4  
Old February 8th, 2008, 07:42 AM
Member
 
Join Date: Jan 2008
Posts: 56

re: C#:ASP.NET FindControl() in .cs v.s. Eval("column_name") in .aspx


Quote:
Originally Posted by Plater
Well I can tell you that this line:
Expand|Select|Wrap|Line Numbers
  1. Label lblProductName = (Label) e.Item.FindControl("lblProductName");
  2.  
is redundant and not required.

If you have a control in your asp page called (ID property) "lblProductName" and the runat = "server", then the control instance is already available to you in your codebehind page.
You could just say:
Expand|Select|Wrap|Line Numbers
  1. lblProductName.Text ="Some text";
  2.  
Hi Plater,

I tried your way but I'm sorry to say I think you're wrong there. The project wont even compile.

Since I am using a repeater I have to use the EventArgs Item to find the control I want to modify. You could have like a 100 items in the repeater so you have to specify which control properties you are changing.

Where it to be out of a repeater your method would work obviously.

Thanks for the reply though.
  #5  
Old February 8th, 2008, 07:43 AM
Member
 
Join Date: Jan 2008
Posts: 56

re: C#:ASP.NET FindControl() in .cs v.s. Eval("column_name") in .aspx


Quote:
Originally Posted by krishnabhargav
Enable Tracing and do a little performance test yourself .... Tracing gives lots and lots of information
Hi krishnabhargav,

could you give me some tips on how I can perform these tests? I've never used tracing and would really like to try it.

Thanks
  #6  
Old February 8th, 2008, 04:55 PM
Plater's Avatar
Moderator
 
Join Date: Apr 2007
Location: New England
Posts: 7,096
Provided Answers: 3

re: C#:ASP.NET FindControl() in .cs v.s. Eval("column_name") in .aspx


Quote:
Originally Posted by pechar
Hi Plater,

I tried your way but I'm sorry to say I think you're wrong there. The project wont even compile.

Since I am using a repeater I have to use the EventArgs Item to find the control I want to modify. You could have like a 100 items in the repeater so you have to specify which control properties you are changing.

Where it to be out of a repeater your method would work obviously.

Thanks for the reply though.
Well I do it in all of my pages. So I don't know what you are doing different to break it, but I rather like the lack of overhead of searching for a control everytime I want to use it.

EDIT: Missed the part about the repeater. Still, unsure why a member was not generated if it has a name that can be found with FindControl()
Reply