question regarding __doPostBack

M

Marcelo

Hello,
I am trying to create a postback event, and it is working, just not calling
the sub.
I have a datagrid which has
<asp:DataGrid id="Mensajes"
...
<Columns>
<asp:HyperLinkColumn
....

DataNavigateUrlFormatString="javascript:__doPostBack('loadMessage','{0}')"

I also placed the following javascript function:
function __doPostBack(eventTarget, eventArgument) {
var theform;
if (window.navigator.appName.toLowerCase().indexOf("netscape")
theform = document.forms["_ctl0"];
}
else {
theform = document._ctl0;
}
theform.__EVENTTARGET.value = eventTarget.split("$").join(":");
theform.__EVENTARGUMENT.value = eventArgument;
theform.submit();
}


In the Sub Page_Load
I have
response.write ("__EVENTTARGET " & request("__EVENTTARGET") & "<br>")
response.write ("__EVENTARGUMENT " & request("__EVENTARGUMENT") & "<br>")

and I do receive both parameters, but the sub 'loadMessage' never gets
called.
Right now I patched it using a switch/case __EVENTTARGET then calling the
sub "manually".

What did I miss?

Thanks
 
H

Henri

First, instead or creating the _postback javascript code yourself, you'd
better use :

myHref = Page.GetPostBackClientHyperlink(myControl, myArgs)

The problem or your code is that the first argument of __doPostBack should
not be the name of a sub, but the name of the control that fired the
postback event.

To solve your problem, you'd better use a BoundColumn instead of a
HyperLinkColumn:

<asp:BoundColumn onDataBinding="myColumn_DataBinding" />

Sub myColumn_DataBinding(sender As Object, e As EventArgs)
Dim cell As TableCell = CType(sender, TableCell)
Dim oLink As New LinkButton()
cell.Controls.Add(oLink)
oLink.Text = "Click me"
AddHandler oLink.Click, AddressOf loadMessage
End Sub

Sub loadMessage
End Sub

Hope it helps

Henri


Marcelo said:
Hello,
I am trying to create a postback event, and it is working, just not calling
the sub.
I have a datagrid which has
<asp:DataGrid id="Mensajes"
...
<Columns>
<asp:HyperLinkColumn
....

DataNavigateUrlFormatString="javascript:__doPostBack('loadMessage','{0}')"

I also placed the following javascript function:
function __doPostBack(eventTarget, eventArgument) {
var theform;
if (window.navigator.appName.toLowerCase().indexOf("netscape")
theform = document.forms["_ctl0"];
}
else {
theform = document._ctl0;
}
theform.__EVENTTARGET.value = eventTarget.split("$").join(":");
theform.__EVENTARGUMENT.value = eventArgument;
theform.submit();
}


