How do it create/access my own properties for elements in C# that I will use in JS.
and how do I access properties that are avaiable in Html but don't appear to be exposed in the c# set like the border property for tables
I know I can do it with styles and classes, but it seems like a limp around as opposed to the most robust way to do it.
Thanks in advance.
The Attributes property of the WebControl base class is what you're looking for. Example:
MyControl.Attributes["myattr"] = "examplevalue";
The most robust as well as the most correct way of doing it is though CssClass property and a class defined inside a .css file.
One reason to this is that if you have a designer who only touches CSS, they can change styles without touching your C# source code. If you don't have a designated CSS person, layer separation is still beneficial - just imagine looking though source code to change border color.
Separating CSS, source code and JS as much as you can is the advisable practice.
Related
I have created dynamic checkbox with ID="chk" and I want to apply inline style to it. I tried
chk.Attributes.Add("style","opacity:1");
But it doesn't work as expected. It created span around check box and applies style to that.
But it works
chk.InputAttributes["style"] = "opacity:1";
But former works fine for TextBox. Why this complexity for different controls?
Update
As un-lucky answered, it is just an alternative that buries my real question i.e. Why this complexity for different controls?. Lets forget about style, think of any other atrribute. Why check box is different when it comes to Attribute.Add?
To add styles you have to try Style property instead for Attributes Following code will help you:
chkBox.Style.Add("opacity", ".3");
You can also try like this:
chkBox.Style["width"] = "70px";
chkBox.Style["height"] = "70px";
ASP.Net renders two <input> elements for checkboxes to overcome the fact that an unchecked checkbox is not POSTed.
I guess the easier approach is to assign CSS classes in code, and adjust your CSS to deal with the HTML details.
I am creating a custom ChildWindow that I want to use with a DataTemplate.
The DataTemplate will apply to the "body" of the window, but then, separate from that, I want to always display two buttons, "Save" and "Cancel".
I have no idea how to accomplish that... Any help would be greatly appreciated!
Grab a copy of your ChildWindowStyle from your SdkStyles.xaml to give you a foundation for building your custom control template on. To keep a DataContext you could throw it in a UserControl as UserControl.Resources or if you're just populating ContentPresenters etc you can put the template in your own resource dictionary or wherever you like (though you might want to specify a unique x:Key name for it.) Just depends on how you'd like to use it.
Make your desired changes to the template and also add your Buttons etc. Then you can either set it as the default by replacing the Default BasedOn value in your resource dictionary to point to it or call that style explicitly.
Personally I prefer Expression Blend for all of this and there's even some tutorials out there to help you along with a Web Search (which I might suggest first next time.) Like what you might find here... Hope this helps! :)
Is there anyway to access a css class that is located in a css file, and add and remove styles from it.
Edit:
What i am trying to achieve for now, is that i have added a css file "customize", and i have added a css class in it:
.yafheader
{
display:none;
}
I just want to manipulate the display value, that is all.
I believe the simplest solution is to just put the CSS you want in the stylesheet with a new class name, and then at runtime apply that class name to the element(s) you want to modify.
Create two CSS rules - one to display and one not to display. Then create a property of your class for CSS and apply the CSS rule via the property.
Code behind is executed on the server side. So it may be possible to get the CSS class parsed and manipulate it, but you need a plan on how this manipulated object is supposed to be injected into your output. One thing you might do would be to write a new css file with your manipulated class. There might be more convenient ways of doing that however; I believe it might help if you give a bit more context about what you're actually trying to achieve.
you don't want to modify the css file itself because it can be accessed by many users simultaneously. if you change the underlying css file in response to an action by your first site visitor, the next site visitor to load the page will receive the new css file, with styles that reflect the actions of the other user on your site. do as others have suggested and define 2 classes, and add/remove the appropriate classes at runtime using javascript.
I'm looking to hide/show properties depending on what selection the user makes in a drop. I am handling the event fine, but cannot actually make the correct properties disappear. The solutions I have found on line are mainly from 2005 and as I've had very little experience with it I thought I must be doing something wrong if hiding properties is this hard.
So far I have tried accessing the property once the event is handled but the ReadOnly and IsBrowsable properties are read only.
The propertygrid has a property BrowsableAttributes that takes an Attribute list but only works negatively and cannot do or - only and. Providing an attribute collection of ; category - 'test' and isbrowsable - true; returns those that match both and as I can't provide multiple AttributeCollections I cannot make the search specific enough to hide the necessary ones whilst leaving others visible.
I have been banging my head against a wall for the past couple of hours thinking there must be an easier way.
Have you tried applying this attribute to a property:
[Browsable(false)]
public object SomeProperty{
}
In that way SomeProperty will not appear on the propertygrid.
To do what you want to do here, you'd need to implement ICustomTypeDescriptor on your class, which is what the PropertyGrid will use to query your class/object to find out what items to display in the PropertyGrid, how to edit them, what category/description they should have, etc.
It can get quite messy, but it seems that there's a pre-written set of helper classes for you on Code Project at http://www.codeproject.com/KB/grid/PropertyGridDynamicProp.aspx.
I'd started writing my own version of the code given at Code Project and after hitting a snag I did some googling and came up with the Code Project link. It looks like they've done a lot better than I was. I've downloaded the code and it seems to work quite well.
If it doesn't solve your problem, please let me know and I'll dig a bit deeper.
i wonder if there is a way to access a control's templatepart from within c# for modifying the part (e.g. hiding, etc..). is it possible to get a reference to the part with pure c#?
i don't want to touch the controls template.
thanks
j.
It is possible, but its quite nasty.
On the Template there is a method called FindName, which needs two arguments: the name and the FrameworkElement that has the ControlTemplate as Template. Of course, you need to set the name of the element in the ControlTemplate...
Another more elegent solution is to use a Binding in the ControlTemplate to determine the visibility.. That way you do not need to do stuff in your code behind and you can keep it Xaml only...