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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • pechar
    New Member
    • Jan 2008
    • 56

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

    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:
    Code:
    <%#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:

    Code:
    Label lblProductName = (Label) e.Item.FindControl("lblProductName");
    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
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    Well I can tell you that this line:
    Code:
    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:
    Code:
    lblProductName.Text ="Some text";

    Comment

    • krishnabhargav
      New Member
      • Feb 2008
      • 24

      #3
      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:
      Code:
      <%#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:

      Code:
      Label lblProductName = (Label) e.Item.FindControl("lblProductName");
      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

      Comment

      • pechar
        New Member
        • Jan 2008
        • 56

        #4
        Originally posted by Plater
        Well I can tell you that this line:
        Code:
        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:
        Code:
        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.

        Comment

        • pechar
          New Member
          • Jan 2008
          • 56

          #5
          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

          Comment

          • Plater
            Recognized Expert Expert
            • Apr 2007
            • 7872

            #6
            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()

            Comment

            Working...