Sign In | Register Now About Bytes | Help | Site Map
Connecting Tech Pros Worldwide

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

Question posted by: pechar (Member) on February 7th, 2008 10:52 AM
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"]);


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
Plater's Avatar
Plater
Moderator
5,573 Posts
February 7th, 2008
01:51 PM
#2

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

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

Reply
krishnabhargav's Avatar
krishnabhargav
Newbie
24 Posts
February 8th, 2008
03:41 AM
#3

Re: C#:ASP.NET FindControl() in .cs v.s. Eval("column_name") in .aspx
Quote:
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"]);


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

Reply
pechar's Avatar
pechar
Member
53 Posts
February 8th, 2008
06:42 AM
#4

Re: C#:ASP.NET FindControl() in .cs v.s. Eval("column_name") in .aspx
Quote:
Well I can tell you that this line:
Expand|Select|Wrap|Line Numbers
  1. Label lblProductName = (Label) e.Item.FindControl("lblProductName");

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


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.

Reply
pechar's Avatar
pechar
Member
53 Posts
February 8th, 2008
06:43 AM
#5

Re: C#:ASP.NET FindControl() in .cs v.s. Eval("column_name") in .aspx
Quote:
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

Reply
Plater's Avatar
Plater
Moderator
5,573 Posts
February 8th, 2008
03:55 PM
#6

Re: C#:ASP.NET FindControl() in .cs v.s. Eval("column_name") in .aspx
Quote:
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
Reply
Not the answer you were looking for? Post your question . . .
190,081 Experts ready to help you find a solution.
Sign up for a free account, or Login (if you're already a member).

Latest Articles: Read & Comment
Top .NET Forum Contributors