關(guān)于前臺調(diào)用后臺事件__doPostBack函數(shù)
更新時間:2007年04月13日 00:00:00 作者:
這里需要提一下的是,asp.net編程提供了服務(wù)端控件和客戶端控件的說法,其實還是脫離不了HTML的本質(zhì),客戶端和服務(wù)端需要交互必須要提交,提交有兩種方式get和post,get就是通過向服務(wù)端發(fā)送連接地址,服務(wù)端通過地址的參數(shù)來獲得信息的,一般這些參數(shù)都是明文,能在瀏覽器地址欄看到。而post是通過表單的input等元素提交到服務(wù)端的頁面的,這些數(shù)據(jù)一般是看不到的。asp.net的服務(wù)端控件其實就是對一般的HTML控件做了個包裝,大體是通過隱藏控件提供控制的參數(shù)的。
這里介紹一個常用的函數(shù)_doPostBack,這個函數(shù)如果如果是ASP.Net render出來的頁面就是自動產(chǎn)生這個函數(shù),比如有帶autopostback屬性的控件,且其屬性為true的頁面,帶編輯列的datagrid頁面。
__doPostBack是通過__EVENTTARGET,__EVENTARGUMENT兩個隱藏控件向服務(wù)端發(fā)送控制信息的,__EVENTTARGET為要調(diào)用控件的名稱,如果要調(diào)用的控件是子控件,用''$'或':'分割父控件:子控件,__EVENTARGUMENT是調(diào)用事件時的參數(shù)
下面演示下如何調(diào)用后臺事件:
1.新建工程
2.拖入一個服務(wù)端Button1,一個DropDownList1和一個客戶端Button
3.設(shè)置DropDownList1的AutoPostBack屬性為True,Button1的Visible為False
4.雙擊Button1,在事件里寫下Response.Write("hello:" );
5.頁面的HTML里找到客戶端Button,寫入onclick="__doPostBack('Button1','')"
6.編譯,運行,點擊Button是不是出現(xiàn)了"Hello"
7.查看源代碼,發(fā)現(xiàn)里面多了下面行
<script language="javascript">
<!--
function __doPostBack(eventTarget, eventArgument) {
var theform;
if (window.navigator.appName.toLowerCase().indexOf("netscape") > -1) {
theform = document.forms["Form1"];
}
else {
theform = document.Form1;
}
theform.__EVENTTARGET.value = eventTarget.split("$").join(":");
theform.__EVENTARGUMENT.value = eventArgument;
theform.submit();
}
// -->
</script>
以及兩個隱藏控件
<input type="hidden" name="__EVENTTARGET" value="" />
<input type="hidden" name="__EVENTARGUMENT" value="" />
值得注意的是,_doPostPack的第一個參數(shù)是大小寫不敏感的
細(xì)心的人會發(fā)現(xiàn),在__doPostBack里,提交調(diào)用的是theform.submit(),這樣就導(dǎo)致對Form的onsubmit事件校驗失效了,幸好這個問題在asp.net 2.0已經(jīng)修復(fù)了。這里提供一個替換的解決辦法,在Form的最下面插入下面的代碼,這段代碼在保證不管是不是render出來的頁面均有效
<script language="javascript">
<!--
function __doPostBack_Ex(eventTarget, eventArgument)
{
var theform;
if (window.navigator.appName.toLowerCase().indexOf("netscape") > -1) {
theform = document.forms[0];
}
else {
theform = document.forms[0];
}
if(!theform.__EVENTTARGET)
{
theform.appendChild(document.createElement("<input type='hidden' name='__EVENTTARGET'>"));
}
if(!theform.__EVENTARGUMENT)
{
theform.appendChild(document.createElement("<input type='hidden' name='__EVENTARGUMENT'>"));
}
theform.__EVENTTARGET.value = eventTarget.split("$").join(":");
theform.__EVENTARGUMENT.value = eventArgument;
if ((typeof(theform.onsubmit) == "function"))
{
if(theform.onsubmit()!=false)
{
theform.submit();
}
}
else
{
theform.submit();
}
function __doPostBack(eventTarget, eventArgument)
{
__doPostBack_Ex(eventTarget, eventArgument);
}
}
// -->
</script>
這里介紹一個常用的函數(shù)_doPostBack,這個函數(shù)如果如果是ASP.Net render出來的頁面就是自動產(chǎn)生這個函數(shù),比如有帶autopostback屬性的控件,且其屬性為true的頁面,帶編輯列的datagrid頁面。
__doPostBack是通過__EVENTTARGET,__EVENTARGUMENT兩個隱藏控件向服務(wù)端發(fā)送控制信息的,__EVENTTARGET為要調(diào)用控件的名稱,如果要調(diào)用的控件是子控件,用''$'或':'分割父控件:子控件,__EVENTARGUMENT是調(diào)用事件時的參數(shù)
下面演示下如何調(diào)用后臺事件:
1.新建工程
2.拖入一個服務(wù)端Button1,一個DropDownList1和一個客戶端Button
3.設(shè)置DropDownList1的AutoPostBack屬性為True,Button1的Visible為False
4.雙擊Button1,在事件里寫下Response.Write("hello:" );
5.頁面的HTML里找到客戶端Button,寫入onclick="__doPostBack('Button1','')"
6.編譯,運行,點擊Button是不是出現(xiàn)了"Hello"
7.查看源代碼,發(fā)現(xiàn)里面多了下面行
復(fù)制代碼 代碼如下:
<script language="javascript">
<!--
function __doPostBack(eventTarget, eventArgument) {
var theform;
if (window.navigator.appName.toLowerCase().indexOf("netscape") > -1) {
theform = document.forms["Form1"];
}
else {
theform = document.Form1;
}
theform.__EVENTTARGET.value = eventTarget.split("$").join(":");
theform.__EVENTARGUMENT.value = eventArgument;
theform.submit();
}
// -->
</script>
以及兩個隱藏控件
復(fù)制代碼 代碼如下:
<input type="hidden" name="__EVENTTARGET" value="" />
<input type="hidden" name="__EVENTARGUMENT" value="" />
值得注意的是,_doPostPack的第一個參數(shù)是大小寫不敏感的
細(xì)心的人會發(fā)現(xiàn),在__doPostBack里,提交調(diào)用的是theform.submit(),這樣就導(dǎo)致對Form的onsubmit事件校驗失效了,幸好這個問題在asp.net 2.0已經(jīng)修復(fù)了。這里提供一個替換的解決辦法,在Form的最下面插入下面的代碼,這段代碼在保證不管是不是render出來的頁面均有效
復(fù)制代碼 代碼如下:
<script language="javascript">
<!--
function __doPostBack_Ex(eventTarget, eventArgument)
{
var theform;
if (window.navigator.appName.toLowerCase().indexOf("netscape") > -1) {
theform = document.forms[0];
}
else {
theform = document.forms[0];
}
if(!theform.__EVENTTARGET)
{
theform.appendChild(document.createElement("<input type='hidden' name='__EVENTTARGET'>"));
}
if(!theform.__EVENTARGUMENT)
{
theform.appendChild(document.createElement("<input type='hidden' name='__EVENTARGUMENT'>"));
}
theform.__EVENTTARGET.value = eventTarget.split("$").join(":");
theform.__EVENTARGUMENT.value = eventArgument;
if ((typeof(theform.onsubmit) == "function"))
{
if(theform.onsubmit()!=false)
{
theform.submit();
}
}
else
{
theform.submit();
}
function __doPostBack(eventTarget, eventArgument)
{
__doPostBack_Ex(eventTarget, eventArgument);
}
}
// -->
</script>
相關(guān)文章
asp.net(c#)實現(xiàn)從sqlserver存取二進(jìn)制圖片的代碼
有一個員工表Employee,需要保存員工照片(Photo)到數(shù)據(jù)庫(sql server)上。員工照片對應(yīng)的字段是varbinary(max),也就是要存成二進(jìn)制文件類型(這和以前討巧地存圖片文件路徑就不相同了),默認(rèn)可以為空。2011-09-09
基于ASP.NET+EasyUI框架實現(xiàn)圖片上傳提交表單功能(js提交圖片)
這篇文章主要介紹了基于ASP.NET+EasyUI框架實現(xiàn)圖片上傳再提交表單(js提交圖片)的相關(guān)資料,需要的朋友可以參考下2016-06-06

