z
 
:: Home     :: MS Dynamics CRM     :: .Net 1.1     :: .Net 2.0     :: Sharepoint Portal     :: Ajax

  login:        
  passwords:  
 

Resources

Finding Correct Content Managemet System
This list covers the full lifecycle of a content management system, from initially creating the content, through to delivering it to end users...

Workflow Managemet Systems
Workflow management is a crucial component in organizing a variety of business processes so that they benefit the business as a whole and increase profitability...

Using the Power of Content Management Systems
With page editors that resemble a word processor program, adding content with a CMS interface is simple and fun. Most CMS software also allows you to change the location of your content pages and links easily, while the back end processes takes care of updating the links throughout your site...

Content Management Systems (CMS): What They Are And Why We Love Them
In the past, individuals who took interest in having and operating their own websites were burdened with the task of learning HTML, DHTML, and other web-based technologies such as JavaScript and CSS. The only alternative to this was, unfortunately, to pocket the expenses and costs required to pay a web developer to build and maintain it for them...

Outsourcing
Post your project for outsourcing and get bids from qualified programmers, designers, interpreters, copywriters.


 

Code Walkthroughs

Datagrid Formatting the Data
We are able to format the content of the datagrid cell by one of two simple methods, dependant upon whether the column is a bound column or whether it is a template column. In our example we shall format the column to have to digits after the decimal point , followed by a...

Datagrid Highlight a Row With Click Through
It is relatively easy to add alternating colours to the rows in your datagrid. However, when we move the mouse over the rows we may want to highlight this row, and possibly to add the option of a click through based on the row selected...

Add a Delete Button to a Datagrid
To add a delete button to a datagrid follows a similar process to adding an edit button. In the datagrid header...

Add an Edit Button to a Datagrid
The datagrid has a predefined editColumn for handling the editing of a datagrid. Adding this simple column definition to a datagrid adds a powerful feature. When a row is not in edit mode the column item shows the word...

Making a Datagrid Row Editable
Two of the most popular methods of editing a datagrid in asp.net are to either select the row and take the user off to a different presentation of the data, or to change the formatting of the row presented in the database with appropriate edit text boxes, checkboxes and...

Adding Tooltips to Datagrid Rows
Adding tooltips to datagrid rows is easy, assuming that you have already created the code for adding row highlighting. In this article I shall assume that you have already read the article entitled Datagrid Highlight a Row With Click Through...

Binding a Datagrid to an Access Database
This list covers the full lifecycle of a content management system, from initially creating the content, through to delivering it to end users...

Adding Data to a DropDownList
The aim of this article is to answer the question 'How do I add items to a DropDownList?' Initially as part of the declaration for the DropDownList we can also define a number of items, much in the same way as in classic ASP...

Getting Current Date Time
In classic ASP we had now() which would return the current date and time. For asp.net this no longer exists. So what should we use...

Test if File Exists
Sometimes, in order to reduce our chance of error, when working with the filesystem in ASP.NET, we need to determine wether a file exists before performing an action on it. The following short piece of code will enable us to test whether a file exists...

Using Javascript with ASP.NET Form Elements
Adding simple pieces of Javascript to an Asp.net page can be acheived by adding to the attributes of the particular imagebutton or linkbutton. if its normal ASP.Net Button then you can...

Regular Expressions
In the table below we list the characters used in .Net regular expressions, together with their meaning, But first...

Authentication in Asp.net
Forms authentication in ASP.Net is far more easier and safe than Asp 3. It is possible to place a web.config file in any directory of a web site.Therefore, we are able to make most of a web site public, whilst providing authentication on, say, one directory...

Discussion Forums

General ASP.NET

.Net Programming

cSharp Home

Sql Server Home

Javascript / Client Side Development

IT Jobs

Ajax Programming

Ruby on Rails Development

Perl Programming

C Programming Language

C++ Programming

Python Programming Language

Laptop Suggestions?

TCL Scripting

Fortran Programming

Scheme Programming


22. Controls

FAQ Home
   22.1 How to close the browser window on button control click?
   22.2 How to create server controls at runtime?
   22.3 How to change the text color of a linkbutton control?
   22.4 How to dynamically add validator controls?
   22.5 How to change the image button size during runtime?
   22.6 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?
   22.7 How to maintain Scroll Position in any Page Element?
   22.8 DataBinder.Eval imposes performance penalty on code as it uses late bound reflection. How can I replace this calls with explicit calls?
   22.9 What is the difference between User Controls and Custom Controls?
   22.10 How to apply Style to a Web Server control programmatically?
   22.11 How can I disable a button to prevent the user from multiple clicking?
   22.12 Why do I get the error message "Access is denied : <myctrl>" when I try to load a Custom Control?
   22.13 How to pass information between panels. Information entered in one panel should be displayed in other panel?
   22.14 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?
   22.15 How to implement mouseover effects in a <asp:image> web server control?
   22.16 Is there a way to set the position of the controls dynamically?
   22.17 How can I set the maximum and minimum value for RangeValidator based on two dates?
   22.18 How to specifiy the hspace and vspace in a <asp:Image>?
   22.19 How to disable status bar messages for Hyperlink Controls?
   22.20 How to get the list of all System.Web.UI.WebControls in my page?
   22.21 How to set the vertical align of a TableCell programmatically?
   22.22 How to control viewstate on individual controls?



22.1 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
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
     Response.Write("<script>window.close();</script>")
End Sub



C#
private void Button1_Click(object sender, System.EventArgs e)
{     
     Response.Write("<script>window.close();</script>");
}


Method 2. This is preferrable 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();")



C#
      Button1.Attributes.Add("OnClick", "window.close();");



22.2 How to create server controls at runtime?


Here is an example of creating a HyperLink control in code.

VB.NET


