How can the child controls be created in server controls?
Child controls can be created by overridding the CreateChildControl method as below using System; using System.Collections.Generic; using System.ComponentModel; using System.Text; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; namespace WebCustomControl1 { [DefaultProperty(“Text”)] [ToolboxData(“<{0}:WebCustomControl1 runat=server> </{0}:WebCustomControl1>”)] public class WebCustomControl1 : WebControl { public TextBox txtname = new TextBox(); public TextBox txtnum = new TextBox(); [Bindable(true)] [Category(“Appearance”)] [DefaultValue(“”)] [Localizable(true)] public string Text { get { String s = (String)ViewState[“Text”]; return ((s == null) ? String.Empty : s); } set { ViewState[“Text”] = value; } } protected override void CreateChildControls() { Controls.Add(new LiteralControl(“Name: “)); Controls.Add(txtname); txtname.Text = “”; txtname.ToolTip = “Enter a Username”; txtname.Attributes.Add(“autocomplete”, “off”); Controls.Add(new LiteralControl(“</br>”)); Controls.Add(new LiteralControl(“EmpNo: “)); this.txtnum = new TextBox(); Controls.Add(txtnum); txtnum.Text = “”; txtnum.ToolTip = “Enter a Username”; txtnum.Attributes.Add(“autocomplete”, “off”); Controls.Add(new LiteralControl(“</br>”)); } protected override void RenderContents(HtmlTextWriter writer) { this.EnsureChildControls(); this.RenderChildren(writer); } } }
How can I stamp some text on an image?
The DrawingVisual can be used with a ‘BitmapImage’ instance and then rendered using ‘RenderBitmap’ class for creating custom rendered images. If text needs to be added, then use the ‘FomattedText’ instance. The following code snippet shows how to render an image with a text. [C#] BitmapImage bitImage = new BitmapImage() { UriSource = new Uri(@’C:\Documents and Settings\All Users\Documents\My Pictures\Sample Pictures\Winter.jpg’, UriKind.Relative), DecodePixelWidth = 200, DecodePixelHeight = 200 }; FormattedText text = new FormattedText(‘Winter’, new CultureInfo(‘en-us’), FlowDirection.RightToLeft, new Typeface(‘Calibri’, FontStyles.Normal, FontWeights.Normal, new FontStretch()), this.FontSize, Brushes.White); DrawingVisual drawingVisual = new DrawingVisual(); DrawingContext drawingContext = drawingVisual.RenderOpen(); drawingContext.DrawImage(bitImage, new Rect(0, 0, bitImagebi.Width, bitImagebi.Height)); drawingContext.DrawText(text, new Point(bitImage.Height / 2, 0)); drawingContext.Close(); RenderTargetBitmap renderBmap = new RenderTargetBitmap(bitImage.PixelWidth, bitImage.PixelHeight, 96, 96, PixelFormats.Pbgra32); renderBmap.Render(drawingVisual); // Encoding the RenderBitmapTarget as a PNG file. PngBitmapEncoder png = new PngBitmapEncoder(); png.Frames.Add(BitmapFrame.Create(rtb)); using (Stream stm = File.Create(‘new.png’)) { png.Save(stm); }
How can I create and use a GridLengthConverter Object ?
The following example shows how to create and use an ‘instance’ of the GridLengthConverter object. The example defines a custom method called ‘changeCol()’, which passes the ListBoxItem to a GridLengthConverter that converts the Content of a ListBoxItem to an instance of GridLength. The converted value is then passed back as the value of the ‘Width’ property of the ColumnDefinition element. [C#] public void changeColVal(object sender, RoutedEventArgs e) { txt1.Text = ‘Current Grid Column is ‘ + hs1.Value.ToString(); } public void changeCol(object sender, SelectionChangedEventArgs args) { ListBoxItem li = ((sender as ListBox).SelectedItem as ListBoxItem); GridLengthConverter myGridLengthConverter = new GridLengthConverter(); if (hs1.Value == 0) { GridLength gl1 = (GridLength)myGridLengthConverter.ConvertFromString(li.Content.ToString()); col1.Width = gl1; } else if (hs1.Value == 1) { GridLength gl2 = (GridLength)myGridLengthConverter.ConvertFromString(li.Content.ToString()); col2.Width = gl2; } else if (hs1.Value == 2) { GridLength gl3 = (GridLength)myGridLengthConverter.ConvertFromString(li.Content.ToString()); col3.Width = gl3; } }
How can I apply a Style to a Menu control on a ToolBar ?
You can apply a menu control style on a toolbar by using the Toolbar.MenustyleKey Property. This can be done with the following code snippet,
How can the username and password been created from login control?
Drag and Drop the Login Control VS.NET toolbar under Login , change the form name to ‘Login.aspx’. In the web.config file: Add the codings given below <authentication mode=’Forms’> <forms loginUrl=’Login.aspx’ protection=’All’> <credentials passwordFormat=’Clear’> <user name=’Admin’ password=’Admin’/> <user name=’Super’ password=’Super’/> <user name=’User’ password=’User’/> </credentials> </forms> </authentication> When the user name and corresponding password mentioned above are given , then the login operation will be completed successfully.