Archive

Archive for October, 2009

Get Current Page Name using javascript

October 30, 2009 1 comment

The below function will give you the current page name by using javascript:

function GetCurrentPageName() {
//method to get Current page name from url.
//return page name in small .
var sPath = window.location.pathname;
var sPage = sPath.substring(sPath.lastIndexOf(‘/’) + 1);
return sPage.toLowerCase()
}

now when using :

 

function OnLoad() {

if (GetCurrentPageName() == ‘home.aspx’) {

alert(‘i got current page name’);

}

Calling Javascript From Asp.net Codebehind in .aspx.cs

October 26, 2009 Leave a comment

Many times we need to invoke javascript function in codebehind of asp.net page ie. in .aspx.cs page.

There are many ways to accomplish this.

I will provide you a link which will show you how to invoke javascript in master and content page of asp.net

http://www.dotnetcurry.com/ShowArticle.aspx?ID=273

Other ways are as follows:

in .aspx.cs write following code:

Page.ClientScript.RegisterStartupScript(this.GetType(), “alert”, “Disconnect();”, true);

in .aspx write following code:

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

function Disconnect()
{
alert(‘Sucessfully Disconnected’);

}

</script>

Other way is:

Response.Write(“<script>alert(Successfully Disconnected”)</script>”);

Refer another good article at :
http://www.codedigest.com/Articles/ASPNET/314_Multiple_Ways_to_Call_Javascript_Function_from_CodeBehind_in_ASPNet.aspx

Open a New Window On Server Side using Javascript Code in Asp.net

October 24, 2009 1 comment

Normally to open a new window using javascript we write the following code:

// open new window with specified path

function OpenWindow(url)

{
    newwindow = window.open(url, 'mywindow', 'width=500,height=400');  

}

Now In .aspx.cs page or server side to use the javascript window open you have to use the following code

 StringBuilder popupScript = new StringBuilder();
 popupScript.Append("<script language='JavaScript'> window.open('ViewReports.aspx?groupid=" + ddlreportGroup.SelectedValue.ToString() + "&FromDate=" + Convert.ToDateTime(txtDate.Text) + "&Todate=" + Convert.ToDateTime(txtToDate.Text) + "&Report=Rptname" + "', '', 'width=950, height=580,scrollbars,status,menubar=no,resizable,toolbar=no,titlebar=no,location=no');</script>");
// if you are using script manager - update panel then this
 ScriptManager.RegisterStartupScript(this.Page, Page.GetType(), "PopupScript", popupScript.ToString(), false);
//else
Page.RegisterStartupScript("PopupScript", popupScript.ToString());



Add “Select” Option To Dropdown list in Asp.net

October 23, 2009 Leave a comment

There are many ways to add select option to dropdown list.. Here in this post i will list some of the ways:

1)

<asp:DropDownList ID=”ddltemp" runat=”server”>
<asp:ListItem Value=”0">Select</asp:ListItem>
</asp:DropDownList>

2)

ddltemp.Items.Add(new ListItem(”Select”, “0"));

3)

ddltemp.Items.Insert(0, new ListItem(”Select”, “0"));

4)

ddltemp.Items.Insert(0, "–Select–");
ddltemp.Items[0].Value = "0";