Find answers for the most frequently asked questions
Why do I get the error message ‘Access is denied : <myctrl>’ when I try to load a Custom Control
This is a common problem in ASP.NET. It happens because some program is scanning the Temporary ASP.NET files folders where your assemblies are copied. They have the files open in a mode that prevents ASP.NET itself from using the assembly file.
You can set the EnableViewState property of the control. You should disable viewstate for any control that doesn’t use it to guarantee optimum performance.
How to close the browser window on button control click
Method 1. This will cause a postback on button click in response to which we will send some script to close the window.
VB.NET
PrivateSub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Response.Write('<script>window.close();</script>')EndSub
Method 2. This is preferable since there is no postback involved.
Use the Attribute collections i.e use Attributes of button control to add client side JavaScript code
VB.NET
Button1.Attributes.Add('OnClick', 'window.close();')
PrivateSub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) HandlesMyBase.Load
’ Put user code to initialize the page hereIfNot Page.IsPostBack Then
Button1.Style.Add('LEFT', '1px')EndIfEndSub’Page_LoadPrivateSub RadioButtonList1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButtonList1.SelectedIndexChanged
Button1.Style.Add('LEFT', RadioButtonList1.SelectedItem.Value.ToString())EndSub’RadioButtonList1_SelectedIndexChanged
C#
privatevoidPage_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page hereif (!Page.IsPostBack )
{
Button1.Style.Add('LEFT', '1px');
}
}
privatevoidRadioButtonList1_SelectedIndexChanged(object sender, System.EventArgs e)
{
Button1.Style.Add('LEFT',RadioButtonList1.SelectedItem.Value.ToString());
}
ProtectedFunction GetControls() As ArrayList
Dim arrList AsNew ArrayList()
Dim t As Type
ForEach t InGetType(Page).Assembly.GetTypes()
If t.Namespace = 'System.Web.UI.WebControls' And GetType(Control).IsAssignableFrom(t) Then
arrList.Add(t)
EndIfNextReturn arrList
EndFunction’GetControls’In Page_Load
DataBind()
C#
protected ArrayList GetControls()
{
ArrayList arrList = new ArrayList();
foreach (Type t intypeof(Page ).Assembly.GetTypes())
{
if (t.Namespace == 'System.Web.UI.WebControls' && typeof(Control).IsAssignableFrom(t))
arrList.Add(t);
}
return arrList;
}
//In Page_Load
DataBind();
ProtectedFunction createStyleForControl(ByVal _forecolor As Color, ByVal _backcolor As Color, ByVal _fontbold AsBoolean) As Style
Dim s As Style = New Style
s.ForeColor = _forecolor
s.BackColor = _backcolor
s.Font.Bold = _fontbold
Return s
EndFunction
In Page_Load
Dim textboxStyle As Style = createStyleForControl(Color.Pink, Color.Yellow, False)
Dim buttonStyle As Style = createStyleForControl(Color.Blue, Color.Teal, True)
Dim checkboxStyle As Style = createStyleForControl(Color.AliceBlue, Color.PowderBlue, False)
TextBox1.ApplyStyle(textboxStyle)
Button1.ApplyStyle(buttonStyle)
CheckBox1.ApplyStyle(checkboxStyle)
How to change the text color of a linkbutton control
Pseudo-elements are fictional elements that do not exist in HTML. They address the element’s sub-part. There are 2 types of pseudo-elements as “first-line pseudo-element’ and ’first-letter pseudo-element’. Pseudo-element is created by a colon followed by pseudo-element’s name,e.g:
P:first-lineH1:first-letter
and can be combined with normal classes;
e.g:
P.initial:first-line
The first line of this paragraph will be displayed in uppercase letters
Why do I get error message ‘The control ‘_ctl1’ of type ‘TextBox’ must be inside a form label with runat=server’ while trying to place a control inside a form dynamically
You are probably adding the textbox into the Page directly instead of adding it to the Page’s form. Try doing this:
What is the difference between User Controls and Custom Controls?
User Controls are text files with the extension ’.ascx’ which enables you to make GUI re-usable controls. Its a text file, which contains a mixture of HTML and scripting. User Controls can be edited in an editor.
On the other hand Custom Controls reside in compiled ( Dll ) assemblies. They are non-GUI but can emit HTML GUI at runtime. Example of Custom Controls would be the sever controls which come bundled with the .NET SDK like DataGrid, Repeater, DataList etc. Custom Controls are always compiled into Dll’s and hence require good programming knowledge. The purpose of Custom Controls is also to create re-usable units of code.
How to maintain Scroll Position in any Page Element
Check out Jim Ross’s article Maintain Scroll Position in any Page Element
The article describes, when using a scrollable Datagrid–one inside an overflow-auto DIV control–how to maintain the user’s scroll position inside the DIV across postbacks. It does this using IE behaviors, HTC files.
How to change the image button size during runtime
VB.NET
Dim x AsNew Unit('80px')Dim y AsNew Unit('20px')Dim imagestate asstring= 'stand'If imagestate = 'stand' then
imagebutton1.height = x
else
imagebutton1.height = y
endif
C#
Unit x = new Unit ('80px');
Unit y = new Unit('20px');
string imagestate='stand';
if( imagestate != 'stand')
{
ImageButton1.Height = x;
}
else
{
ImageButton1.Height = y;
}
How can I set the maximum and minimum value for RangeValidator based on two dates
<asp:textboxid='TextBox1'runat='server'></asp:textbox><asp:rangevalidatorid='RangeValidator1'type='Date'controltovalidate='TextBox1'runat='server'errormessage='Not in range' /><asp:buttonid='Button1'runat='server'text='Button'></asp:button>
VB.NET
...
Dim dtMinDate As DateTime
Dim dtMaxDate As DateTime
dtMinDate = Convert.ToDateTime('04/25/04')
dtMaxDate = Convert.ToDateTime('10/17/04')
RangeValidator1.MinimumValue = dtMinDate
RangeValidator1.MaximumValue = dtMaxDate
TextBox1.Text = dtMaxDate.ToShortDateString
RangeValidator1.ErrorMessage = 'Not in Range of ' & dtMinDate.ToShortDateString() & ' to ' & dtMaxDate.ToShortDateString()
C#
DateTime dtMinDate ;
DateTime dtMaxDate ;
dtMinDate = Convert.ToDateTime('04/25/04');
dtMaxDate = Convert.ToDateTime('10/17/04');
RangeValidator1.MinimumValue = dtMinDate.ToShortDateString () ;
RangeValidator1.MaximumValue = dtMaxDate.ToShortDateString () ;
TextBox1.Text = dtMaxDate.ToShortDateString();
RangeValidator1.ErrorMessage = 'Not in Range of ' + dtMinDate.ToShortDateString() + ' to ' + dtMaxDate.ToShortDateString();
How can I to divide the .aspx page to different parts with different functionality in a neat and organized manner. Is there any control that can do that?
You can use Panel controls, which render as HTML div elements. You can then show or hide the panels by enabling/disabling them. In addition, you can specify different formatting options for different Panels/divs using manual formatting or CSS styles.
Share with