为什么__doPostBack(clientid,"");是不是在做postpack?

问题描述:

你好,



这是我的编码。当我在文本框中键入任何文本并在javascript中分配gridview客户端ID时,javascript函数onkeyup正在调用。但是为什么它不是Posting Back(为什么它不会在语言后面编码 - aspx.cs)..我已经工作了4天......真的我无法搞清楚。如果有人知道,请帮助我。谢谢。



这里我的编码:

hello,

this is my coding. the javascript function onkeyup is invoking when i type any text on textbox and assigning gridview client id inside the javascript. But why it is not Posting Back (Why it is not going to code behind language - aspx.cs)..I have been working on 4 days..really i am not able to figure out. if anybody knows, help me. Thank you.

here my coding:

<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"

CodeBehind="Default.aspx.cs" Inherits="WebApplication6._Default" %>

<asp:Content ID="contentid" runat="server" ContentPlaceHolderID="ContentPlaceHolder2">
<script type="text/javascript" language="javascript">
function fun() {
debugger;
var x = document.getElementById('<%=GridView1.ClientID %>')
__doPostBack("<%=GridView1.ClientID%>", " ");

}
</script>

<asp:TextBox ID="txt" onkeyup="fun();" runat="server"></asp:TextBox>

<div>
<asp:GridView ID="GridView1" runat="server" >
<Columns>
<asp:TemplateField>
<ItemTemplate>
<table>
<tr>
<td><%# Eval("question") %></td>
</tr>
<tr>
<td><%# Eval("answer") %></td>
</tr>

</table>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>

</asp:Content>



aspx .cs(代码隐藏)


aspx.cs (codebehind)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

namespace WebApplication6
{
    public partial class _Default : System.Web.UI.Page
    {
        DataTable dt = new DataTable();
        protected void Page_Load(object sender, EventArgs e)
        {
        dt.Columns.Add("question", typeof(string));
        dt.Columns.Add("answer", typeof(string));
        dt.Rows.Add("What is IT", "information technology is changing everything");
        GridView1.DataSource = dt;
        GridView1.DataBind();
    }
}
}

问题

>
__ doPostBack 是一个 JavaScript 函数,自动添加到提交表单中,从而导致回发。这是为Web控件添加的任何事件将导致回发



在您的情况下,您没有页面上的任何内容,都会导致回发。因此,此功能未添加到 Page ,它在开发者控制台窗口上显示以下错误。



ReferenceError:未定义__doPostBack

__ doPostBack(GridView1,);

解决方案



我找到的最简单的方法是添加属性 AutoPostBack =true TextBox 。所以它会变成......

Problem


__doPostBack is a JavaScript function automatically added to Submit the form resulting in a Post Back. This is added for the Web Controls having any Event which will cause a Post Back.

In your case, you have don't have anything on the page, which would cause a Post Back. So, this function is not added to the Page, which shows the following Error on Developer Console Window.

ReferenceError: __doPostBack is not defined
__doPostBack("GridView1", " ");

Solution


The easiest way I found is to add attribute AutoPostBack="true" to the TextBox. So, it will become...
<asp:TextBox ID="txt" onkeyup="fun();" runat="server" AutoPostBack="true"></asp:TextBox>



现在,您可以看到 __ doPostBack 被添加到t在浏览器上呈现页面脚本后。


Now, you can see __doPostBack is added to the Page Script after it is rendered on Browser.

function __doPostBack(eventTarget, eventArgument) {
    if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
        theForm.__EVENTTARGET.value = eventTarget;
        theForm.__EVENTARGUMENT.value = eventArgument;
        theForm.submit();
    }
}



在此之后,让我们在 TextBox 上输入内容,它会回发,但抛出异常 ...


After this, lets type something on the TextBox, it will Post Back, but throws Exception...

Invalid postback or callback argument.  Event validation is enabled using <pages enableeventvalidation="true" /> in configuration or <%@ Page EnableEventValidation="true" %> in a page.  For security purposes, this feature verifies that arguments to postback or callback events originate from the server control that originally rendered them.  If the data is valid and expected, use the ClientScriptManager.RegisterForEventValidation method in order to register the postback or callback data for validation.



要删除此数据,您可以添加 EnableEventValidation =false指向 Page Directive ,不推荐使用。



参考 - Page.EnableEventValidation Property [ ^ ]


To remove this, you can add EnableEventValidation="false" to the Page Directive which is not recommended.

Refer - Page.EnableEventValidation Property[^]

Quote:

如果编写客户端脚本在运行时更改客户端中的控件,则可能必须使用RegisterForEventValidation [ ^ ]方法,以避免错误事件验证错误。



安全说明

此功能可降低未经授权或恶意回发请求和回调的风险。强烈建议您不要禁用事件验证。

If you write client script that changes a control in the client at run time, you might have to use the RegisterForEventValidation[^] method in order to avoid false event validation errors.

Security Note
This feature reduces the risk of unauthorized or malicious postback requests and callbacks. It is strongly recommended that you do not disable event validation.



如果设置 EnableEventValidation =false,那么它会发布根据您的要求返回。但我不建议这样做。

结论



请重新考虑逻辑并想想你为什么要明确回复。您可以直接将事件直接添加到 GridView ,也可以使用 RegisterForEventValidation [ ^ ]方法。


If you set EnableEventValidation="false", then it would Post Back acoording to your requirements. But I would not suggest that.

Conclusion


Please reconsider the Logic and think why you want to Post Back explicitly. You can directly add Events to GridView directly or use RegisterForEventValidation[^] method.