In the Sub Page_Load
I have
response.write ("__EVENTTARGET " & request("__EVENTTARGET") & "<br>")
response.write ("__EVENTARGUMENT " & request("__EVENTARGUMENT") &
 
M

Marcelo

Hello,
Thanks for your help, I am having some problems implementing this code,
these are the error messages I am getting:

<asp:BoundColumn onDataBinding="myColumn_DataBinding" />
onDataBinding is not a property of a column, it can only be added to the
code of the dataGrid.

Dim cell As TableCell = CType(sender, TableCell)
gives me the error: System.InvalidCastException: Specified cast is not
valid.

Thanks.


----- Original Message -----
From: "Henri" <[email protected]>
Newsgroups: microsoft.public.dotnet.framework.aspnet
Sent: Saturday, December 11, 2004 2:06 PM
Subject: Re: question regarding __doPostBack

First, instead or creating the _postback javascript code yourself, you'd
better use :

myHref = Page.GetPostBackClientHyperlink(myControl, myArgs)

The problem or your code is that the first argument of __doPostBack should
not be the name of a sub, but the name of the control that fired the
postback event.

To solve your problem, you'd better use a BoundColumn instead of a
HyperLinkColumn:

<asp:BoundColumn onDataBinding="myColumn_DataBinding" />

Sub myColumn_DataBinding(sender As Object, e As EventArgs)
Dim cell As TableCell = CType(sender, TableCell)
Dim oLink As New LinkButton()
cell.Controls.Add(oLink)
oLink.Text = "Click me"
AddHandler oLink.Click, AddressOf loadMessage
End Sub

Sub loadMessage
End Sub

Hope it helps

Henri


Marcelo said:
Hello,
I am trying to create a postback event, and it is working, just not calling
the sub.
I have a datagrid which has
<asp:DataGrid id="Mensajes"
...
<Columns>
<asp:HyperLinkColumn
....

DataNavigateUrlFormatString="javascript:__doPostBack('loadMessage','{0}')"

I also placed the following javascript function:
function __doPostBack(eventTarget, eventArgument) {
var theform;
if (window.navigator.appName.toLowerCase().indexOf("netscape")
theform = document.forms["_ctl0"];
}
else {
theform = document._ctl0;
}
theform.__EVENTTARGET.value = eventTarget.split("$").join(":");
theform.__EVENTARGUMENT.value = eventArgument;
theform.submit();
}


In the Sub Page_Load
I have
response.write ("__EVENTTARGET " & request("__EVENTTARGET") & "<br>")
response.write ("__EVENTARGUMENT " & request("__EVENTARGUMENT") &
and I do receive both parameters, but the sub 'loadMessage' never gets
called.
Right now I patched it using a switch/case __EVENTTARGET then calling the
sub "manually".

What did I miss?

Thanks
 
H

Henri

Sorry, I forgot an important part of the code:
You have to create your custom BoundColumn declaring a class that inherits
BoundColumn:

'*******************************************
NameSpace MyNameSpace

Public Class MyBoundColumn
Inherits System.Web.UI.WebControls.BoundColumn

Public Event DataBinding As EventHandler

Public Overrides Sub InitializeCell(ByVal cell As TableCell, ByVal
columnIndex As Integer, ByVal itemType As ListItemType)
MyBase.InitializeCell(cell, columnIndex, itemType)
Select Case itemType
Case ListItemType.Item, ListItemType.AlternatingItem
AddHandler cell.DataBinding, AddressOf CreateItem
End Select

End Sub

Private Sub CreateItem(ByVal sender As Object, ByVal e As EventArgs)
Dim cell As TableCell = CType(sender, TableCell)
RaiseEvent DataBinding(cell, EventArgs.Empty)

End Sub

End Class

End NameSpace
'******************************************************************

Compile this class in MyAssembly.dll and put it in your .bin directory

Then in your .aspx page :

<%@ Register TagPrefix="myprefix" Namespace="myNameSpace"
Assembly="MyAssembly" %>

......

<asp:DataGrid .... >
...
<myprefix:MyBoundColumn onDataBinding="myColumn_DataBinding" />
...
</asp:DataGrid>


If you need more information about custom columns, try:
http://msdn.microsoft.com/library/d...y/en-us/dnaspp/html/creatingcustomcolumns.asp

Hope it helps

Henri


Marcelo said:
Hello,
Thanks for your help, I am having some problems implementing this code,
these are the error messages I am getting:

<asp:BoundColumn onDataBinding="myColumn_DataBinding" />
onDataBinding is not a property of a column, it can only be added to the
code of the dataGrid.

Dim cell As TableCell = CType(sender, TableCell)
gives me the error: System.InvalidCastException: Specified cast is not
valid.

Thanks.


----- Original Message -----
From: "Henri" <[email protected]>
Newsgroups: microsoft.public.dotnet.framework.aspnet
Sent: Saturday, December 11, 2004 2:06 PM
Subject: Re: question regarding __doPostBack

First, instead or creating the _postback javascript code yourself, you'd
better use :

myHref = Page.GetPostBackClientHyperlink(myControl, myArgs)

The problem or your code is that the first argument of __doPostBack should
not be the name of a sub, but the name of the control that fired the
postback event.

To solve your problem, you'd better use a BoundColumn instead of a
HyperLinkColumn:

<asp:BoundColumn onDataBinding="myColumn_DataBinding" />

Sub myColumn_DataBinding(sender As Object, e As EventArgs)
Dim cell As TableCell = CType(sender, TableCell)
Dim oLink As New LinkButton()
cell.Controls.Add(oLink)
oLink.Text = "Click me"
AddHandler oLink.Click, AddressOf loadMessage
End Sub

Sub loadMessage
End Sub

Hope it helps

Henri


Marcelo said:
Hello,
I am trying to create a postback event, and it is working, just not calling
the sub.
I have a datagrid which has
<asp:DataGrid id="Mensajes"
...
<Columns>
<asp:HyperLinkColumn
....

DataNavigateUrlFormatString="javascript:__doPostBack('loadMessage','{0}')"

I also placed the following javascript function:
function __doPostBack(eventTarget, eventArgument) {
var theform;
if (window.navigator.appName.toLowerCase().indexOf("netscape")
-1) {
theform = document.forms["_ctl0"];
}
else {
theform = document._ctl0;
}
theform.__EVENTTARGET.value = eventTarget.split("$").join(":");
theform.__EVENTARGUMENT.value = eventArgument;
theform.submit();
}


In the Sub Page_Load
I have
response.write ("__EVENTTARGET " & request("__EVENTTARGET") &
response.write ("__EVENTARGUMENT " & request("__EVENTARGUMENT") &
and I do receive both parameters, but the sub 'loadMessage' never gets
called.
Right now I patched it using a switch/case __EVENTTARGET then calling the
sub "manually".

What did I miss?

Thanks
 

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

Top