.NET中调用JS的方法大全

1.如何在.cs里面写js的事件
    Q1: Response.Write("<script language='javascript'></script>");
    Q2:Page.RegisterStartupScript("js","<script>alert('dfdf')</script>");
    Q3:Page.RegisterOnSubmitStatement("onsubmit","<script>alert('ddff')</script>");
             可以直接写到<form    >里去,把上面的代码加到page_load事件里
   Q4:使用Page.RegisterClientScriptBlock("Click","<script>function isValid(){.....}</script>");注意,其中的名称"Click"不能和已有的输出脚本块名称重复,以免带来不必要的麻烦。意见:输出前使用Page.IsClientScriptBlockRegistered进行名称判断。或者干脆给每个脚本的名称都使生成GUID.对应于Page.RegisterStartupScript方法也有Page.IsStartupScriptRegistered进行判断


2.服务端的Button怎么触发js函数啊?我在page_load里加入btn_send.Attributes.Add("OnClick","datacheck()");没用啊?
     Q1:btn_send.Attributes.Add("OnClick","return datacheck()");
     Q2:<script language=javascript>
         function dclick()
         {
               alert("hello");
         }
         </script>

         btn.Attributes["onclick"]="javascript:dclick();";

    Q3:一个很好的例子,四种方法来激发JS。
html:
<%@ Page language="c#" Codebehind="VC1.aspx.cs" AutoEventWireup="false" Inherits="WebApplicationCSharp.form1" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
   <title>Click 事件使用示范</title>
   <meta content="True" name="vs_showGrid">
   <meta content="Microsoft Visual Studio 7.0" name="GENERATOR">
   <meta content="C#" name="CODE_LANGUAGE">
   <meta content="JavaScript" name="vs_defaultClientScript">
   <meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
</HEAD>
<body MS_POSITIONING="GridLayout">
   <script language="JavaScript">
       function sayhello()
       {     
        alert("大家好\n\n欢迎光临");
       }     
   </script>
   <form id="form1" method="post" runat="server">
    <INPUT id="btnSubmit1" style="FONT-SIZE: 12pt; Z-INDEX: 101; LEFT: 26px; POSITION: absolute; TOP: 21px" type="submit" value="请按我" onclick="sayhello()">
    <INPUT id="btnSubmit2" style="FONT-SIZE: 12pt; Z-INDEX: 102; LEFT: 26px; POSITION: absolute; TOP: 87px" type="submit" value="请按我" onclick="sayhello()" runat="server" NAME="btnSubmit2">
    <asp:button id="btnSubmit3" style="Z-INDEX: 103; LEFT: 26px; POSITION: absolute; TOP: 153px" runat="server" Text="请按我" Font-Size="12pt"></asp:button>
    <INPUT id="btnSubmit4" style="FONT-SIZE: 12pt; Z-INDEX: 104; LEFT: 25px; POSITION: absolute; TOP: 223px" type="button" value="请按我" onclick="sayhello();" runat="server" NAME="btnSubmit4"></form>
</body>
</HTML>


cs:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace WebApplicationCSharp
{
/// <summary>
/// VC1 的摘要描述。
/// </summary>
public class form1 : System.Web.UI.Page
{
   protected System.Web.UI.WebControls.Button btnSubmit3;
   protected System.Web.UI.WebControls.DropDownList ColorDropList;
   protected System.Web.UI.HtmlControls.HtmlInputButton btnSubmit4;
   protected System.Web.UI.HtmlControls.HtmlInputButton btnSubmit2;
  
   private void Page_Load(object sender, System.EventArgs e)
   {
    btnSubmit3.Attributes.Add("onclick", "sayhello()");
   }

   #region Web Form Designer generated code
   override protected void OnInit(EventArgs e)
   {
    //
    // CODEGEN: 此呼叫为 ASP.NET Web Form 设计工具的必要项。
    //
    InitializeComponent();
    base.OnInit(e);
   }
   
   /// <summary>
   /// 此为设计工具支持所必需的方法 - 请勿使用程式码编辑器修改
   /// 这个方法的内容。
   /// </summary>
   private void InitializeComponent()
   {    
    this.btnSubmit3.Click += new System.EventHandler(this.btnSubmit3_Click);
    this.btnSubmit2.ServerClick += new System.EventHandler(this.btnSubmit2_ServerClick);
    this.btnSubmit4.ServerClick += new System.EventHandler(this.btnSubmit4_ServerClick);
    this.ID = "form1";
    this.Load += new System.EventHandler(this.Page_Load);

   }
   #endregion

   private void btnSubmit2_ServerClick(object sender, System.EventArgs e)
   {
    btnSubmit2.Value = DateTime.Now.ToString();
   }

   private void btnSubmit3_Click(object sender, System.EventArgs e)
   {
    btnSubmit3.Text = DateTime.Now.ToString();
   }

   private void btnSubmit4_ServerClick(object sender, System.EventArgs e)
   {
    btnSubmit4.Value = DateTime.Now.ToString();
   }
    
   
}
}




4.如何给DataGrid中的TextBox加Js事件?Datagrid1的模板列中有名为"TextBox1"的TextBox控件,如何为它加上Js事件,如onkeydown?请用C#
     Q1:DataGrid的ItemDataBound事件里写

TextBox txt = e.Item.Cells[1].FindControl("TextBox1") as TextBox;
if (txt != null)
{
     txt.Attributes.Add("onClick","jsFunction();");
}

Q2:itemDataBound里面

if(e.Item.ItemIndex > -1)
{
    TextBox tb = (TextBox)e.Item.FindControl("TextBox1);
    if(tb != null)
        tb.Attributes.Add("onkeydown","yourjsfunction();");
}

Q3:在ItemDataBound内
if(e.Item.ItemType==ListItemType.Item||e.Item.ItemType==ListItemType.AlternatingItem)
{
    TextBox txt = (TextBox)e.Item.FindControl("TextBox1");
    if(txt != null)
{   
    txt.Addributes["onkeydown"]="YourJavascriptFunctionName()";
}
}

Q4:在datagrid的ItemDataBind事件里
private void dg_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)

     TextBox t=e.Item.FindControl("TextBox1");
     t.Attribute.Add("onkeydown","alert('hello!')");



5.我在DataGrid中添加了编辑列(更新,删除),

<asp:editcommandcolumn buttontype="LinkButton" updatetext="更新" canceltext="取消" edittext="编辑"></asp:editcommandcolumn>
          <asp:buttoncolumn text="删除" commandname="Delete"></asp:buttoncolumn>

在itemdatabinding事件中怎样为DataGrid的编辑列(更新,删除)添加js的confirm提示框啊?
就是当点击了编辑列(更新,删除)的时候弹出js的提问框啊!

     Q1:private void DataGrid1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
   {
    if(e.Item.ItemIndex<0)   return;
    if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
    {

              e.Item.Attributes.Add("OnClick","javascript:return confirm()");

    }

   }

     Q2:可以在ItemDataBound事件处理程序中添加,看这个帖子:
http://community.csdn.net/Expert/topic/3200/3200879.xml?temp=.5592005

Q3:在属性生成器---列--删除的文本里 加入
<div id=del onclick="javascript:return confirm('是否删除');"删除<div>
     Q4:datagrid.items[i].cells[j].attributes.ass("onclick","if(!confirm('hello')){return false}");

你可能感兴趣的:(.net)