How can we set the default button that should submit the form and raise its Click event on the server when the enter key is pressed on the form

.NET Classes used : System.Web.UI.Control

It is usual for a web form to have more than one button control. How can you set the default button that should submit the form and raise its Click event on the server when the enter key is pressed on the form?

The textbox and the other input controls don’t have a property that allow you to specify such a default button.

The answer is in a simple client-side javascript function called whenever a button is pressed, that, if the button pressed is the Enter key, cancels the default submit and simulates a click on the wanted button. Here’s the function, that you can copy and paste as-is into your ASPX pages, or that you can paste into a separate .js file, referenced by any ASPX page that needs it:

function KeyDownHandler(btn)

{

// process only the Enter key

if (event.keyCode == 13)

{

// cancel the default submit

event.returnValue=false;

event.cancel = true;

// submit the form by programmatically clicking the specified button

btn.click();

}

}

Now, in the input controls declaration you just have to call this function when a button is pressed, by handling the onKeyPressed client-side event, and pass a reference to the default button:

11
Oct

Common Regular Expression Patterns

   Posted by: Kannan V   in C#, DotNet

Integer “^[0-9]“;

VarCharSmall “^([a-zA-Z0-9\s]{0,50})$”;

VarCharBig “^([a-zA-Z0-9\s]{0,255})$”;

Date(MM/DD/YYYY) “^((0[1-9])(1[0-2]))\/((0[1-9])(1\d)(2\d)(3[0-1]))\/((\d{4}))$”;

Date(DD/MM/YYYY) “^(((0[1-9])(1\d)(2\d)(3[0-1])))\/(0[1-9])(1[0-2]))\/((\d{4}))$”;

PasswordChar “^([a-zA-Z0-9@*#]{8,15})$”;

EmailAddress “^([a-zA-Z0-9_\-])([a-zA-Z0-9_\-\.]*)@(\[((25[0-5]2[0-4][0-9]1[0-9][0-9][1-9][0-9][0-9])\.)

{3}((([a-zA-Z0-9\-]+)\.)+))([a-zA-Z]{2,}(25[0-5]2[0-4][0-9]1[0-9][0-9][1-9][0-9][0-9])\])$”;

PhoneNumber(US) “^1?\s*-?\s*(\d{3}\(\s*\d{3}\s*\))\s*-?\s*\d{3}\s*-?\s*\d{4}$”;

PhoneNumber(International) “^\d(\d-){7,20}”;

Tags:

11
Oct

Getting a Control’s Property using Reflection

   Posted by: Kannan V   in Uncategorized

******this code block loops thro the controls and gets the associated text entered in the TextBox using Reflection******

foreach(Control c in this.Controls)

{

if(c.ToString() == “System.Web.UI.TextBox”)

{

PropertyInfo p1 = c.GetType().GetProperty(“Text”);

string strVal = p1.GetValue (c,BindingFlags.GetProperty,null,null,null).ToString();

}

}

11
Oct

Code to check for CAPS Lock Mode

   Posted by: Kannan V   in Uncategorized

state = NativeMethods.GetKeyState(0×14 /*VK_CAPTIAL*/);

capsKeyDown = Convert.ToBoolean(NativeMethods.HIWORD(state));

capsKeyON = Convert.ToBoolean(NativeMethods.LOWORD(state));

if((state == -127 state == 1 ) && capsKeyON == true)

{

pictureBox1.Visible = true;

}

else if((state == -128 state == 0) && capsKeyON == false)

{

pictureBox1.Visible = false;

}

11
Oct

Persisting a Property value for a Web Control

   Posted by: Kannan V   in Uncategorized

//This code block shows how to persist a boolean property value in a web control.

[

Bindable(true),

Category("Behaviour"),

DefaultValue(true),

Description("Show Condition")

]

public bool ShowCondition

{

get

{

bool selval;

if( ViewState["ShowCondition"] == null)

{

selval = true;

}

else

{

selval = (bool) ViewState["ShowCondition"];

}

return(selval);

}

set

{

this.ViewState["ShowCondition"] = value;

}

}

//Declare the Event as an object.

private static readonly object EventSave = new object();

//Create a Property to enable the user assign an Event.

[

Category("Action"),

DefaultValue(""),

Description("Raised when the user clicks the Save button")

]

public event EventHandler Save

{

add

{

Events.AddHandler(EventSave,value);

}

remove

{

Events.RemoveHandler(EventSave,value);

}

}

//get the list of events for the handler, if not null wire it to the handler in the button save click event inside the web control.

EventHandler SaveHandler=(EventHandler)Events[EventSave];

if(SaveHandler!=null)

{

SaveHandler(this,e);

}

***********Another example using Event Bubbling********************

# region EventBubbling

protected virtual void onSave(EventArgs e)

{

EventHandler SaveHandler=(EventHandler)Events[EventSave];

if(SaveHandler!=null)

{

SaveHandler(this,e);

}

}

#region Event bubbling

protected override bool OnBubbleEvent(object source, EventArgs e)

{

bool handled = false;

if (e is CommandEventArgs)

{

CommandEventArgs ce = (CommandEventArgs)e;

if (ce.CommandName == “Save”)

{

onSave(EventArgs.Empty);

handled = true;

}

}

return handled;

}

#endregion Event bubbling

Page 25 of 25« First...10202122232425