Resizing things with DataBinder.Eval
-
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
-
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
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
-
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
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
-
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
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. -
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
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!
-
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!
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 -
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, 2005This 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