asp教程.net clientscript.registerclientscriptblock用法
第一個是 registerstartups教程cript 方法。當您有一個想要在頁面載入時啟動的
網頁特效 函數時,最好使用該類
<%@ page language="c#" autoeventwireup="true" codefile="default.aspx.cs"
inherits="default" %>
<!doctype html public "-//w3c//dtd xhtml 1.1//en"
"http://www.w3.org/tr/xhtml11/dtd/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>untitled page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:wizard id="wizard1" runat="server">
<wizardsteps>
<asp:wizardstep runat="server" title="step 1">
<asp:textbox id="textbox1" runat="server"
></asp:textbox>
<asp:button id="button1"
runat="server" text="button"
onclientclick="test()"/>
</asp:wizardstep>
<asp:wizardstep runat="server" title="step 2">
</asp:wizardstep>
</wizardsteps>
</asp:wizard>
</div>
</form>
</body>
</html>
file: default.aspx.cs
using system;
using system.data;
using system.configuration;
using system.collections;
using system.web;
using system.web.security;
using system.web.ui;
using system.web.ui.webcontrols;
using system.web.ui.webcontrols.webparts;
using system.web.ui.htmlcontrols;
public partial class default : system.web.ui.page
{
protected void page_load(object sender, eventargs e)
{
string jscript=
"<script language=javascript> n" +
"<!-- n" +
"function test()n" +
"{ n" +
"var what = document.forms['form1']; n" +
"if (!what) { n" +
"what = document.form1; n" +
"} n" +
"if ( what." + this.textbox1.clientid + ".value=='' ) n" +
"{ n" +
"alert('you entered: nothing!');n" +
"}n" +
"elsen" +
"{n" +
"alert('you entered: ' + what." + this.textbox1.clientid +
".value);" +
"}n" +
"}n" +
"//--></script>n";
clientscript.registerclientscriptblock(typeof(page),"test",jscript);
}
}
javascript 添加到位於 asp.net教程 頁面中的某個特定伺服器控制項是非常簡單的。我
們以按鈕伺服器控制項為例。如果您使用任一 microsoft visual studio 2005 將
button html 伺服器控制項(htmlinputbutton 類)拖放到某個頁面中,並將其作為
伺服器控制項運行,則應具有以下代碼結構:
<input id="button1" type="button" value="button" runat="server" />
這是一個普通按鈕,可通過 asp.net 頁面的代碼分離或伺服器端指令碼以編程方式
對其進行控制。例如,要在產生頁面時指定按鈕文本,只需在該元素變成 html 服
務器控制項(按右鍵該控制項,然後選擇 run as server control(作為伺服器控制項
運行))後使用該按鈕的 value 屬性即可。