Click here to Skip to main content
14,880,690 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
i am using grid view to show data from the database using asp.net c#. i have enabled edit and delete options of the grid view but once i click on the edit or delete button system is showing below error

JavaScript runtime error: '__doPostBack' is undefined

What I have tried:

asp.net

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
            DataKeyNames="serial_no" OnPageIndexChanging="GridView1_PageIndexChanging" 
            OnRowCancelingEdit="GridView1_RowCancelingEdit" 
            OnRowDeleting="GridView1_RowDeleting" OnRowEditing="GridView1_RowEditing" 
            OnRowUpdating="GridView1_RowUpdating">
    <columns>
        <asp:BoundField DataField="serial_no" HeaderText="serial_no" 
            InsertVisible="False" ReadOnly="True" SortExpression="serial_no" />
        <asp:BoundField DataField="internal_receiving" HeaderText="internal_receiving" 
            SortExpression="internal_receiving" />
        <asp:BoundField DataField="date" HeaderText="date" SortExpression="date" />
        <asp:BoundField DataField="department" HeaderText="department" 
            SortExpression="department" />
        <asp:BoundField DataField="department_type" HeaderText="department_type" 
            SortExpression="department_type" />
        <asp:BoundField DataField="organization" HeaderText="organization" 
            SortExpression="organization" />
        <asp:BoundField DataField="organization_type" HeaderText="organization_type" 
            SortExpression="organization_type" />
        <asp:BoundField DataField="file_no" HeaderText="file_no" 
            SortExpression="file_no" />
        <asp:BoundField DataField="subject" HeaderText="subject" 
            SortExpression="subject" />
        <asp:BoundField DataField="file_name" HeaderText="file_name" 
            SortExpression="file_name" />
        <asp:BoundField DataField="status" HeaderText="status" 
            SortExpression="status" />
             <asp:CommandField ShowEditButton="true" />
        <asp:CommandField ShowDeleteButton="true" />




c#

public partial class viewfile : System.Web.UI.Page
{
    private SqlConnection conn = new SqlConnection("Data Source=.\\sqlexpress;Initial Catalog=college_education;User ID=sa;Password=system;");

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            gvbind();
        }
    }

    protected void gvbind()
    {
      
            conn.Open();
            SqlCommand cmd = new SqlCommand("Select * from file_rec", conn);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            da.Fill(ds);
            conn.Close();
            if (ds.Tables[0].Rows.Count > 0)
            {
                GridView1.DataSource = ds;
                GridView1.DataBind();
            }
            else
            {
                ds.Tables[0].Rows.Add(ds.Tables[0].NewRow());
                GridView1.DataSource = ds;
                GridView1.DataBind();
                int columncount = GridView1.Rows[0].Cells.Count;
                GridView1.Rows[0].Cells.Clear();
                GridView1.Rows[0].Cells.Add(new TableCell());
                GridView1.Rows[0].Cells[0].ColumnSpan = columncount;
                GridView1.Rows[0].Cells[0].Text = "No Records Found";
            }
      
    }

    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];
        Label lbldeleteid = (Label)row.FindControl("lblID");
        conn.Open();
        SqlCommand cmd = new SqlCommand("delete FROM file_rec where serial_no='" + Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value.ToString()) + "'", conn);
        cmd.ExecuteNonQuery();
        conn.Close();
        gvbind();

    }

    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        gvbind();
    }
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        int serial_no = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value.ToString());
        GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];
        Label lblID = (Label)row.FindControl("lblID");
        //TextBox txtname=(TextBox)gr.cell[].control[];
        TextBox textinternal_receiving = (TextBox)row.Cells[0].Controls[0];
        TextBox textdate = (TextBox)row.Cells[1].Controls[0];
        TextBox textdepartment = (TextBox)row.Cells[2].Controls[0];
        TextBox textdepartment_type = (TextBox)row.Cells[3].Controls[0];
        TextBox textorganization = (TextBox)row.Cells[4].Controls[0];
        TextBox textorganization_type = (TextBox)row.Cells[5].Controls[0];
        TextBox textfile_no = (TextBox)row.Cells[6].Controls[0];
        TextBox textsubject = (TextBox)row.Cells[7].Controls[0];
        TextBox textfile_name = (TextBox)row.Cells[8].Controls[0];
        TextBox textstatus = (TextBox)row.Cells[9].Controls[0];
        //TextBox textadd = (TextBox)row.FindControl("txtadd");
        //TextBox textc = (TextBox)row.FindControl("txtc");
        GridView1.EditIndex = -1;
        conn.Open();
        //SqlCommand cmd = new SqlCommand("SELECT * FROM detail", conn);
        SqlCommand cmd = new SqlCommand("update file_rec set internal_receiving='" + textinternal_receiving + "',date='" + textdate + "',department='" + textdepartment + "',department_type='" + textdepartment_type + "',organization='" + textorganization + "',organization_type='" + textorganization_type + "',file_no='" + textfile_no + "',subject='" + textsubject + "',file_name='" + textfile_name + "',status='" + textstatus + "'where ='" + serial_no + "'", conn);
        cmd.ExecuteNonQuery();
        conn.Close();
        gvbind();
        //GridView1.DataBind();
    }
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        gvbind();
    }
    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView1.EditIndex = -1;
        gvbind();
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        Response.Redirect("adminview.aspx");
    }
}
Posted
Updated 1-Feb-17 21:19pm
v3
Comments
[no name] 1-Feb-17 11:34am
   
"JavaScript runtime error:", other than the SQL injection attack invitation I do not see a single bit of javascript anywhere in your posting. But I didn't go through your unformatted code dump very carefully
Member 12950401 1-Feb-17 11:39am
   
i have not used java script in the code but i dont know why this error is shown
Richard Deeming 1-Feb-17 14:02pm
   
Are you running on .NET 2.0 or 4.0? There was a bug with the browser definition files which was fixed in 2011, but you might be missing the patch:
Bug and Fix: ASP.NET fails to detect IE10 causing _doPostBack is undefined JavaScript error or maintain FF5 scrollbar position[^]

As mentioned in the comments, setting Page.ClientTarget = "uplevel" in the Page_Init event would also avoid this bug.
Member 12950401 1-Feb-17 21:14pm
   
i am using VS2010, V4.0
Bryian Tan 1-Feb-17 23:30pm
   
Since you guy mention SQL injection. I want to throw in another example. Hopefully it will help to understand how SQL injection works.

SQL-Injection-and-Cross-Site-Scripting

1 solution

The Form that displays this view in must have runat="server" declared. In the case that you have this added and are still not seeing the reference, add the following to your page load method, ClientScriptManager.GetPostBackEventReference.
   
Comments
Member 12950401 2-Feb-17 9:14am
   
where exactly i should put this...

ClientScriptManager.GetPostBackEventReference is showing below error.

"only assigments , call,increment decrement and new object expressions can be used as a statement "
Pete O'Hanlon 2-Feb-17 10:11am
   
That's because you're missing a (); off the end. I just put the call you'd need - I didn't think I'd have to spell it out exactly.
Member 12950401 22-Mar-17 22:06pm
   
Sir i am still not able to solve the problem.

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