Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ValueChanged Event Doesn't Fire

Reply
Thread Tools

ValueChanged Event Doesn't Fire

 
 
=?Utf-8?B?cmZpbmNo?=
Guest
Posts: n/a
 
      10-26-2006
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('val ue');
var txt2 = document.getElementById('Text2').getAttribute('val ue');
document.getElementById('HiddenField1').setAttribu te('value', txt1);
document.getElementById('HiddenField2').setAttribu te('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();
}

 
Reply With Quote
 
 
 
 
=?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=
Guest
Posts: n/a
 
      10-26-2006
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('val ue');
> var txt2 = document.getElementById('Text2').getAttribute('val ue');
> document.getElementById('HiddenField1').setAttribu te('value', txt1);
> document.getElementById('HiddenField2').setAttribu te('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();
> }
>

 
Reply With Quote
 
 
 
 
=?Utf-8?B?cmZpbmNo?=
Guest
Posts: n/a
 
      10-26-2006
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('val ue');
> > var txt2 = document.getElementById('Text2').getAttribute('val ue');
> > document.getElementById('HiddenField1').setAttribu te('value', txt1);
> > document.getElementById('HiddenField2').setAttribu te('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();
> > }
> >

 
Reply With Quote
 
=?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=
Guest
Posts: n/a
 
      10-26-2006
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('val ue');
> > > var txt2 = document.getElementById('Text2').getAttribute('val ue');
> > > document.getElementById('HiddenField1').setAttribu te('value', txt1);
> > > document.getElementById('HiddenField2').setAttribu te('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();
> > > }
> > >

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
JTree: ValueChanged event not firing gsz Java 1 08-09-2007 10:27 AM
valueChanged gsz Java 0 08-02-2007 11:54 AM
ImageButton event doesn't fire but Button event does suzanne.boyle@gmail.com ASP .Net 4 06-13-2007 09:19 PM
dynamic datagrid dropdown valuechanged repopulate 2nd list editmo =?Utf-8?B?Y2luZHk=?= ASP .Net 0 09-21-2006 04:40 PM
mysterious Jlist behaviour (valuechanged) wnstnsmith@yahoo.com Java 1 11-29-2003 02:33 AM



Advertisments