binding long to user control property - c#

I seem to have run across an issue with binding and I have looked everywhere to try to resolve it.
I have a user control in ASP.NET that has a Channel ID public property which is a long. I have added this user control to an empty ASPX page for testing. When I simply add a number for this property value (i.e. ChannelID="9") then it works perfectly. When I try to do the following:
ChannelID="<%#: ChannelID %>"
where ChannelID is a variable on the hosting ASPX page, it tells me it cannot convert from string to long as a compilation error.
What I have gathered so far through my research is that the data binding functions convert values to strings rather than retaining their original data types.
What I am trying to accomplish is to have multiple user controls in a single page that all bind a property through a shared interface to the same value of the host page. I know that I could do this in code behind but that is what I am trying to avoid. I also know that I could simply change the data type to string and then convert it before it is sent from the user control to the database....that will be the route I go if I cannot find another solution.
There is not really code I can post because the scenario is extremely basic.But here is the declaration of the user code on the host page:
<UC:ChanControl ID="ChanControl1" runat="server" IsActiveView="true"
DisplayMode="Normal" ChannelID="<%#: ChannelID%>" />
Again, the ChannelID property in the host page and the user control are both set to long.
Any suggestions would be greatly appreciated. Thanks.

try this ChannelID='<%#: Eval(ChannelID) %>'
or
ChannelID='<%#: Eval(ChannelID).ToString() %>'

Related

Passing data to control from kentico transformation

I am trying to pass the value of a custom table field that is retrieved via a transformation in kentico cms to a custom user control.
To explain my setup, inside my master file I have a QueryUniView control that calls the transformation "SelectedStoreInformation". Inside of this transformation I have markup and I am able to print out the field I am looking for just fine using:
<%# Eval("StoreCodeName") %>
However I am in need to pass this value to a custom user control and want to be able to check the value in the code behind. So I proceeded to add a property to the code behind and set this property on the server tag itself assuming that the value would pass through.
Property on custom control code behind:
public string StoreName { get; set; }
Server tag include (this is in the transformation to pull in the custom control):
<cms:Hours ID="Hours" StoreName='<%# Eval("StoreCodeName") %>' runat="server" />
However the StoreName is null in the code behind. If I change the value of StoreName on the tag to just a static string of text it works fine.
My confusion is why does the Eval of the field work when I place it right above the server tag but does not work in the server tag?
Thanks for any help, sorry for the long question.
Kentico v6
It's not that the Eval does not work in the server tag - it's probably just evaluated later. Check that you don't access the property too early. I suggest to use its value in OnDataBinding or OnPreRender.

What could cause this ASP.NET tag to be treated as a string?

I recently started working with some legacy ASP.NET stuff, and I've run into an interesting problem:
I've got a table displaying a few values, some of which are evaluated in C# server-side. They all work correctly. Then all of a sudden...
<td><asp:Label ID="Label2" runat="server" class='<%=SevenDayThresholdTooHighOrLow%>'><%=ChgFromSevenDaysAgoInfo%></asp:Label></td>
ChgFromSevenDaysAgoInfo is evaluated properly.
SevenDayThresholdTooHighOrLow is rendered as a string inside of the class quotations. That is
class="<%=SevenDayThresholdTooHighOrLow%>".
In the code-behind file, the two variables are declared in the same scope, and assigned values pretty much one after the other. My Visual Studio doesn't complain about not finding certain variables in code like it would if the property did not exist.
What other factors could be influencing this oddity? What could I have missed?
Thank you very much for your help, everyone!
Eli
EDIT: I took a look at what is the use of Eval() in asp.net and tried to set my tag up that way (class='<%# Eval("SevenDayThresholdTooHighOrLow")%>'), unfortunately to no effect.
That class attribute probably isn't being evaluated for server-side code, likely because class isn't a property of Label. (Label isn't an HTML element, it's a server-side control. So instead of HTML attributes it uses object properties.)
There's a CssClass property, you might try that instead. But even then the approach is different because the system may still not attempt to evaluate server-side code injected into already server-side components. Still, worth a try.
What should definitely work is setting the CssClass property in the code-behind:
Label2.CssClass = SevenDayThresholdTooHighOrLow;
If you want to keep this out of code-behind (and who could blame you?) then another approach could be to replace the server-side control with an HTML element entirely. The idea being that if properties on this control aren't otherwise being set in server-side code, then does it really need to be a server-side control? If I remember correctly, a Label emits as a span:
<span class="<%=SevenDayThresholdTooHighOrLow%>"><%=ChgFromSevenDaysAgoInfo%></span>
You can't do that in just the markup code. You can't use server tags in a server control. You could use a data binding tag, but then you would need to trigger the data binding from the code behind anyway.
Just set the attribute from the code behind:
Label2.CssClass = SevenDayThresholdTooHighOrLow;

