2011年3月21日星期一

javascript jQuery code

1. _pTreeView.Attributes.Add("onclick", "OnTreeClick(event)");

2.
CLBOA.Utils.processClientConfirmation = function(confirmText, postbackLinkRef) {
if (typeof (CLBOA.Utils.getCustomClientConfirmation) == 'function') {
CLBOA.Utils.getCustomClientConfirmation(confirmText, postbackLinkRef);
}
else {
return confirm(confirmText);
}
return false;
}


CLBOA.Utils.getCustomClientConfirmation = function(confirmText, postbackValue) {
if ($('#dialog').length > 0) {

$('#dialog').dialog('option', 'title', 'Confirmation');
$('#dialog').text(confirmText);
$('#dialog').dialog('option', 'buttons', { "OK": function() { $(this).dialog("close"); eval(postbackValue); }, "Cancel": function() { $(this).dialog("close"); } });
$('#dialog').dialog("open");
}
else {
alert("Require object with id 'dialog'");
}
}



// find the row's delete button
LinkButton deleteButton = CLBPCHelpers.FindCommandButton(e.Row, "delete");
if (deleteButton != null)
{
// add the confirm call with our text
deleteButton.OnClientClick = "return CLBOA.Utils.processClientConfirmation(\"" + this.ConfirmDeleteText + "\",\"" + Page.ClientScript.GetPostBackEventReference(deleteButton, String.Empty) + "\")";

}

GetPostBackEventReference(Control, String)
Returns a string that can be used in a client event to cause postback to the server. The reference string is defined by the specified control that handles the postback and a string argument of additional event information.

///
/// Loop through each of the grid row cells to find the appropriate command button
///

///
/// ie. "delete", "select", etc...
///
public static LinkButton FindCommandButton(GridViewRow row, string commandString)
{
// default to null
LinkButton button = null;

if (!string.IsNullOrEmpty(commandString))
{
// get the first cell
foreach (TableCell commandCell in row.Cells)
{
button = FindThatCommandButton(commandCell, commandString) as LinkButton;
if (button != null)
break;
}
}
return button;
}

private static Control FindThatCommandButton(Control currentControl, string commandString)
{
Control button = null;
button = (from LinkButton b in currentControl.Controls.OfType(LinkButton>()
where b.CommandName.ToLower() == commandString.ToLower()
select b).FirstOrDefault();
if (button == null && currentControl.Controls.Count > 0)
{
foreach (Control c in currentControl.Controls)
{
//call recursive
button = FindThatCommandButton(c, commandString);
if (button != null)
break;
}
}
return button;
}


3.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Text;
using System.IO;

namespace CLBOA.PLL.Custom
{
///
/// To solve issue with inline javascripts that are used with the UpdatePanel.
/// Contents created/injected by the updatepanel (ie. Ajax contents) cannot access the javascripts
/// that is embedded in the page unless they are wrapped by this control.
///

public class CLBInlineScript : Control
{
protected override void Render(HtmlTextWriter writer)
{
ScriptManager sm = ScriptManager.GetCurrent(Page);
if (sm.IsInAsyncPostBack)
{
StringBuilder sb = new StringBuilder();
base.Render(new HtmlTextWriter(new StringWriter(sb)));
string script = sb.ToString();
ScriptManager.RegisterStartupScript(this, typeof(CLBInlineScript), UniqueID, script, false);
}
else
{
base.Render(writer);
}
}
}
}

%@ Register assembly="CLBOA.PLL.Custom" namespace="CLBOA.PLL.Custom" tagprefix="PLLCustom" %>

PLLCustom:CLBInlineScript ID="inlineScript" runat="server">
script type="text/javascript">
$(document).ready(function() {
$("#<%= pnlPrivileges.ClientID %>").panel({ collapsible: false });
});
/script>
/PLLCustom:CLBInlineScript>

4.
ajaxTK:ToolkitScriptManager ID="tksm" runat="server" OnAsyncPostBackError="tksm_AsyncPostBackError" >
Scripts>
asp:ScriptReference Path="~/js/jquery/1.4.2/jquery-1.4.2.min.js" />
asp:ScriptReference Path="~/js/jquery.cookie.js" />
/Scripts>
/ajaxTK:ToolkitScriptManager>

5.
使用ScriptManager.RegisterStartupScript方法可注册一个每次发生异步回发时都将包括的启动脚本块。 若要为 UpdatePanel 控件内的某个控件注册一个脚本块,以便仅在更新 UpdatePanel 控件时才注册该脚本块,请使用该方法的 RegisterStartupScript(Control, Type, String, String, Boolean) 重载。

如果要注册与部分页面更新无关的启动脚本,并且只想在初始页面呈现期间注册一次该脚本,请使用 ClientScriptManager 类的 RegisterStartupScript 方法。 可以从页面的 ClientScript 属性获取对 ClientScriptManager 对象的引用。

if (!ClientScript.IsClientScriptBlockRegistered("exampleScript"))
ClientScript.RegisterStartupScript(this.GetType(), "exampleScript","

");


6.
Script to hide status bar messages in IE
onmouseover="window.status=''; return true;" OnClientClick="location.href='/Login.aspx'"

7.
//right click disabled
$(document).bind("contextmenu",function(e){
return false;
});

$(document).bind("keydown", function(e) {
//backspace
if (e.keyCode == 8) return false;
});

8.

In basepage, define ExecuteClientScript method:
public void ExecuteClientScript(StringBuilder scriptJs)
{
ClientScript.RegisterStartupScript(typeof(string), "script", scriptJs.ToString(), true);
}


From code behind:
ExecuteClientScript(new StringBuilder(" CloseMapOverlay(); "));

In page:
script type="text/javascript">
function CloseMapOverlay()
{
parent.$.fancybox.close();
}
/script>

9. From javacript, run sever side event handler:
$("li.dropdown ul li a").live("click", function ()
{
$('#hdnProvince').val($(this).attr("id"));
$('#btnSaveCookie').click(); //here call server side event handler
return false;
});

This is the button that has onclick event



10. Call webservice from javascript

Method RegisterFirstAccess is defined in Default.aspx.cs.

[WebMethod()]
public static void RegisterFirstAccess()
{
//Registering the first access to call the map overlay
HttpContext.Current.Session.Add("FirstAccess", "false");
}

From jquery:

if (IsFirstAccess == 'true') {

//registering the first access with user session...
$.ajax({
type: "POST",
url: "Default.aspx/RegisterFirstAccess",
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json"
});

没有评论:

发表评论