Supression operator
Ternary operators
Ternary Operators
$todo = (empty($_POST[’todo’])) ? ‘default’ : $_POST[’todo’];
The above is identical to this if/else statement
if (empty($_POST[’todo’])) {
$action = ‘default’;
} else {
$action = $_POST[’todo’];
}
Nullable Types
When using the System.Nullable syntax you can substitute it for T?
For example,
System.Nullable<int> intNullable; is the same as
int? intNullable;
If you have declared a non-nullable variable and are assigning it a value that could be null you can use the ?? syntax to give it a default value. For example:
int? intNullable = null;
int intNotNullable = intNullable ?? 99;
Would give the intNotNullable variable a value of 99 in the event of intNullable being null.
Add a confirm Popup in asp .net
To add a confirm popup when a button is clicked in asp .net do the following:
In the buttons OnClientClick add the following javascript:
if(confirm(’Are you sure?’))
{
return true;
}else{
return false;
}
The confirm method returns a boolean.
The popup displays the message, an Ok button and a canel button.
If Ok is clicked return true allows the button to cause a post back.
Clicking cancel returns false cancel the submit action of the button.
Modal Popup in IE and Firefox 3
To have a web page popup in a modal page IE has a javascript function
window.showModalDialog(’page url’, ‘page title’, ‘options’);
The syntax for the options is slightly different from window.open
To specify height and width we would do:
‘dialogHeight:300px; dialogWidth:300px’
To make sure the code works cross browser we can check for the showModalDialog function and if it is not available we can you use window.open.
eg.
if (window.showModalDialog)
{
window.showModalDialog(’page.html’,'MyPage’,'dialogWidth:300px;dialogHeight:3000px’);
}else{
window.open(’page.html’,'MyPage’,'width=300, height=300′);
}
Using external Javascript with asp .net
If your wanting to add an external javascript file/library to use in your aspx pages then you would usually just add a script tag at the top of the page. This works in most cases but not when you are using master pages, a master page wont recognise your script.
To solve this problem do the following in your master page:
<script type='text/javascript'
src='<%# ResolveUrl ("~/Javascript/jquery-1.2.6.min.js") %>'></script>
Use a data bind expression to reference you javascript in the head block of the master page.
Then in the master page code behind Databind the page header on the page load:
Page.Header.DataBind ();