Dim hpl As New HyperLink()
hpl.Text="Text"
hpl.NavigateUrl="http://www.syncfusion.com"'
hpl.ID="theID"
Page.Controls(1).Controls.Add(hpl)



C#
HyperLink hpl = new HyperLink();
hpl.Text="Text";
hpl.NavigateUrl="http://www.syncfusion.com";
hpl.ID="theID";
Page.Controls[1].Controls.Add(hpl);



22.3 How to change the text color of a linkbutton control?



<asp:LinkButton forecolor ="green" id="LinkButton1" runat="server">LinkButton</asp:LinkButton>




22.4 How to dynamically add validator controls?


VB.NET


Dim i As Integer = 1

'Textbox
Dim txtBox As TextBox = New TextBox
txtBox.ControlStyle.CssClass = "textbox"
txtBox.ID = "txtbox" + i.ToString()

'RequiredFieldValidator
Dim rqdVal As RequiredFieldValidator = New RequiredFieldValidator
rqdVal.ID = "rqdVal" + i.ToString()
rqdVal.ControlToValidate = "txtbox" + i.ToString()
rqdVal.ErrorMessage = "Please enter a value"
rqdVal.Display = ValidatorDisplay.Dynamic

'RangeValidator
Dim rngVal As RangeValidator = New RangeValidator
rngVal.ID = "rngVal" + i.ToString()
rngVal.MinimumValue = "1"
rngVal.MaximumValue = "100"
rngVal.ControlToValidate = "txtbox" + i.ToString()
rngVal.Type = ValidationDataType.Double
rngVal.ErrorMessage = " Value should be between 1 and 100"

'Add Controls on the page
Page.Controls(1).Controls.Add(txtBox)
Page.Controls(1).Controls.Add(rqdVal)
Page.Controls(1).Controls.Add(rngVal)


C#


int i=1;

//Textbox
TextBox txtBox = new TextBox();
txtBox.ControlStyle.CssClass = "textbox";
txtBox.ID = "txtbox" + i.ToString();

//RequiredFieldValidator
RequiredFieldValidator rqdVal = new RequiredFieldValidator();
rqdVal.ID = "rqdVal" + i.ToString();
rqdVal.ControlToValidate = "txtbox" + i.ToString();
rqdVal.ErrorMessage = "Please enter a value";
rqdVal.Display =ValidatorDisplay.Dynamic;

//RangeValidator
RangeValidator rngVal = new RangeValidator();
rngVal.ID = "rngVal" + i.ToString();
rngVal.MinimumValue = "1";
rngVal.MaximumValue = "100";
rngVal.ControlToValidate = "txtbox" + i.ToString();
rngVal.Type = ValidationDataType.Double;
rngVal.ErrorMessage = " Value should be between 1 and 100";

//Add Controls on the page
Page.Controls[1].Controls.Add (txtBox);
Page.Controls[1].Controls.Add (rqdVal);
Page.Controls[1].Controls.Add (rngVal);



22.5 How to change the image button size during runtime?


VB.NET


Dim x As New Unit("80px")
Dim y As New Unit("20px")
Dim imagestate as string= "stand"
If imagestate = "stand" then
     imagebutton1.height = x
else
     imagebutton1.height = y
end if


C#


Unit x = new Unit ("80px");
Unit y = new Unit("20px");
string imagestate="stand";
if( imagestate != "stand")
{
     ImageButton1.Height = x;
}
else
{
     ImageButton1.Height = y;
}


For more details Setting Web Server Control Properties Programmatically


22.6 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.


22.7 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.


22.8 DataBinder.Eval imposes performance penalty on code as it uses late bound reflection. How can I replace this calls with explicit calls?


Change the Databinding Expression


<%#DataBinder.Eval (Container.DataItem , "StartDate" , "{0:c}")%>


to In VB.NET


<%#String.Format("{0:d}" , (CType(Container.DataItem, DataRowView))("StartDate"))%>


C#


<%#String.Format("{0:d}" , ((DataRowView)Container.DataItem)["StartDate"])%>



22.9 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.


22.10 How to apply Style to a Web Server control programmatically?


User Interface


<asp:TextBox id="TextBox1" style="Z-INDEX: 101; LEFT: 40px; POSITION: absolute; TOP: 64px" runat="server">Syncfusion</asp:TextBox>
<asp:Button id="Button1" style="Z-INDEX: 102; LEFT: 48px; POSITION: absolute; TOP: 120px" runat="server" Text="Button"></asp:Button>
<asp:CheckBox id="CheckBox1" style="Z-INDEX: 103; LEFT: 40px; POSITION: absolute; TOP: 200px" runat="server" Text="Syncfusion"></asp:CheckBox>


VB.NET
Create a function


Protected Function createStyleForControl(ByVal _forecolor As Color, ByVal _backcolor As Color, ByVal _fontbold As Boolean) As Style
     Dim s As Style = New Style
     s.ForeColor = _forecolor
     s.BackColor = _backcolor
     s.Font.Bold = _fontbold
     Return s
End Function


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)


C#
Create a function


protected     Style createStyleForControl(Color _forecolor ,Color _backcolor ,bool _fontbold )
{
     Style s = new Style();
     s.ForeColor = _forecolor;
     s.BackColor =_backcolor;
     s.Font.Bold = _fontbold;
     return s;
}


In Page_Load


Style textboxStyle=createStyleForControl (Color.Pink , Color.Yellow ,false );
Style buttonStyle=createStyleForControl (Color.Blue , Color.Teal ,true );
Style checkboxStyle =createStyleForControl (Color.AliceBlue , Color.PowderBlue ,false );

TextBox1.ApplyStyle(textboxStyle );
Button1.ApplyStyle (buttonStyle );
CheckBox1.ApplyStyle (checkboxStyle );