.Net中调用Js方法问题归类
关于打开模态窗口的公用方法:
public string ModalWindow(string openAspxPage,int width,int height)
{
string js = string.Format("javascript:window.showModalDialog(\"{0}\",window,\"status:false;dialogWidth:{1}px;dialogHeight:{2}px\")",openAspxPage,width,height);
return js;
}
1.如何在.cs里面写js的事件
Q1: Response.Write(" ");
Q2:Page.RegisterStartupScript("js"," ");
Q3:Page.RegisterOnSubmitStatement("onsubmit"," ");
可以直接写到里去,把上面的代码加到page_load事件里
Q4:使用Page.RegisterClientScriptBlock("Click"," ");注意,其中的名称"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:
btn.Attributes["onclick"]="javascript:dclick();";
Q3:一个很好的例子,四种方法来激发JS。
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
{
///
/// VC1 的摘要描述。
///
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);
}
///
/// 此为设计工具支持所必需的方法 - 请勿使用程式码编辑器修改
/// 这个方法的内容。
///
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中添加了编辑列(更新,删除),
在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:在属性生成器---列--删除的文本里 加入
Q4:datagrid.items[i].cells[j].attributes.ass("onclick","if(!confirm('hello')){return false}");