ValueChanged Event Doesn't Fire

Discussion in 'ASP .Net' started by =?Utf-8?B?cmZpbmNo?=, Oct 26, 2006.

  1. I'm trying to pass two values from client script (Javascript) to the
    code-behind for a page using three hidden fields - two to hold the values and
    a third to fire the ValueChanged event.

    I have tried every method I can think of to get the event to fire, but it
    will not, so either I am missing some code or am misunderstanding the nature
    of the event. If anyone can review the test code posted below (which I
    created to try to debug the problem) and provide some help, it would be much
    appreciated.

    What I'm doing is using a Javascript function (moveValues()) to copy
    whatever is typed into the two text boxes to the hidden fields, and also
    change the value of the third hidden field in an attempt to fire the event.
    I've used the VS2005 debugging facility to confirm that the Javascript
    function is working correctly; however, the c# code is never called.

    Web form code:
    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs"
    Inherits="HFTest" %>

    <!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>Hidden Field Test</title>
    <script type="text/javascript" src="script.js"></script>
    </head>
    <body>
    <form id="form1" runat="server">
    <div>
    <asp:HiddenField ID="HiddenField1" runat="server" value="" />
    <asp:HiddenField ID="HiddenField2" runat="server" value="" />
    <asp:HiddenField ID="HiddenField3" runat="server" value=""
    OnValueChanged="OnHiddenValuesChanged" />
    </div>
    <div>
    <input id="Text1" type="text" />
    <br />
    <input id="Text2" type="text" />
    <br />
    <input id="Button2" type="button" value="Submit"
    onclick="moveValues()" />
    <br />
    </div>
    <div id="results" runat="server">
    </div>
    </form>
    </body>
    </html>

    Javascript function:
    function moveValues()
    {
    var txt1 = document.getElementById('Text1').getAttribute('value');
    var txt2 = document.getElementById('Text2').getAttribute('value');
    document.getElementById('HiddenField1').setAttribute('value', txt1);
    document.getElementById('HiddenField2').setAttribute('value', txt2);

    form1.HiddenField3.value = 'change';
    }

    Code-behind function:
    protected void OnHiddenValuesChanged(object sender, EventArgs e)
    {
    StringBuilder sb = new StringBuilder("<b>Text Box 1: </b>");
    sb.Append(HiddenField1.Value);
    sb.Append("<br />");
    sb.Append("<b>Text Box 2: </b>");
    sb.Append(HiddenField2.Value);

    results.InnerHtml = sb.ToString();
    }
     
    =?Utf-8?B?cmZpbmNo?=, Oct 26, 2006
    #1
    1. Advertising

  2. Your OnHiddenValuesChanged eventhander is only going to be fired if there is
    a postback. From the looks of what code you posted, all you have is a
    client-side script function that changes the values.
    Hope that helps.
    Peter

    --
    Co-founder, Eggheadcafe.com developer portal:
    http://www.eggheadcafe.com
    UnBlog:
    http://petesbloggerama.blogspot.com




    "rfinch" wrote:

    > I'm trying to pass two values from client script (Javascript) to the
    > code-behind for a page using three hidden fields - two to hold the values and
    > a third to fire the ValueChanged event.
    >
    > I have tried every method I can think of to get the event to fire, but it
    > will not, so either I am missing some code or am misunderstanding the nature
    > of the event. If anyone can review the test code posted below (which I
    > created to try to debug the problem) and provide some help, it would be much
    > appreciated.
    >
    > What I'm doing is using a Javascript function (moveValues()) to copy
    > whatever is typed into the two text boxes to the hidden fields, and also
    > change the value of the third hidden field in an attempt to fire the event.
    > I've used the VS2005 debugging facility to confirm that the Javascript
    > function is working correctly; however, the c# code is never called.
    >
    > Web form code:
    > <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs"
    > Inherits="HFTest" %>
    >
    > <!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>Hidden Field Test</title>
    > <script type="text/javascript" src="script.js"></script>
    > </head>
    > <body>
    > <form id="form1" runat="server">
    > <div>
    > <asp:HiddenField ID="HiddenField1" runat="server" value="" />
    > <asp:HiddenField ID="HiddenField2" runat="server" value="" />
    > <asp:HiddenField ID="HiddenField3" runat="server" value=""
    > OnValueChanged="OnHiddenValuesChanged" />
    > </div>
    > <div>
    > <input id="Text1" type="text" />
    > <br />
    > <input id="Text2" type="text" />
    > <br />
    > <input id="Button2" type="button" value="Submit"
    > onclick="moveValues()" />
    > <br />
    > </div>
    > <div id="results" runat="server">
    > </div>
    > </form>
    > </body>
    > </html>
    >
    > Javascript function:
    > function moveValues()
    > {
    > var txt1 = document.getElementById('Text1').getAttribute('value');
    > var txt2 = document.getElementById('Text2').getAttribute('value');
    > document.getElementById('HiddenField1').setAttribute('value', txt1);
    > document.getElementById('HiddenField2').setAttribute('value', txt2);
    >
    > form1.HiddenField3.value = 'change';
    > }
    >
    > Code-behind function:
    > protected void OnHiddenValuesChanged(object sender, EventArgs e)
    > {
    > StringBuilder sb = new StringBuilder("<b>Text Box 1: </b>");
    > sb.Append(HiddenField1.Value);
    > sb.Append("<br />");
    > sb.Append("<b>Text Box 2: </b>");
    > sb.Append(HiddenField2.Value);
    >
    > results.InnerHtml = sb.ToString();
    > }
    >
     
    =?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=, Oct 26, 2006
    #2
    1. Advertising

  3. Hi Peter -

    Yes, it does, as I suspected it might have something to do with that. I'm
    trying to work out how I can force a postback. Any pointers?

    Cheers,
    Ron

    "Peter Bromberg [C# MVP]" wrote:

    > Your OnHiddenValuesChanged eventhander is only going to be fired if there is
    > a postback. From the looks of what code you posted, all you have is a
    > client-side script function that changes the values.
    > Hope that helps.
    > Peter
    >
    > --
    > Co-founder, Eggheadcafe.com developer portal:
    > http://www.eggheadcafe.com
    > UnBlog:
    > http://petesbloggerama.blogspot.com
    >
    >
    >
    >
    > "rfinch" wrote:
    >
    > > I'm trying to pass two values from client script (Javascript) to the
    > > code-behind for a page using three hidden fields - two to hold the values and
    > > a third to fire the ValueChanged event.
    > >
    > > I have tried every method I can think of to get the event to fire, but it
    > > will not, so either I am missing some code or am misunderstanding the nature
    > > of the event. If anyone can review the test code posted below (which I
    > > created to try to debug the problem) and provide some help, it would be much
    > > appreciated.
    > >
    > > What I'm doing is using a Javascript function (moveValues()) to copy
    > > whatever is typed into the two text boxes to the hidden fields, and also
    > > change the value of the third hidden field in an attempt to fire the event.
    > > I've used the VS2005 debugging facility to confirm that the Javascript
    > > function is working correctly; however, the c# code is never called.
    > >
    > > Web form code:
    > > <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs"
    > > Inherits="HFTest" %>
    > >
    > > <!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>Hidden Field Test</title>
    > > <script type="text/javascript" src="script.js"></script>
    > > </head>
    > > <body>
    > > <form id="form1" runat="server">
    > > <div>
    > > <asp:HiddenField ID="HiddenField1" runat="server" value="" />
    > > <asp:HiddenField ID="HiddenField2" runat="server" value="" />
    > > <asp:HiddenField ID="HiddenField3" runat="server" value=""
    > > OnValueChanged="OnHiddenValuesChanged" />
    > > </div>
    > > <div>
    > > <input id="Text1" type="text" />
    > > <br />
    > > <input id="Text2" type="text" />
    > > <br />
    > > <input id="Button2" type="button" value="Submit"
    > > onclick="moveValues()" />
    > > <br />
    > > </div>
    > > <div id="results" runat="server">
    > > </div>
    > > </form>
    > > </body>
    > > </html>
    > >
    > > Javascript function:
    > > function moveValues()
    > > {
    > > var txt1 = document.getElementById('Text1').getAttribute('value');
    > > var txt2 = document.getElementById('Text2').getAttribute('value');
    > > document.getElementById('HiddenField1').setAttribute('value', txt1);
    > > document.getElementById('HiddenField2').setAttribute('value', txt2);
    > >
    > > form1.HiddenField3.value = 'change';
    > > }
    > >
    > > Code-behind function:
    > > protected void OnHiddenValuesChanged(object sender, EventArgs e)
    > > {
    > > StringBuilder sb = new StringBuilder("<b>Text Box 1: </b>");
    > > sb.Append(HiddenField1.Value);
    > > sb.Append("<br />");
    > > sb.Append("<b>Text Box 2: </b>");
    > > sb.Append(HiddenField2.Value);
    > >
    > > results.InnerHtml = sb.ToString();
    > > }
    > >
     
    =?Utf-8?B?cmZpbmNo?=, Oct 26, 2006
    #3
  4. Ok, well the simplest way to create a postback is to have a control that
    when changed, will create one by having it's AutoPostBack property set to
    "true". A Button will always create a postback, all you need to do is click
    it.
    Peter

    --
    Co-founder, Eggheadcafe.com developer portal:
    http://www.eggheadcafe.com
    UnBlog:
    http://petesbloggerama.blogspot.com




    "rfinch" wrote:

    > Hi Peter -
    >
    > Yes, it does, as I suspected it might have something to do with that. I'm
    > trying to work out how I can force a postback. Any pointers?
    >
    > Cheers,
    > Ron
    >
    > "Peter Bromberg [C# MVP]" wrote:
    >
    > > Your OnHiddenValuesChanged eventhander is only going to be fired if there is
    > > a postback. From the looks of what code you posted, all you have is a
    > > client-side script function that changes the values.
    > > Hope that helps.
    > > Peter
    > >
    > > --
    > > Co-founder, Eggheadcafe.com developer portal:
    > > http://www.eggheadcafe.com
    > > UnBlog:
    > > http://petesbloggerama.blogspot.com
    > >
    > >
    > >
    > >
    > > "rfinch" wrote:
    > >
    > > > I'm trying to pass two values from client script (Javascript) to the
    > > > code-behind for a page using three hidden fields - two to hold the values and
    > > > a third to fire the ValueChanged event.
    > > >
    > > > I have tried every method I can think of to get the event to fire, but it
    > > > will not, so either I am missing some code or am misunderstanding the nature
    > > > of the event. If anyone can review the test code posted below (which I
    > > > created to try to debug the problem) and provide some help, it would be much
    > > > appreciated.
    > > >
    > > > What I'm doing is using a Javascript function (moveValues()) to copy
    > > > whatever is typed into the two text boxes to the hidden fields, and also
    > > > change the value of the third hidden field in an attempt to fire the event.
    > > > I've used the VS2005 debugging facility to confirm that the Javascript
    > > > function is working correctly; however, the c# code is never called.
    > > >
    > > > Web form code:
    > > > <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs"
    > > > Inherits="HFTest" %>
    > > >
    > > > <!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>Hidden Field Test</title>
    > > > <script type="text/javascript" src="script.js"></script>
    > > > </head>
    > > > <body>
    > > > <form id="form1" runat="server">
    > > > <div>
    > > > <asp:HiddenField ID="HiddenField1" runat="server" value="" />
    > > > <asp:HiddenField ID="HiddenField2" runat="server" value="" />
    > > > <asp:HiddenField ID="HiddenField3" runat="server" value=""
    > > > OnValueChanged="OnHiddenValuesChanged" />
    > > > </div>
    > > > <div>
    > > > <input id="Text1" type="text" />
    > > > <br />
    > > > <input id="Text2" type="text" />
    > > > <br />
    > > > <input id="Button2" type="button" value="Submit"
    > > > onclick="moveValues()" />
    > > > <br />
    > > > </div>
    > > > <div id="results" runat="server">
    > > > </div>
    > > > </form>
    > > > </body>
    > > > </html>
    > > >
    > > > Javascript function:
    > > > function moveValues()
    > > > {
    > > > var txt1 = document.getElementById('Text1').getAttribute('value');
    > > > var txt2 = document.getElementById('Text2').getAttribute('value');
    > > > document.getElementById('HiddenField1').setAttribute('value', txt1);
    > > > document.getElementById('HiddenField2').setAttribute('value', txt2);
    > > >
    > > > form1.HiddenField3.value = 'change';
    > > > }
    > > >
    > > > Code-behind function:
    > > > protected void OnHiddenValuesChanged(object sender, EventArgs e)
    > > > {
    > > > StringBuilder sb = new StringBuilder("<b>Text Box 1: </b>");
    > > > sb.Append(HiddenField1.Value);
    > > > sb.Append("<br />");
    > > > sb.Append("<b>Text Box 2: </b>");
    > > > sb.Append(HiddenField2.Value);
    > > >
    > > > results.InnerHtml = sb.ToString();
    > > > }
    > > >
     
    =?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=, Oct 26, 2006
    #4
    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. Replies:
    1
    Views:
    587
    Lee Weiner
    Nov 29, 2003
  2. =?Utf-8?B?Y2luZHk=?=
    Replies:
    0
    Views:
    517
    =?Utf-8?B?Y2luZHk=?=
    Sep 21, 2006
  3. Replies:
    4
    Views:
    549
  4. gsz

    valueChanged

    gsz, Aug 2, 2007, in forum: Java
    Replies:
    0
    Views:
    428
  5. gsz
    Replies:
    1
    Views:
    697
    Andrew Thompson
    Aug 9, 2007
Loading...

Share This Page