使用__doPostBack函數(shù)回送表單.
在asp.net中服務(wù)器控件回送表單是通過(guò)調(diào)用__doPostBack函數(shù)來(lái)回送表單,觸發(fā)事件的,先來(lái)看看__doPostBack函數(shù):
function __doPostBack(eventTarget, eventArgument) {
??? if (theForm.onsubmit == null || theForm.onsubmit()) {
??????? theForm.__EVENTTARGET.value = eventTarget;
??????? theForm.__EVENTARGUMENT.value = eventArgument;
??????? theForm.submit();
??? }
}
第一個(gè)參數(shù)是控件名稱,第二個(gè)參數(shù)包含事件的額外信息.
這個(gè)使用例子中,含有一個(gè)hiddenfield隱含控件和的一個(gè)linkbutton控件,hiddenfield用于保存要存接收的值,linkbutton用于觸發(fā)點(diǎn)擊事件void lbtnDeleteDiscussion_Click(object sender, EventArgs e)
例:
****
<asp:hiddenfield id="hidfDiscussionId" runat="server" />
<asp:linkbutton id="lbtnDeleteDiscussion" runat="server" onclick="lbtnDeleteDiscussion_Click"></asp:linkbutton>
*******************
??? void lbtnDeleteDiscussion_Click(object sender, EventArgs e)
??? {
??????? string[] roles = SiteSettings.Instance.ReviewAdminRoles.Split(',');
??????? bool isDelete = false;
??????? foreach (string role in roles)
??????? {
??????????? if (HttpContext.Current.User.IsInRole(role))
??????????? {
??????????????? isDelete = true;
??????????????? break;
??????????? }
??????? }
??????? if (isDelete)
??????? {
??????????? if (!string.IsNullOrEmpty(hidfDiscussionId.Value))
??????????? {
??????????????? int discussionId = int.Parse(hidfDiscussionId.Value);
??????????????? DiscussionManager.RemoveDiscussionItem(discussionId);
??????????????? Response.Redirect(Request.RawUrl);
??????????? }
??????? }
??? }
void Page_Load(object sender, EventArgs e)
{
????....
????sb.Remove(0, sb.Length);
????sb.Append("function DeleteDiscussion(discussionId){");
????sb.AppendFormat("confirm('{0}');", "確定要?jiǎng)h除這條討論嗎?");
????sb.AppendFormat("document.all('{0}').value = discussionId;", hidfDiscussionId.ClientID);
????sb.AppendFormat("__doPostBack('{0}','');", lbtnDeleteDiscussion.ClientID.Replace('_', '$'));
????sb.Append("}");
????Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "DeleteDiscussion", sb.ToString(), true);
}
****************
現(xiàn)在在客戶端使用HTML代碼也可以可以觸發(fā)指定的服務(wù)器端事件并且傳遞一些值了.
如:
?sb.AppendFormat("<a href=\"javascript:DeleteDiscussion('{0}');\">",item.DiscussionId);
?sb.AppendFormat("<img src='{0}' alt='{1}' border='{2}' />",
CommerceContext.GetThemesImagePathForImgTag("Common/button_delete.gif"),
?"刪除討論","0");
sb.Append("</a>");
這里使用html代碼也以觸發(fā)一個(gè)刪除事件,把指定的項(xiàng)刪除.
這個(gè)例子是我上一篇Blog的相關(guān)內(nèi)容,給合Cilent CallBack可以實(shí)現(xiàn)無(wú)刷新地動(dòng)態(tài)生成的html代碼,并可以觸發(fā)服務(wù)器端事件
function __doPostBack(eventTarget, eventArgument) {
??? if (theForm.onsubmit == null || theForm.onsubmit()) {
??????? theForm.__EVENTTARGET.value = eventTarget;
??????? theForm.__EVENTARGUMENT.value = eventArgument;
??????? theForm.submit();
??? }
}
第一個(gè)參數(shù)是控件名稱,第二個(gè)參數(shù)包含事件的額外信息.
這個(gè)使用例子中,含有一個(gè)hiddenfield隱含控件和的一個(gè)linkbutton控件,hiddenfield用于保存要存接收的值,linkbutton用于觸發(fā)點(diǎn)擊事件void lbtnDeleteDiscussion_Click(object sender, EventArgs e)
例:
****
<asp:hiddenfield id="hidfDiscussionId" runat="server" />
<asp:linkbutton id="lbtnDeleteDiscussion" runat="server" onclick="lbtnDeleteDiscussion_Click"></asp:linkbutton>
*******************
??? void lbtnDeleteDiscussion_Click(object sender, EventArgs e)
??? {
??????? string[] roles = SiteSettings.Instance.ReviewAdminRoles.Split(',');
??????? bool isDelete = false;
??????? foreach (string role in roles)
??????? {
??????????? if (HttpContext.Current.User.IsInRole(role))
??????????? {
??????????????? isDelete = true;
??????????????? break;
??????????? }
??????? }
??????? if (isDelete)
??????? {
??????????? if (!string.IsNullOrEmpty(hidfDiscussionId.Value))
??????????? {
??????????????? int discussionId = int.Parse(hidfDiscussionId.Value);
??????????????? DiscussionManager.RemoveDiscussionItem(discussionId);
??????????????? Response.Redirect(Request.RawUrl);
??????????? }
??????? }
??? }
void Page_Load(object sender, EventArgs e)
{
????....
????sb.Remove(0, sb.Length);
????sb.Append("function DeleteDiscussion(discussionId){");
????sb.AppendFormat("confirm('{0}');", "確定要?jiǎng)h除這條討論嗎?");
????sb.AppendFormat("document.all('{0}').value = discussionId;", hidfDiscussionId.ClientID);
????sb.AppendFormat("__doPostBack('{0}','');", lbtnDeleteDiscussion.ClientID.Replace('_', '$'));
????sb.Append("}");
????Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "DeleteDiscussion", sb.ToString(), true);
}
****************
現(xiàn)在在客戶端使用HTML代碼也可以可以觸發(fā)指定的服務(wù)器端事件并且傳遞一些值了.
如:
?sb.AppendFormat("<a href=\"javascript:DeleteDiscussion('{0}');\">",item.DiscussionId);
?sb.AppendFormat("<img src='{0}' alt='{1}' border='{2}' />",
CommerceContext.GetThemesImagePathForImgTag("Common/button_delete.gif"),
?"刪除討論","0");
sb.Append("</a>");
這里使用html代碼也以觸發(fā)一個(gè)刪除事件,把指定的項(xiàng)刪除.
這個(gè)例子是我上一篇Blog的相關(guān)內(nèi)容,給合Cilent CallBack可以實(shí)現(xiàn)無(wú)刷新地動(dòng)態(tài)生成的html代碼,并可以觸發(fā)服務(wù)器端事件
posted on 2006-11-07 11:36 風(fēng)雨兼程 閱讀(245) 評(píng)論(0) 編輯 收藏