Skip navigation

Monthly Archives: January 2009

A .Net page can be posted back with JavaScript. We can call __doPostBack() function to generate a manual postback.

The following example shows how to do the postback manually with JavaScript and detect the manual postback from server side. In our example, we will call “PerformTask()” server side method on postback. The __doPostBack() function takes two parameters — eventTarget and eventArgument.

JavaScript:

__doPostBack(‘__Page’, ‘performTask’);

Server side:

protected void Page_Load(object sender, System.EventArgs e)

{

if(Page.IsPostBack)

{

if(Request[“__EVENTARGUMENT”] == “performTask”)

{

PerformTask();

}

}

}

private void PerformTask()

{

//perform the processing after manual postback

}

There are situations when we want to call Asp.net validatiors form JavaScript. One such situation is when we want to close a pop up window on button click using window.close(). But before closing the window using JavaScript, we want to validate the data written in the controls of the window.

It possible to call Asp.net validators from JavaScript. The following code shows a portion of asp.net page which includes one standard .net required field validator and one regular expression validator.

<table>

<tr>

<td><asp:Label ID=”lbl1″ runat=”server” Text=”Please enter a digit”></asp:Label></td>

<td><asp:TextBox ID=”txtbox1″ runat=”server”></asp:TextBox></td>

<td><asp:RequiredFieldValidator ID=”valReq” ControlToValidate=”txtbox1″ runat=”server” ErrorMessage=”*” Display=”Dynamic”>
</asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID=”valreg” ControlToValidate=”txtbox1″ runat=”server” ErrorMessage=”Not valid character” ValidationExpression=”[0-9]”>
</asp:RegularExpressionValidator></td>

</tr>

<tr>

<td></td>
<td><asp:Button ID=”btn1″ runat=”server” Text=”Submit” OnClientClick=”performCheck()”/></td>
<td></td>

</tr>

</table>

In design mode, it looks as below

View in design mode

View in design mode

Now we want to make sure that .net validators get fired up on “Submit” button click before closing the window using javascript window.close(). In our example, we have a text box where we expect a single digit before closing the window.

All we have to do to fire up .net validators is to call “Page_ClientValidate()” function.  The following JavaScript code shows how “Page_ClientValidate()” function can be used before closeing window.

<script type=”text/javascript” language=”javascript”>

function performCheck()
{

if(Page_ClientValidate())
{

window.close();

}

}
</script>

Now, if the Submit button is clicked leaving the text box empty, the required field validator will fire up as shown in the below screen shot.

Required field validotor fires up

Required field validotor fires up

If any thing is written in other than a single digit, we will get the following output.

Regular Expression Validator fires up

Regular Expression Validator fires up

How about Calling any specific validator rather than all?

Well, this can be done as well. In this case we need to provide a ValidationGroup name for the validator and pass the validator group name to Page_ClientValidate() function as parameter. The following code segment shows a group name is provided for the required field validator and passed it to Page_ClientValidate() function. In this case, only required field validatior will be fired up and not the regular expression validator.

<table>

<tr>

<td><asp:Label ID=”lbl1″ runat=”server” Text=”Please enter a digit”></asp:Label></td>
<td><asp:TextBox ID=”txtbox1″ runat=”server”></asp:TextBox></td>
<td><asp:RequiredFieldValidator ID=”valReq” ControlToValidate=”txtbox1″ runat=”server”
ErrorMessage=”*” ValidationGroup=”Required” Display=”Dynamic”>
</asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID=”valreg” ControlToValidate=”txtbox1″ runat=”server”
ErrorMessage=”Not valid character” ValidationExpression=”[0-9]” ValidationGroup=”RegExpression”>
</asp:RegularExpressionValidator></td>

</tr>

<tr>

<td></td>
<td><asp:Button ID=”btn1″ runat=”server” Text=”Submit” OnClientClick=”performCheck()”/></td>
<td></td>

</tr>

</table>

<script type=”text/javascript” language=”javascript”>
function performCheck()
{

if(Page_ClientValidate(“Required”))
{

window.close();

}

}
</script>


Advantage

By using standard asp.net validators for validation, we don’t have to write JavaScript code to do the validation. This can minimize the development time to a great extent.

Though we cannot test a private method using NUnit, we can do this using Reflection. Suppose, we have a class named “CellPhone” and it has a private method with the following signature —

private bool SendMessage(string MsgBody, int length)

We can test the method using the the following code segment —

Type cellPhoneType = type0f(CellPhone);

MethodInfo sendMessageInfo = cellPhoneType.GetMethod(“SendMessage”, BindingFlags.NonPublic | BindingFlags.Instance);

bool isSent = (bool) sendMessageInfo.Invoke(new CellPhone(), new object[]{“Hello there, how are you”,  24});


The first parameter to the Invoke method is an object on which the method will be operated and the second parameter an array of object. This array contains the parameter values of the method.

The MethodInfo class is in System.Reflection package. Please note that if the “SendMessage” Method was a static method then we had to set the BindingFlags.Static instead of BindingFlags.Instance and in Invoke method, we had to pass null instead of an object. For private static method we will have the following code –

Type cellPhoneType = type0f(CellPhone);

MethodInfo sendMessageInfo = cellPhoneType.GetMethod(“SendMessage”, BindingFlags.NonPublic | BindingFlags.Static);

bool isSent = (bool) sendMessageInfo.Invoke(null, new object[]{“Hello there, how are you”,  24});