asp:TextBox - override Text property - asp.net

How do I override the TextBox property "Text" (getter) ?

According to MSDN the property is virtual so you should be able to override like so:
class MyTextBox : TextBox {
public override string Text
{
get { return /* return what you wish*/; }
set { /**/ }
}
}
You can also override one of the Render methods to have full control of generated HTML. Though perhaps you had something else in mind?

Related

How to add superscripts to text for buttons in windows forms?

I am trying to create a virtual keyboard like the one we have in windows 10. I am using buttons to represent all the characters on our keyboard. But how do I put text on keys where there are multiple characters like for example on ? key, we also have a /, so how do I do it?
basically this is what I want to do (from windows 10 virtual keyboard)
but I can only do this as of now:
So how can I add a superscript kind of thing to make my buttons look more professional?
Thanks
You could create a new Button class that extends the System.Windows.Forms.Button.
public class SuperscriptButton : Button
{
[Browsable(true)]
public event EventHandler SuperscriptChanged;
[Browsable(true)]
public string Superscript
{
get
{
return _superScript;
}
set
{
_superScript = value;
OnSuperscriptChanged(EventArgs.Empty);
}
}
private string _superScript;
public SuperscriptButton()
{
TextAlign = System.Drawing.ContentAlignment.BottomRight;
}
protected override void OnPaint(PaintEventArgs pevent)
{
base.OnPaint(pevent);
System.Drawing.Graphics g = pevent.Graphics;
g.DrawString(Superscript, Font, new System.Drawing.SolidBrush(ForeColor), 5, 5);
}
private void OnSuperscriptChanged(EventArgs e)
{
if(SuperscriptChanged != null)
SuperscriptChanged(this, e);
Refresh();
}
}
Now you can use this button in the designer and change the Superscript property in order to change the displayed character.

Not being able to change Title in a Caliburn.Micro Conductor View using MahApps MetroWindow

I'm doing like so:
<Controls:MetroWindow x:Class="BS.Expert.Client.App.Views.ShellView"
xmlns:Controls="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
ShowTitleBar="True"
Title="My Title">
The thing is that this is at the same time a defined main conductor on the main window with which I control navigation through other windows, so I'm not able to inherit from MetroWindow to at least trying change the title in the ViewModel:
public class ShellViewModel : Conductor<IScreen>.Collection.OneActive, IShell
{
public ShellViewModel()
{
#region mahApps Theme Loading
var theme = ThemeManager.DetectAppStyle(Application.Current);
var appTheme = ThemeManager.GetAppTheme(App.Configuration.Theme);
ThemeManager.ChangeAppStyle(Application.Current, theme.Item2, appTheme);
#endregion
//TODO: Changing Title here is not possible ((MetroWindow)this).Title = "No way";
// Tudo bem na seguinte liƱa
LocalizeDictionary.Instance.Culture = new System.Globalization.CultureInfo("pt-BR");
ShowPageOne();
}
public void ShowPageOne()
{
ActivateItem(new PageOneViewModel());
}
}
How should I change the title?
When using the MVVM pattern you should never try to set anything on the view directly in the view model like this. Instead using data binding to accomplish this.
So you would have a property on your ShellViewModel with something like:
public string MyTitle
{
get { return _myTitle; }
set
{
_myTitle = value;
//Insert your property change code here (not sure of the caliburn micro version)
}
}
and in your window xaml it would be something like:
<Controls:MetroWindow
Title="{Binding MyTitle}"
xmlns:Controls="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro"
...
>

Create custom control for a button

I want to create a custom control for a button. However wow can i override the visibility of the button?
For example if the access level of the user is false then it will hide the button.
Here is my coding
public override void Visible()
{//Get access right from session
blcAccessLevel accessLevel = (blcAccessLevel)HttpContext.Current.Session[gbcAccessLevel.sessionAcl];
if (accessLevel.Read_Access == true)
{
base.Visible = true;//error occu
}
else
{
base.Visible = false;
}
}
It return me the error.Error 'blcCustomControl.cusAclReadButton.Visible()': cannot override because 'System.Web.UI.Control.Visible' is not a function By the way, should I implement the get set method?
public class MyButton : Button
{
public new bool Visible
{
get
{
return (blcAccessLevel)HttpContext.Current.Session[gbcAccessLevel.sessionAcl].ReadAccess;
}
}
}

