Friday, October 5, 2012
Monday, July 9, 2012
Get the ConnectionString from the Web.Config file with only one line of code (C#)
Get the ConnectionString from the Web.Config file with only one line of code (C#):
var conString = ConfigurationManager.ConnectionStrings["LocalSqlServer"]; string strConnString = conString.ConnectionString;
Tuesday, May 8, 2012
Getting All Controls On The ASP.NET Page
While working with ASP.NET, from time to time, I need to change some parameter that is common to all controls of on a page. An example of such operation might be setting ReadOnly property for all TextBox controls etc.
Below is a sample method code I use to get a flattened hierarchy of controls on a page:
public static Control[] FlattenHierachy(Control root) { List<Control> list = new List<Control>(); list.Add(root); if (root.HasControls()) { foreach (Control control in root.Controls) { list.AddRange(FlattenHierachy(control)); } } return list.ToArray(); }As you can see, it is pretty simple. The method recursively traverses control hierarchy and collects each control it finds on its way to a collection which is finally returned to the caller in a form of an array.
As mentioned above we can use this method to set the ReadOnly property of all TextBox-es on the page. To do this, a code similar to the one below can be used:
private void MakeTextBoxesReadOnly() { Control[] allControls = FlattenHierachy(Page); foreach (Control control in allControls) { TextBox textBox = control as TextBox; if (textBox != null) { textBox.ReadOnly = true; } } }Using a combination of the above outlined methods it is easy to implement a kind of ReadOnlyMode page filter. Just identify what controls need to be ReadOnly and how to make them so (RadioButton for example doesn't have a ReadOnly property). When you know how and what you want to make ReadOnly, just override Page's OnPreRender method by adding a call a MakeTextBoxesReadOnly method (or whatever method you create).
Of course all of this is possible thanks to the great ASP.NET control model which is one of the things that I really like about ASP.NET.
Source: http://vaultofthoughts.net
Thursday, March 8, 2012
Sunday, November 20, 2011
How to open a page in a new window.
How to open a page in a new window.
Easy. Here is how:
ScriptManager.RegisterStartupScript(this, typeof(string), "OPEN_WINDOW", "window.open( 'http://www.google.com', null, 'height=200,width=400,status=yes,toolbar=no,menubar=no,location=no' );", true);
or :
ScriptManager.RegisterStartupScript(this, typeof(string), "OPEN_WINDOW", "window.open( 'http://www.google.com', null, 'height=200,width=400,status=yes,toolbar=no,menubar=no,location=no' );", true);
or :
Monday, October 31, 2011
Wednesday, September 21, 2011
Comfirm for delete button
Simply making a confirm message for a delete button.
All you have to do is add this onclientclick="return confirm('Are you sure you want to Delete this Apointment ?');"
For example :
All you have to do is add this onclientclick="return confirm('Are you sure you want to Delete this Apointment ?');"
For example :
<asp:Button ID="Button2"
runat="server"
Text="Delete" onclick="Button2_Click"
onclientclick="return confirm('Are you sure you want to Delete this Apointment ?');" />
Simple?
Subscribe to:
Posts (Atom)