__doPostBack hijacking doesn't seem to work

Joined
Oct 21, 2006
Messages
1
Reaction score
0
Hi all...
I've been messing around with the __doPostBack for a while - under asp.net 2.0
what I have is a gridview that whenever a line is pressed it postback with
a few functions that all run well before the postback.
when I had a control that asp.net compiler enter an automatic __dopostback I can't seem to hijack it - it just doesn't send the right data - it always
seems to go to asp's function last...

code :

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" EnableEventValidation="false"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>Untitled Page</title>
<style type="text/css">
.normalrow
{
background-color:#ffffff;
}

.selectedrow
{
background-color:#ff0022;
}
</style>
<script language="javascript" type="text/javascript">
var gridViewCtlId = '<%=ctlGridView.ClientID%>';
var gridViewCtl = null;
var curSelRow = null;





function getGridViewControl()
{
if (null == gridViewCtl)
{
gridViewCtl = document.getElementById(gridViewCtlId);
}
}

var __oldDoPostBack = null
function onGridViewRowSelected(rowIdx)
{

__oldDoPostBack = __doPostBack; var selRow = getSelectedRow(rowIdx);
if (curSelRow != null)
{
curSelRow.style.backgroundColor = '#ffffff';
}

if (null != selRow)
{
curSelRow = selRow;
curSelRow.style.backgroundColor = '#ff0022';
}
// __doPostBack = AlwaysFireBeforeFormSubmit(gridViewCtl,rowIdx,__oldDoPostBack);
return __oldDoPostBack(gridViewCtl,rowIdx);
}

function AlwaysFireBeforeFormSubmit (eventTarget, eventArgument,postback) {
// var __oldDoPostBack = __doPostBack;
return postback (eventTarget, eventArgument);

}


function getSelectedRow(rowIdx)
{
getGridViewControl();
if (null != gridViewCtl)
{
return gridViewCtl.rows[rowIdx];
}
return null;
}
</script>
<script language="javascript" type="text/javascript">
function __doPostBack(eventTarget, eventArgument) {
if (theForm.onsubmit == null || theForm.onsubmit()) {
var theform = document.form1;
theform.__EVENTTARGET.value = eventTarget;
theform.__EVENTARGUMENT.value = eventArgument;
theform.submit();
}
}

</script>
</head>

<body>
<form id="form1" runat="server">
<input type="hidden" name="__EVENTTARGET" value="" />
<input type="hidden" name="__EVENTARGUMENT" value="" />
<div>
<asp:GridView ID="ctlGridView" runat="server" OnRowCreated="OnRowCreated">
<selectedrowstyle backcolor="#FFFFC0" />
</asp:GridView>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
<asp:LinkButton ID="LinkButton1" runat="server">LinkButton</asp:LinkButton>
<asp:GridView ID="GridView1" runat="server" BackColor="LightGoldenrodYellow"
BorderColor="Tan" BorderWidth="1px" CellPadding="2" DataSourceID="SqlDataSource1"
EmptyDataText="There are no data records to display." ForeColor="Black" GridLines="None">
<FooterStyle BackColor="Tan" />
<SelectedRowStyle BackColor="DarkSlateBlue" ForeColor="GhostWhite" />
<PagerStyle BackColor="PaleGoldenrod" ForeColor="DarkSlateBlue" HorizontalAlign="Center" />
<HeaderStyle BackColor="Tan" Font-Bold="True" />
<AlternatingRowStyle BackColor="PaleGoldenrod" />
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:YekevConnectionString1 %>"
ProviderName="<%$ ConnectionStrings:YekevConnectionString1.ProviderName %>" SelectCommand="SELECT * FROM [Stock_General]">
</asp:SqlDataSource>
&nbsp;
</div>
</form>
</body>
</html>


The codebehind :

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;


public partial class _Default : System.Web.UI.Page
{
private DataSet m_dsProducts;
private Int32 m_iRowIdx;

protected void Page_Load(object sender, EventArgs e)
{
getProducts();
if (!IsPostBack)
{
bindGridView();
}
else
{
object obTarget = this.Request.Form["__EVENTTARGET"];
object obArg = this.Request.Form["__EVENTARGUMENT"];

if (null != obArg)
{
// int rowIdx = Int32.Parse(obArg.ToString());
// OnRowSelected(rowIdx);
}
}
}

#region GridView Methods
#region Data Binding
private void bindGridView()
{
ctlGridView.DataSource = m_dsProducts;
ctlGridView.DataBind();
}
#endregion

#region Event Handlers
protected void OnRowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes.Add("onclick", "onGridViewRowSelected('" + m_iRowIdx.ToString() + "')");
}
m_iRowIdx++;
}

protected void OnRowSelected(Int32 iRowIdx)
{

}
#endregion
#endregion

#region Data Methods
private void getProducts()
{
if (!IsPostBack)
{
m_dsProducts = new DataSet();
SqlDataAdapter adpter=new SqlDataAdapter();
SqlConnection con=new SqlConnection();
con.ConnectionString=SqlDataSource1.ConnectionString;
SqlCommand cmd = new SqlCommand(SqlDataSource1.SelectCommand.ToString(), con);
adpter.SelectCommand = cmd;
adpter.Fill(m_dsProducts, "Stock_General");
Session["_Products_Set_"] = m_dsProducts;
}
else
{
m_dsProducts = Session["_Products_Set_"] as DataSet;
}
}
#endregion
protected void Button1_Click(object sender, EventArgs e)
{

}
}


must of the code can be disregarded - the problem occurs
in the Page_load (When to postback happens)
the EVENTTARGET & EVENTARGU don't get the right values...
what Can I DO???
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top