473,387 Members | 1,624 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

What's wrong in this code ??

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

Aug 19 '06 #1
4 1281
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(option s)

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.
--
Teemu Keiski
ASP.NET MVP, AspInsider
Finland, EU
http://blogs.aspadvice.com/joteke
<pa***********@libero.itwrote in message
news:11**********************@b28g2000cwb.googlegr oups.com...
>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

Aug 20 '06 #2
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:
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(option s)

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.
--
Teemu Keiski
ASP.NET MVP, AspInsider
Finland, EU
http://blogs.aspadvice.com/joteke
<pa***********@libero.itwrote in message
news:11**********************@b28g2000cwb.googlegr oups.com...
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
Aug 20 '06 #3
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=submitat client), therefore it worked.

--
Teemu Keiski
ASP.NET MVP, AspInsider
Finland, EU
http://blogs.aspadvice.com/joteke
<pa***********@libero.itwrote in message
news:11**********************@i42g2000cwa.googlegr oups.com...
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:
>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(optio ns)

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.
--
Teemu Keiski
ASP.NET MVP, AspInsider
Finland, EU
http://blogs.aspadvice.com/joteke
<pa***********@libero.itwrote in message
news:11**********************@b28g2000cwb.googleg roups.com...
>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

Aug 20 '06 #4
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:
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=submitat client), therefore it worked.

--
Teemu Keiski
ASP.NET MVP, AspInsider
Finland, EU
http://blogs.aspadvice.com/joteke
<pa***********@libero.itwrote in message
news:11**********************@i42g2000cwa.googlegr oups.com...
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:
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(option s)

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.
--
Teemu Keiski
ASP.NET MVP, AspInsider
Finland, EU
http://blogs.aspadvice.com/joteke
<pa***********@libero.itwrote in message
news:11**********************@b28g2000cwb.googlegr oups.com...
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
Aug 20 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

125
by: Sarah Tanembaum | last post by:
Beside its an opensource and supported by community, what's the fundamental differences between PostgreSQL and those high-price commercial database (and some are bloated such as Oracle) from...
72
by: E. Robert Tisdale | last post by:
What makes a good C/C++ programmer? Would you be surprised if I told you that it has almost nothing to do with your knowledge of C or C++? There isn't much difference in productivity, for...
121
by: typingcat | last post by:
First of all, I'm an Asian and I need to input Japanese, Korean and so on. I've tried many PHP IDEs today, but almost non of them supported Unicode (UTF-8) file. I've found that the only Unicode...
51
by: WindAndWaves | last post by:
Can anyone tell me what is wrong with the goto command. I noticed it is one of those NEVER USE. I can understand that it may lead to confusing code, but I often use it like this: is this...
46
by: Keith K | last post by:
Having developed with VB since 1992, I am now VERY interested in C#. I've written several applications with C# and I do enjoy the language. What C# Needs: There are a few things that I do...
13
by: Jason Huang | last post by:
Hi, Would someone explain the following coding more detail for me? What's the ( ) for? CurrentText = (TextBox)e.Item.Cells.Controls; Thanks. Jason
1
by: GS | last post by:
I got a combobox box that I load at load time. the Item and vales ended up in reverse order of each other, what went wrong? the database table has the following row code value ebay ...
98
by: tjb | last post by:
I often see code like this: /// <summary> /// Removes a node. /// </summary> /// <param name="node">The node to remove.</param> public void RemoveNode(Node node) { <...> }
9
by: Pyenos | last post by:
import cPickle, shelve could someone tell me what things are wrong with my code? class progress: PROGRESS_TABLE_ACTIONS= DEFAULT_PROGRESS_DATA_FILE="progress_data" PROGRESS_OUTCOMES=
20
by: Daniel.C | last post by:
Hello. I just copied this code from my book with no modification : #include <stdio.h> /* count characters in input; 1st version */ main() { long nc; nc = 0;
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.