extending asp.net ImageButton to css Sprite

I'm updating my ImageButtons on my asp.net application to a WebControl that extends the ImageButton, which I called CSSImageButton. I've overridden the render methods so I can make them work with css sprites. I've managed to get the OnClick working and the OnClientClick. But some buttons have the OnCommand, and I can't figure out what to do.
public class CSSImageButton : ImageButton
{
public string DivClass { get; set; }
public CSSImageButton()
{
CssClass = "";
}
protected override void Render(HtmlTextWriter writer)
{
base.Render(writer);
}
public override void RenderBeginTag(HtmlTextWriter writer)
{
writer.Write("<div class=\"" + CssClass + " " + DivClass + "\">");
}
protected override void RenderContents(HtmlTextWriter writer)
{
writer.Write("<a id=\"" + this.ClientID + "\" title=\""+ this.ToolTip+"\"");
if (!String.IsNullOrEmpty(OnClientClick))
{
writer.Write(" href=\"javascript:void(0);\" OnClick = \"" + OnClientClick + "\"");
}
else
{
writer.Write(" href=\"javascript:" + this.Page.GetPostBackEventReference(this, "ButtonClick") + "\"");
}
writer.Write("\"></a>");
}
public override void RenderEndTag(HtmlTextWriter writer)
{
writer.Write("</div>");
}
}
Alternatively, you can replace ImageButton with LinkButton, and have a div inside of it with proper class for css-sprites
The Source
In the property ImageUrl of your ImageButton, you could place a link to a transparent image (which has a size of 1x1 pixel, for example: spacer.png).
Then in your CSS you place a background image on it, with a width and height.
So at any place where you use an ImageButton, you call the same transparent image (which remain in the browsers cache).
#roman m: The issue with this approach (depending on your situation), is that this will end up nesting a div within an anchor ("bar") which will not pass HTML validation.
Granted, if that is not a concern, then what you suggest will solve the problem, however Etienne's solution is more "natural" from a markup / validation perspective.
Just my $.02

Possible to have a inner control on a custom server control?

I would like to be able to do something like:
<ui:Tab Title="A nice title">
<TabTemplate>
<asp:Literal runat="server" ID="SetMe">With Text or Something</asp:Literal>
</TabTemplate>
</ui:Tab>
but also be able to do:
<ui:Tab Title="A nice title">
<TabTemplate>
<asp:DataList runat="server" ID="BindMe"></asp:DataList>
</TabTemplate>
</ui:Tab>
Answer code I eventually came up with:
[ParseChildren(true)]
public class Node : SiteMapNodeBaseControl, INamingContainer
{
private ITemplate tabTemplate;
[Browsable(false),
DefaultValue(null),
Description("The tab template."),
PersistenceMode(PersistenceMode.InnerProperty),
TemplateContainer(typeof(TabTemplate))]
public ITemplate TabTemplate
{
get { return tabTemplate; }
set { tabTemplate = value; }
}
protected override void CreateChildControls()
{
if (TabTemplate != null)
{
Controls.Clear();
TabTemplate i = new TabTemplate();
TabTemplate.InstantiateIn(i);
Controls.Add(i);
}
}
protected override void Render(HtmlTextWriter writer)
{
EnsureChildControls();
base.Render(writer);
}
}
public class TabTemplate : Control, INamingContainer
{
}
The ParseChildren attribute tells .NET whether to treat your control's children as properties or as controls. For your first example, you want to treat children as controls, so add
[ ParseChildren(ChildrenAsProperties = false) ]
For the second, you want ChildrenAsProperties=true, and a TabTemplate property of type ITemplate. There's some plumbing involved after that, which this MSDN sample describes. It doesn't add a lot of value if you only need one template, though.

Resources