Click here to Skip to main content
15,886,026 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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:
XML
<%@ 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 (codebehind)
C#
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();
    }
}
}
Posted
v2
Comments
samit kaneriya 25-Jan-14 1:31am    
hi __doPostBack is work with control [name] attribute so please check in client side your gridview which [name] generate in HTML Source

1 solution

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...
XML
<asp:TextBox ID="txt" onkeyup="fun();" runat="server" AutoPostBack="true"></asp:TextBox>

Now, you can see __doPostBack is added to the Page Script after it is rendered on Browser.
JavaScript
function __doPostBack(eventTarget, eventArgument) {
    if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
        theForm.__EVENTTARGET.value = eventTarget;
        theForm.__EVENTARGUMENT.value = eventArgument;
        theForm.submit();
    }
}

After this, lets type something on the TextBox, it will Post Back, but throws Exception...
HTML
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.

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

Refer - Page.EnableEventValidation Property[^]
Quote:
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.

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.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900