What's wrong in this code ??

P

pamelafluente

I am trying to pass a javascript variable to ASP.

It almost works, except for the postback part. Infact if one clicks on
the div and on the button it can be seen that the variable passes
through. Can anyone help to do the postback?

Here is my complete code:

<%@ Page Language="VB" AutoEventWireup="false"
CodeFile="Default.aspx.vb" Inherits="_Default" %>

<!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 runat="server">
<title>Test Page</title>

<script type="text/javascript" language="JavaScript">

function Hi(MyControlID)
{
var obj = document.getElementById("Hidden1");
obj.value = MyControlID;
__doPostBack(MyControlID,'');
}

</script>
</head>
<body>
<form id="form1" runat="server">
<br />
<br />

<div id="MyDiv" style="width: 100px; height: 100px;
background-color:green" onclick = "Hi('MyDiv')" runat="server" >
</div>

<asp:Button ID="Button1" runat="server" Text="Button" />

<%--Hidden field to store the ID of control clicked--%>
<input id="Hidden1" type="hidden" language="javascript"
runat="server" /><br />


</form>
</body>

</html>


Code behind is:

Partial Class _Default
Inherits System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load
Response.Write(Me.Hidden1.Value)
End Sub

End Class
 
T

Teemu Keiski

Hi,

you don't need to do all that.

Just change the div into something like:

div id="MyDiv" style="width: 100px; height: 100px; background-color:green"
runat="server" >
</div>

and in code

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
Handles Me.Load
Dim options As New PostBackOptions(MyDiv, "")
MyDiv.Attributes("onclick") =
Page.ClientScript.GetPostBackEventReference(options)

If Page.IsPostBack Then
Response.Write(Request.Form("__EVENTTARGET"))
End If
End Sub

Now, it posts back, and via _EVENTTARGET you get which control caused it (as
that you seemed to want to catch). Note that getting postbacking control
isn't always available __EVENTTARGET which is for only control postbacking
with __doPostBack call.
 
P

pamelafluente

Thank you Teemu! Very elegant and beautiful asp solution!!

-pam

PS
Still I have the doubt why the __doPostBack(MyControlID,'');
did not work while the submit works (?)

Teemu Keiski ha scritto:
 
T

Teemu Keiski

Hi,

because __doPostBack script is outputted only when you have a control
requiring javascript for postback mechanism. By calling
Page.ClientScript.GetPostBackEventReference you are essentially telling that
you need "help" from the page framework to get pieces for postback e.g it
causes __doPostBack call to be generated.

Button doesn't need __doPostBack it is able to submit as-is (since it's
<input type=submit> at client), therefore it worked.
 
P

pamelafluente

Ahh, Thanks, now understand. My mistake was then to assume that the
_doPostBack script was there in any case...

One last thing. You have provided a beautiful solution to be able to
attach the postback to any element of the page and then to retrieve its
properties (its ID).

One modification I need is that the control causing postback (submit)
is on one page (plain Html) but its ID or other information are passed
(redirect) to another ASP page which would do server side processing
and output a new html page.

Is it possible to modify Your solution to obtain that? Thank you

-Pam

Teemu Keiski ha scritto:
 

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

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top