如何prevent回传不使用JavaScript? [英] How to prevent PostBack without using JavaScript?
问题描述
我有一个asp按钮 Default.aspx的
:
<asp:Button ID="Button1" runat="server" Text="Button" />
和有一个程序 default.aspx.vb
:
Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Panel1.Controls.Add(New LiteralControl(Date.Now))
End Sub
和老师问我,如何使完全相同的操作(当点击按钮时,显示出来的日期),但没有回传,没有JavaScript的code。我可以做一个java code有并使用的OnClientClick =返回false
但似乎不对
And teacher asked me, how to make exactly same operation (when click on button, shows up a date) but without postback and without javascript code. I could make a java code there and use OnClientClick="return false"
but it seems not right
推荐答案
请参阅的 ASP.Net页面的客户端
Refer The Client Side of ASP.Net Pages
你不能这样做。按钮的默认行为(输入类型=提交)是提交/后
形式的url在表单的操作中指定
属性。
You can't do. The default behaviour of Button(input type=submit) is to submit/post
the form to the url specified in the form's action
attribute.
如果您想prevent默认行为,你需要写一个javascript 返回false
If you want to prevent default behaviour you need to write a javascript return false
<asp:Button ID="btn" OnClientClick="return false" runat="server" Text="Button" />
在默认情况下asp.net发帖的形式向同一个页面本身。我们说这是回发。在浏览器中呈现的HTML查看表单标签的行动值,这将是相同的页面名称
By Default asp.net post the form to the same page itself. We say this as PostBack. See the form tags action value from rendered html in browser , it will be the same page name
<input type =submit name ="btn" id="btn"
onclick="javascript:__doPostBack('btn','')"value ="Button">
下列内置的JavaScript code做到这一点。
The following built in javascript code does this
<script>
function __doPostBack(eventTarget, eventArgument) {
document.Form1.__EVENTTARGET.value = eventTarget;
document.Form1.__EVENTARGUMENT.value = eventArgument;
document.Form1.submit();
}
</script>
您网页会自动添加下面的字段,序检测需要服务器端被解雇的事件。您可以在一个请求参数,如 Request.Params只需访问[__ EVENTTARGET]
Your page will have the below fields added automatically, inorder to detect which event needs to be fired in server side. you can access simply in a request parameters like Request.Params["__EVENTTARGET"]
<input type =hidden name ="__EVENTTARGET" value ="">
<input type =hidden name ="__EVENTARGUMENT" value ="">
这篇关于如何prevent回传不使用JavaScript?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!