get aspxcombobox css class name - c#

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;

Related

Add new attribute to "ColumnSeries" tag in RadHtmlChart (Telerik)

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.

Get HiddenField in JQuery inside Repeater

I have a Repeater control, and I need to access a HiddenField inside. I can't use:
<%= Control.ID.ClientID %>
And I can't use the class or cssclass attributes either?
So my question is rather simple, how can I access my HiddenField control inside my repeater?
My scenario is that I populate a multiselectable dropdown, and I need to know in an update function which elements I have selected, for this I use the HiddenField to store the Id's. Then in code behind I can access the HiddenField values and make a propor databind.
HiddenField control elements are rendered to inputs of type hidden, so, albeit not thoroughly understanding your vague scenario with limited application, you can access them in jQuery with a selector like so:
$("input[type=hidden]")
Depending on your situation you might want to constrain that selector yet more.
However, this is focusing on your inclusion of the jquery tag, though your example seems to want to use inline ASP.NET script to use managed code. Please clarify your intentions and ultimate goal.
You can use the class selector
Put a class over the hidden field.
And on change event of multiselectable find the appropriate hidden field and set it's value.
Edit-1
I will suggest you to put a class say ddl over the multiselectable dropdown.
Bind jquery on change method on this multiselect.
Using jquery parent and prev selectors you can set the values in the hidden field.
Exmaple
$(".ddl").change( function(){
$(this).parent().next().find("input:hidden").val($(".ddl").val());
});
This is just for and idea of my approach.
Edit 2
Here is a good example of how to use css-class over a hidden field.
Jquery Hidden Field
I found a solution which in fact is pretty easy :)
I used the ClientIDMode property on the HiddenField inside the repeater:
<asp:HiddenField ID="HiddenField_Pladser" runat="server" Value="Selected" ClientIDMode="Static" />
The ClientID value is set to the value of the ID property. Then it's easy in the JQuery script to access the value:
$("#HiddenField_Pladser").val();
And from the code behind I know the ItemIndex from the repeater, which makes it easy to create a data collection whit this code:
var data = from RepeaterItem item in rpPladser.Items
let hidden = ((HiddenField)item.FindControl("HiddenField_Pladser"))
select new
{
selected = hidden.Value
};
Please leve a comment if this implementation have flaws or somehow is bad.

is there a way asp.net to trigger whether to display a fieldset?

I am creating a program with asp.net c#, i am using multiple gridviews to show different criteria of data on 1 page, each of these criteria has a title,
Now, if one of the gridviews happens to have no data it will not show, however the title of the gridview will still be there as it is purely html fieldset and legend.
Are there any functions i could use to trigger the visibility of the title from the c# code?
Thanks
(screenshot below)
You can add runat="server" to fieldset and set Visible = true or false as usual (of course you need to set an ID, too).
You can surround each grid in an <asp:Panel... with a visibility on the panel set to a server based property (e.g. Visibility=<%# RecordsReturnedGreaterThanZero %>
so that the panel will only render the contents if there are some records returned in your query. Simply provide a property in your codebehind that acts as the boolean operator

Content within a user control

I am almost too embarrassed to ask this question, but here we go...
I am not an expert with user controls, and need some design advise regarding trying to achieve a specific desired functionality.
The goal is to have a usercontrol that would render as a complex structure of html elements and css to form an elegant container box. The problem lies in how to populate the contents of the box as each instance of the usercontrol would have its own individual HTML content. The content container div of the usercontrol would be nested deep within the structure of the rendered html. It is undesirable to programmatically set the contents of the usercontrol, or use properties.
In psuedo-code, the desired syntax would be something like:
<usercontrol Title="Some Title"><p>some random html content</p></usercontrol>
A sample of the rendered usercontrol would be:
<div class="CommonBox">
<div class="Title">Some Title</div>
<div class="Content"><p>some random html content</p></div>
</div>
I hope my explanation is adequate. Does this make sense to anyone or is the desired functionality unachievable?
Cheers!
EDIT
I attempted the templated user control suggestion in the hopes of coming to a decent solution. I "waybacked" this site and now have a working templated user control. My next question is, how can I programmatically access the controls nested within the template... say there is a textbox control in the "Description" template from the example link and I want to set its value programmatically? Is this possible?
What you're looking for is definitely possible. One solution is to create a templated user control. In the end you can define the contents similar to how your example looks. Something like:
<uc:MyControl Title="Some TItle" runat="server">
<ContentsTemplate>
<p>some random html content</p>
</ContentsTemplate>
</uc:MyControl>
Here's a simple how-to. I've done this in the past with success. There are lots of resources found through Google as well on the topic.
Yes this is very possible. First you can create a custom control as I describe in this post. Properties in the control become attributes like "Title" you have in your example. Then you can extend your control with the technique shown in this post to add child nodes for the other html inputs you will require.
Enjoy!
It looks like you need two properties for your custom server control.
Title property to render first inner div's content,
Content for the second one. If you want to set contents inside your server control, you should use [PersistenceMode(PersistenceMode.InnerDefaultProperty)] attribute for your property, like this :
[PersistenceMode(PersistenceMode.InnerDefaultProperty)]
public string Contents
{
get
{
string contents = (string)ViewState["Contents"];
return (contents == null) ? String.Empty : contents;
}
set
{
ViewState["Contents"] = value;
}
}
PersistenceModeAttribute is explained in MSDN like that :
PersistenceModeAttribute Passing the
InnerDefaultProperty parameter
specifies that a visual designer
should persist the property to which
the attribute is applied as an inner
default property. This means that a
visual designer persists the property
within the control's tags. The
attribute can be applied to only one
property, because only one property
can be persisted within the control's
tags. The property value is not
wrapped in a special tag.
For more information take a look at the last example here.

ASP.NET Get Original Text of Label

Is there any way to get the original text from a Asp:Label on a page, after the text has been altered?
with the orginal text i mean the text that is hard coded into the asp.net markup.
There is no standard way to get it back after some chages, but you could do that in some your ways, for example add custom attributes to label
textLabel.Attributes.Add("data", textLabel.Text);
and then use it on your page.
Or cache label value using js code on page startup or statically.
Create a property that wraps the text assignment. Before the assignment take the current value and assign it to a hidden input or stick it in the session or viewstate.
Create a property that retrieves the previous value from the hidden input or session or viewstate.
Could get fancy and extend the label to add a PreviousValue property. Not sure how this would work in practice though.

Categories