放在CodeBehind里是一样的啊 Attribute.aspx <%@ Page language="c#" Codebehind="Attribute.aspx.cs" AutoEventWireup="false" Inherits="WebApplication2.Attribute" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" > <HTML> <form id="Form1" method="post" runat="server"> <asp:Button id="Button1" style="Z-INDEX: 101; LEFT: 112px; POSITION: absolute; TOP: 64px" runat="server" Text="Button"></asp:Button> </form></HTML>Attribute.aspx.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 WebApplication2 { /// <summary> /// Attribute 的摘要说明。 /// </summary> public class Attribute : System.Web.UI.Page { protected System.Web.UI.WebControls.Button Button1;
private void Page_Load(object sender, System.EventArgs e) { // 在此处放置用户代码以初始化页面 Button1.Attributes.Add("onclick","return confirm('Are You Sure?');"); } #region Web 窗体设计器生成的代码 override protected void OnInit(EventArgs e) { // // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。 // InitializeComponent(); base.OnInit(e); }
private void Page_Load(object sender, System.EventArgs e) { btnLink.Attributes.Add("onclick","return confirm('Are You Sure?');"); if(!Page.IsPostBack) { dgrdLinkview.DataSource=objLink.viewLink(); dgrdLinkview.DataBind(); } } #region Web Form Designer generated code override protected void OnInit(EventArgs e) { // // CODEGEN: This call is required by the ASP.NET Web Form Designer. // InitializeComponent(); base.OnInit(e); }
/// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> private void InitializeComponent() { this.Load += new System.EventHandler(this.Page_Load); } #endregion private void btnLink_Click(object sender, System.EventArgs e) { if(Page.IsValid) { Response.Write("btnLin_Click"); objLink.addLink(txLinkname.Text,txLinkmemo.Text,txLinkurl.Text); txLinkname.Text=""; txLinkmemo.Text=""; txLinkurl.Text=""; } }这样写对吗?
<html>
<head></head>
<body>
<form id="Form1" method="post" runat="server">
<asp:Button id="Button1" runat="server" Text="Button" onClick="Button1_Click">
</asp:Button>
</form>
</body>
</html><script language="c#" runat="server">
void Page_Load(Object Src, EventArgs E)
{
Button1.Attributes.Add("onclick", "return confirm('Are you sure you wish to delete this item?');");
}
void Button1_Click(Object Src, EventArgs E)
{
Response.Write("Button1_Click");
}
</script>Button1按钮同时相应客户端的javascript指令和服务器端的Button1_Click事件。
Response.Write("<script>window.alert('你想干什么');</script>");
alert的用法楼上很多了,我就不说了
Response.Write("<script defer>window.alert('你想干什么');</script>");
这样弹出的窗口不会出现白屏,而且能夺取焦点啊
public void MessageBox(string strText ,Page objPage)
{
/* 1、编写:
* 2、功能:在客户端弹出对话框
* 3、参数:string strText 对话框的文本信息;Page objPage 对话框的所在页面
* 4、返回值: 无
* 5、用途:提示用户操作是否合法,应如何进行下一步操作
*/
string IidScript,strWindow;
strWindow="window.alert('"+strText+"')";
IidScript = "<script>"+strWindow+"</script>";
objPage.RegisterClientScriptBlock("failure",IidScript);
}//调用事例
//MessageBox("提示内容",Page)//其中Page是永远不变的。
一、直接输出代码:
response.write("<script>window.alert('这是一个对话框!');</script>");
二、注册一个脚本到客户端:
registerclientscript("Message","<script>window.alert('这是一个对话框!');</script>");
三、注册一个控件的html属性,在执行服务器控件之前执行一段客户端脚本:
button btn = new button();
btn.attributes["onClick"]="confirm('您确定吗?');";
请问,可以放在CodeBehind里面吗?
还有就是麻烦一下大家给我讲解一下客户端脚本,和服务器端脚本的区别
Attribute.aspx
<%@ Page language="c#" Codebehind="Attribute.aspx.cs" AutoEventWireup="false" Inherits="WebApplication2.Attribute" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML> <form id="Form1" method="post" runat="server">
<asp:Button id="Button1" style="Z-INDEX: 101; LEFT: 112px; POSITION: absolute; TOP: 64px" runat="server"
Text="Button"></asp:Button>
</form></HTML>Attribute.aspx.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 WebApplication2
{
/// <summary>
/// Attribute 的摘要说明。
/// </summary>
public class Attribute : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Button Button1;
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
Button1.Attributes.Add("onclick","return confirm('Are You Sure?');"); } #region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.Load += new System.EventHandler(this.Page_Load); }
#endregion private void Button1_Click(object sender, System.EventArgs e)
{
Response.Write("Button1_click");
}
}
}
private void Page_Load(object sender, System.EventArgs e)
{
btnLink.Attributes.Add("onclick","return confirm('Are You Sure?');");
if(!Page.IsPostBack)
{
dgrdLinkview.DataSource=objLink.viewLink();
dgrdLinkview.DataBind();
}
} #region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load); }
#endregion private void btnLink_Click(object sender, System.EventArgs e)
{
if(Page.IsValid)
{
Response.Write("btnLin_Click");
objLink.addLink(txLinkname.Text,txLinkmemo.Text,txLinkurl.Text);
txLinkname.Text="";
txLinkmemo.Text="";
txLinkurl.Text="";
}
}这样写对吗?