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.
Related
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() %>'
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;
I would like to know how to add custom attribute to the ColumnSeries attribute of RadHtmlChart control from Telerik?
I then want to access this attribute in code behind of asp.net page to set the Value but I don't to show the value of CustomeAttribute to RadHtmlChart on the asp.net Page.
<telerik:ColumnSeries CustomeAttribute=""></telerik:ColumnSeries>
You can't. Server properties are strictly defined in the control's class and adding your own that the code will not recognize is likely to result in a server error.
If you need information on the client you can store it in hidden fields, hidden panels or inject a custom JavaScript object from the server via the ScriptManager.RegisterStartupScript() method.
Im adding dynamically some aspxcombobox controls to my page. Each time I add one I add specified cssclass to it. Is it then possible to get that controls cssclass from javascript ?
Thanks for any hints
the CssClass is the class field on an html element. So to get it from javascript easily
document.getElementById("MyElement").className;
If the editor is inside a NamingContainer, for example, WebUserControl, or TabControl or any template, I would suggest that you set the editor's ClientInstanceName property. This property was specially designed to allow our users access the corresponding client side java script objects. For example, if the ClientInstanceName is set to the combo value, the code would be:
var class = combo.GetMainElement().className;
I have a page that I need to allow a user to edit, then I parse the information into HTML and save it to a MS SQL 2008 R2 database. I need to then add this information to an announcements page from the items contained in the database.
I am using C#, so the question is how would I do this? I have a div specifically for the content. Also, is this the best way to allow a user to manage content if I cannot use a cms ( this question is not so vital as I know it is prob more complicated than I realize)?
You can use an asp:Literal control to insert the HTML on the page via the "Text" property on the server-side:
Markup:
<asp:Literal ID="litAnnouncement" runat="server" />
Code-behind:
string htmlAnnouncement = GetHtmlFromDB(); // Get the HTML however you need to as a string.
litAnnouncement.Text = htmlAnnouncement;
I would put the code above somewhere in one of the Load or Init events, either for the page or the Literal control. Of course, there are other ways to do this, but I think this is the most straight-forward given your description.
I didn't get your question completely. but if you want to save data from an HTML element into the SQL data base, then you can add a Sqldatasource control to your page, and then define it with a parameter in the source code.