Skip navigation

Category Archives: .NET

If we want to make a control invisible in asp.net, may be the most straight forward way is to set the Visible property to “false”. As long as we are accessing this control from server side, there is no problem. However, if we need to access it from client side using JavaScript, then we have a problem. Once we make the control invisible by setting the Visible property to “false”, we will no longer be able to access it from client side using JavaScript and we will not find the control using document.getElementById(“controlID”).

Solution to this problem is to use the style property to make it invisible. Here are some considerations as well. By using style, we can make a control invisible in two ways.

  1. using visibility:hidden
  2. using display:none

By using any one of the above 2 ways we can still access a control by using document.getElementById(“controlID”) but if we use visibility:hidden, the control will still occupy the space. On the other hand, the control will not occupy space if “display:none” is used.

Making a button coltrol invisible using style

To make a control invisible from aspx page the following code can be used

Setting display style from aspx page

Setting display style from aspx page

To make a control invisible from cs page the following code can be used

setting display style from cs file

setting display style from cs file

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.

Sometimes we want to format a double value to monetary value. For example, consider the double value “1234.5678”. We want to format this value as “$1,234.56”. We can do this very easily.


double d = 2234500.2345;
Response.Write("$" + d.ToString("#,#.00"));
//output - $2,234,500.23
Response.Write("$" + d.ToString("#,#.000"));
//output - $2,234,500.235
Response.Write("$" + d.ToString("#,#.0000"));
//output - $2,234,500.2345

It also takes care of rounding the value. consider the following code.


double d1 = 1234.124;
double d2 = 1234.125;
Response.Write("$" + d1.ToString("#,#.00"));
//outputs $1,234.12
Response.Write("$" + d2.ToString("#,#.00"));
//outputs $1,234.13

This technique may be useful in the case when we retrieve data from the database and have to convert a varchar to monetary value. For example, if we are retrieving data using data reader, we can do the following:


//dr is DataReader
Response.Write("$" + (Convert.ToDouble(dr["ColumnName"].ToString())).ToString("#,#.00"));