Connect UserControl to a Datasource (ObjectDataSource in my case)

I'm trying to find a way to Databind a usercontrol to a datasource (an
ObjectDataSource in my case) like in the case below :
<uc1:AutoComplete ID="autoComCities" runat="server"
DataSourceID="objCitiesDS" DataValueField="Id" DataTextField="Name" />
I've try to find some good exemple with our lil' friend Google and I still
ain't find anything concret. Someone can show me or give me good links on
how I could do this. I mean, about how to handle it in my usercontrol ;o)
Thanks!
Edit
Note that in my case, it's not a Dropdownlist or any Bindable control in my usercontrol, its a normal asp.Net Textbox with some custom jQuery so when user type in the textbox, it shows "possibilities" under the textbox. So, I want to get values from the DataSrouce and put it into HTML so jQuery can Handles values and shows it to user.
You can't just use the ObjectDataSource, there is a lot of plumbing to get that to work. You can pass the ID and call the Select() method to perform the selection manually and then get the values from the data and render them out. You'll have to use reflection or DataBinder.GetPropertyValue method to extract the individual field values within each record.
HTH.

Validators and ClientIDMode issue (ASP.NET)

ASP.NET 4.0 has introduced the property 'ClientIDMode', which allows one to specify how the html element's ids are rendered in the output html.
My project is currently set to 'Static' (the shortest / contains no hierarchy id's)
When using any validator (RequiredFieldValidator, RegularExpressionValidator...), unless the control it's validating explicitly has it's property ClientIDMode:AutoID I get the error:
Input parameter 'controlId' cannot be
an empty string.
Is this the expected behaviour?
Edit: Being abit dumb with the error message, it wanted me to put an id on the Validator, which I obviously dont do (unless I need to reference it).
It still seems odd that it should fail because of this reason.
I ran into this in a slightly different way: if my user control had ClientIDMode="Static", I received the error on the validators that existed within the control (they did not all have ID's).
One workaround is to switch ClientIDModes on the offending user control. Hope this helps!

foreach in ASP.net with nested runat="server"

For the following code:
<% foreach (Entities.Core.Location loc in locations){ %>
<div class="place_meta">
<img src="~/static/images/stars/star_25_sml.gif" runat="server" class="star_rating"/>
</div>
<% }; %>
I would like to display the star rating image for each location object displayed. However, only the first location object's star rating is displayed. For the rest, the image tag becomes <img class="star_rating" />
Am I missing anything in the syntax that allows the ability to have controls with runat=server within a foreach on the aspx page? This is with ASP.net 2.0.
I could possibly call a function in the codebehind or a display class to absolute map the URL but I am very curious if there is a solution to this problem.
Just for clarifications, the path to the image could possibly be different for each location object.
You can't use server controls within an inline loop, because ASP.NET needs to be able to uniquely identify each control to process it. The for... loop prevents this. The easiest and cleanest way is to use a Repeater control and bind your collection to it (in code-behind). Set the URL property in the bind event handler, and maintain a counter variable while binding, to check if you're at the first item or not.
Edit: I've played around with this some more, and found that if you assign an ID attribute, all looped instances get the same ID, but ASP.NET only recognizes the first control found. This would be an extremely dirty way to set properties on the first instance only. Please don't write code like this.
Unless you're using MVC, you might find a Repeater Control more useful in this situation.
If I remember correctly, you can use a source of data (your locations in this instance) and then loop through and set each image.
I don't know if your code is supposed to work, but this one is ;-)
<asp:Image ImageUrl="~/static/images/stars/star_25_sml.gif" runat="server" class="star_rating" />
Perhaps it's what you are looking for.
EDIT: looking at the code of System.Web.UI.HtmlControls.HtmlImage you can see that the src attribute is removed after the (first) processing. This may be the reason why you can get just the first URI correct with your code.
I would remove the runat="server" attribute and reference the image using a relative path (or the full path) instead of using the tilde.
EDIT: FWIW, #miies idea fits the ASP.NET paradigm AND is an example of why I'm switching to ASP.NET MVC.

Categories