而confirm 中true和False會做不同事情(導到不同網頁)。
--HTML
< asp:Button id="btnSure" runat="server" OnClick="btnSure_Click" UseSubmitBehavior="false" Style="display: none" /> < href="#" id="btnSureJavaScript" runat="server" style="display: none"/> < asp:Button id="btnHidden" runat="server" OnClick="btnHidden_Click" UseSubmitBehavior="false" Style="display: none" />
--C#
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { btnSureJavaScript.Attributes["onclick"] = string.Format(@"if (confirm('{0}')) {{document.getElementById('{2}').onclick();}} else {{window.location.href='default.aspx?ActivityId={1}';}}","你要套用嗎", ActivityId.ToString(), btnHidden.ClientID); } } protected void btnHidden_Click(object sender, EventArgs e) { } protected void btnSure_Click(object sender, EventArgs e) { string js = string.Format(@"window.setTimeout(""document.getElementById('{0}').onclick();"", 0);", btnSureJavaScript.ClientID); Page.ClientScript.RegisterStartupScript(GetType(), "EvalList_eventHandle", GenClientScript.JavaScript(js)); }
--重點
UseSubmitBehavior="false" -- Button加上UseSubmitBehavior
document.getElementById('ID').onclick()--用onclick()而不要用click()
沒有留言:
張貼留言