What's wrong in this code ??

Discussion in 'ASP .Net' started by pamelafluente@libero.it, Aug 19, 2006.

  1. Guest

    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, 2006
    #1
    1. Advertising

  2. Teemu Keiski Guest

    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.


    --
    Teemu Keiski
    ASP.NET MVP, AspInsider
    Finland, EU
    http://blogs.aspadvice.com/joteke


    <> wrote in message
    news:...
    >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
    >
    Teemu Keiski, Aug 20, 2006
    #2
    1. Advertising

  3. Guest

    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(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.
    >
    >
    > --
    > Teemu Keiski
    > ASP.NET MVP, AspInsider
    > Finland, EU
    > http://blogs.aspadvice.com/joteke
    >
    >
    > <> wrote in message
    > news:...
    > >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, 2006
    #3
  4. Teemu Keiski Guest

    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.

    --
    Teemu Keiski
    ASP.NET MVP, AspInsider
    Finland, EU
    http://blogs.aspadvice.com/joteke


    <> wrote in message
    news:...
    > 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(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.
    >>
    >>
    >> --
    >> Teemu Keiski
    >> ASP.NET MVP, AspInsider
    >> Finland, EU
    >> http://blogs.aspadvice.com/joteke
    >>
    >>
    >> <> wrote in message
    >> news:...
    >> >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
    >> >

    >
    Teemu Keiski, Aug 20, 2006
    #4
  5. Guest

    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=submit> at client), therefore it worked.
    >
    > --
    > Teemu Keiski
    > ASP.NET MVP, AspInsider
    > Finland, EU
    > http://blogs.aspadvice.com/joteke
    >
    >
    > <> wrote in message
    > news:...
    > > 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(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.
    > >>
    > >>
    > >> --
    > >> Teemu Keiski
    > >> ASP.NET MVP, AspInsider
    > >> Finland, EU
    > >> http://blogs.aspadvice.com/joteke
    > >>
    > >>
    > >> <> wrote in message
    > >> news:...
    > >> >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, 2006
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. walala
    Replies:
    3
    Views:
    2,184
    Ralf Hildebrandt
    Sep 10, 2003
  2. willem oosthuizen

    What is wrong with the following code?

    willem oosthuizen, Oct 10, 2003, in forum: VHDL
    Replies:
    9
    Views:
    1,265
  3. Matthew
    Replies:
    7
    Views:
    665
    Priscilla Walmsley
    Jan 7, 2005
  4. David. E. Goble
    Replies:
    9
    Views:
    473
    David. E. Goble
    Feb 2, 2005
  5. kiran
    Replies:
    12
    Views:
    1,115
    Scott Sauyet
    Dec 7, 2011
Loading...

Share This Page