Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Resizing things with DataBinder.Eval

Resizing things with DataBinder.Eval

Scheduled Pinned Locked Moved ASP.NET
csharpjavahtmlasp-netvisual-studio
7 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Stefan R
    wrote on last edited by
    #1

    I am trying to set the size of an imagebutton dynamically through databound values. Is there any way to make this work: height='<%# DataBinder.Eval(Container.DataItem, "thumb_width") %>' The above line is from the html-page with the proper code preceding it (using Visual Studio..). I get a "Specified cast is not valid" message when i try this. Why is this happening? The databound value is "100px" and i have tried "100" as well, but nothing works. I'm really thinking about going back to Java again, I suck at asp.net;) -- modified at 18:40 Thursday 8th December, 2005

    D M 2 Replies Last reply
    0
    • S Stefan R

      I am trying to set the size of an imagebutton dynamically through databound values. Is there any way to make this work: height='<%# DataBinder.Eval(Container.DataItem, "thumb_width") %>' The above line is from the html-page with the proper code preceding it (using Visual Studio..). I get a "Specified cast is not valid" message when i try this. Why is this happening? The databound value is "100px" and i have tried "100" as well, but nothing works. I'm really thinking about going back to Java again, I suck at asp.net;) -- modified at 18:40 Thursday 8th December, 2005

      D Offline
      D Offline
      Daniel Santillanes
      wrote on last edited by
      #2

      you're sure thumb_width is a varchar / char / string value? if it is, it should work. height is a property for what control? a asp.net web control? a css style definition? daniero

      S 1 Reply Last reply
      0
      • D Daniel Santillanes

        you're sure thumb_width is a varchar / char / string value? if it is, it should work. height is a property for what control? a asp.net web control? a css style definition? daniero

        S Offline
        S Offline
        Stefan R
        wrote on last edited by
        #3

        i'm using an access db so the value is set to text. I believe it's the same as varchar and the others. The control i want to manipulate is an asp:linkbutton, see below: asp:ImageButton ImageUrl='<%# DataBinder.Eval(Container.DataItem, "path") %>' width='<%# DataBinder.Eval(Container.DataItem, "thumb_width") %>' Runat=server -- modified at 19:02 Thursday 8th December, 2005

        D 1 Reply Last reply
        0
        • S Stefan R

          i'm using an access db so the value is set to text. I believe it's the same as varchar and the others. The control i want to manipulate is an asp:linkbutton, see below: asp:ImageButton ImageUrl='<%# DataBinder.Eval(Container.DataItem, "path") %>' width='<%# DataBinder.Eval(Container.DataItem, "thumb_width") %>' Runat=server -- modified at 19:02 Thursday 8th December, 2005

          D Offline
          D Offline
          Daniel Santillanes
          wrote on last edited by
          #4

          ok, you could do this in code behind, and I'm sure it would work, I believe you are working with a DataGrid / DataList / Repeater, so when you call the databind function for the control, it starts to generate a ItemCreated and then a ItemDataBound event for each item in the control. Inside ItemDataBound event, you can do something like this: Private Sub dgServidores_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles dgServidores.ItemDataBound Select Case e.Item.ItemType Case ListItemType.Item, ListItemType.AlternatingItem Dim lbButton as LinkButton Dim sThumbWidth as string lbButton = CType(e.Item.FindControl("lbButton"), LinkButton) sThumbWidth = CType(DataBinder.Eval(Container.DataItem, "thumb_width"), String) lbButton.Height = New Unit(sThumbWidth) End Select End Sub Let's see if this helps some how. This way is also easier to debug because you can use breakpoints in the events to watch what you're doing. daniero -- modified at 19:40 Thursday 8th December, 2005 Just a quick reminder... in the following line of code: lbButton = CType(e.Item.FindControl("lbButton"), LinkButton) "lbButton" refers to the id of your link button in the aspx page.

          1 Reply Last reply
          0
          • S Stefan R

            I am trying to set the size of an imagebutton dynamically through databound values. Is there any way to make this work: height='<%# DataBinder.Eval(Container.DataItem, "thumb_width") %>' The above line is from the html-page with the proper code preceding it (using Visual Studio..). I get a "Specified cast is not valid" message when i try this. Why is this happening? The databound value is "100px" and i have tried "100" as well, but nothing works. I'm really thinking about going back to Java again, I suck at asp.net;) -- modified at 18:40 Thursday 8th December, 2005

            M Offline
            M Offline
            minhpc_bk
            wrote on last edited by
            #5

            Hi there, Because the height[^] property is of the Unit type, so you cannot bind a value of string to it. In this case, you simply need to edit the data binding expression a bit:

            width='<%# Unit.Parse((string)DataBinder.Eval(Container.DataItem, "thumb_width"))%>'

            Stefan R wrote:

            I'm really thinking about going back to Java again, I suck at asp.net

            Hopefully, CodeProject makes you reconsider your decision ;P!

            S 1 Reply Last reply
            0
            • M minhpc_bk

              Hi there, Because the height[^] property is of the Unit type, so you cannot bind a value of string to it. In this case, you simply need to edit the data binding expression a bit:

              width='<%# Unit.Parse((string)DataBinder.Eval(Container.DataItem, "thumb_width"))%>'

              Stefan R wrote:

              I'm really thinking about going back to Java again, I suck at asp.net

              Hopefully, CodeProject makes you reconsider your decision ;P!

              S Offline
              S Offline
              Stefan R
              wrote on last edited by
              #6

              Thanks a lot guys! You really helped me. I had my suspicions about that Unit thing but I didn't think I could perform type-casting in the html-page. Just one more question: The code between the "<%#...%>" is taken care of by the server. Does this mean that I can use C# code here in the same way as in the code-behind? About that java-thing, let's just say that Mr Gates and his cronies know how to make things easier (I'm a veteran, diet-version, at Java). No way I'm going back now. But you'll probably hear from me again - I'm learning by trial and error and a great deal asking/searching/doing;P -- modified at 8:59 Friday 9th December, 2005

              M 1 Reply Last reply
              0
              • S Stefan R

                Thanks a lot guys! You really helped me. I had my suspicions about that Unit thing but I didn't think I could perform type-casting in the html-page. Just one more question: The code between the "<%#...%>" is taken care of by the server. Does this mean that I can use C# code here in the same way as in the code-behind? About that java-thing, let's just say that Mr Gates and his cronies know how to make things easier (I'm a veteran, diet-version, at Java). No way I'm going back now. But you'll probably hear from me again - I'm learning by trial and error and a great deal asking/searching/doing;P -- modified at 8:59 Friday 9th December, 2005

                M Offline
                M Offline
                minhpc_bk
                wrote on last edited by
                #7

                This is the syntax of the data binding expression, you normally place an expression using the DataBinder.Eval method in there, the expression can be a call to a method defined in code-behind. The result of the expression is bound to the specified property of the element/control, and this binding only happens when you call the DataBind method of either the Page instance or the data-bound control like datagrid, datalist, repeater ....For more information, you can see the documentation in MSDN: Data Binding Expression Syntax[^] ASP.NET Syntax[^]

                Stefan R wrote:

                About that java-thing, let's just say that Mr Gates and his cronies know how to make things easier (I'm a veteran, diet-version, at Java). No way I'm going back now. But you'll probably hear from me again - I'm learning by trial and error and a great deal asking/searching/doing

                Just remind you that CP is your best friend :-D

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups