473,738 Members | 7,110 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Postback of controls which have been modified in javascript

mc
I'm writing an app for managing Task Lists, I'm trying to add some
controls to a form that I can use to link tasks, my original intention
was to: -

Add two list boxes, one listing "all Tasks" and the other listing
"Linked Tasks", I have two buttons which in javascript move items from
the first list into the second.

Then when a user clicks a thrid button I would like to save this data.

however, the first problem was that the Event Validator threw a wobly
and rejected my postback.

I turned off EventValidation for the page.

I then noticed that the control state was the same as it was when I
loaded the form. even though I changed the contents of the ListBox?

Hope this makes sense, it's been a long day!

TIA
MC
Nov 20 '06 #1
15 3622
I've done similar things before, and it requires a little bit of process.
First, create a hidden control (<input type="hidden" name="MyHiddenI tem" />
(make sure you give it a name attribute)) to store the values that are in
your Linked Tasks listbox. Then, on your form submit event, store the
values from the Linked Tasks listbox as a comma-delimited list in the hidden
control. Finally, server-side, check the Request.Form collection for the
Value of your Linked Tasks listbox. This should give you what you want.

Don't get wobbly, now! :)
Jim

"mc" <mc@community.n ospamwrote in message
news:45******** @mail.hmgcc.gov .uk...
I'm writing an app for managing Task Lists, I'm trying to add some
controls to a form that I can use to link tasks, my original intention was
to: -

Add two list boxes, one listing "all Tasks" and the other listing "Linked
Tasks", I have two buttons which in javascript move items from the first
list into the second.

Then when a user clicks a thrid button I would like to save this data.

however, the first problem was that the Event Validator threw a wobly and
rejected my postback.

I turned off EventValidation for the page.

I then noticed that the control state was the same as it was when I loaded
the form. even though I changed the contents of the ListBox?

Hope this makes sense, it's been a long day!

TIA
MC

Nov 20 '06 #2
Thanks for Jim's informative input.

Hi MC,

For the two issues you mentioned, they're due to the following things:

1. The ASP.NET Webcontrol's state is stored in ViewState by default and it
is a compressed and encoded format which can not be modified at
client-side. Thus, any change to the webcontrol's state(except those
property that will be postback every time such as TextBox's Text or List's
selectedItem) at client-side will not be automatically post to server-side.

2. For the EventValidation problem, this is due to the new event validation
in ASP.NET 2.0(by default enabled), it will ensure that all the postback on
the page are raised by the built-in postback script generated by the page
or any webcontrol rather than other custom code(directly injected scripts).
For example, if you use Response.Write to output some postback scripts,
it will violate the event validation.

For the two issues, you can consider the following means to work them out:

1) As Jim suggested, you can use an html input hidden field(<input
type="hidden" .../>) in the page and at client-side, whenever the
dropdownlist has been changed(add or remove items), you update the hidden
field so as to persist the changes. After the page postback, you can check
the hidden field's value and synchorize the status into DropDownlist's
server control instance.

2) If disable EventValidation is doable, you can just turn off it on this
page. Else, since EventValidation just ensure the postback is really raised
from the control self, you can use script to invoke a certain control's
postback event. e.g.

We can use script to trigger a button's click event at client-side:

document.getEle mentById("Butto n1").click();
How do you think? If you have any other consideration or ideas, please feel
free to post here.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead

=============== =============== =============== =====

Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.

=============== =============== =============== =====

This posting is provided "AS IS" with no warranties, and confers no rights.

Nov 21 '06 #3
mc
Ok, I get the first solution with the hidden field, I've implemented it
ok and my code works a treat.

Maybe it's too late in the evening but I'm having a little difficulty
working out the EventValidiatio n stuff.

I'd rather not have to disable event validation for the whole page.

could you explain a little more about what would i need to to to stop
the eventValidation error occurring (without turning off event validation)?

I've wrapped my module is a web control as it will be used elsewhere, in
my app, will this help or hinder the problem?

TIA

mc

Steven Cheng[MSFT] wrote:
Thanks for Jim's informative input.

Hi MC,

For the two issues you mentioned, they're due to the following things:

1. The ASP.NET Webcontrol's state is stored in ViewState by default and it
is a compressed and encoded format which can not be modified at
client-side. Thus, any change to the webcontrol's state(except those
property that will be postback every time such as TextBox's Text or List's
selectedItem) at client-side will not be automatically post to server-side.

2. For the EventValidation problem, this is due to the new event validation
in ASP.NET 2.0(by default enabled), it will ensure that all the postback on
the page are raised by the built-in postback script generated by the page
or any webcontrol rather than other custom code(directly injected scripts).
For example, if you use Response.Write to output some postback scripts,
it will violate the event validation.

For the two issues, you can consider the following means to work them out:

1) As Jim suggested, you can use an html input hidden field(<input
type="hidden" .../>) in the page and at client-side, whenever the
dropdownlist has been changed(add or remove items), you update the hidden
field so as to persist the changes. After the page postback, you can check
the hidden field's value and synchorize the status into DropDownlist's
server control instance.

2) If disable EventValidation is doable, you can just turn off it on this
page. Else, since EventValidation just ensure the postback is really raised
from the control self, you can use script to invoke a certain control's
postback event. e.g.

We can use script to trigger a button's click event at client-side:

document.getEle mentById("Butto n1").click();
How do you think? If you have any other consideration or ideas, please feel
free to post here.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead

=============== =============== =============== =====

Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.

=============== =============== =============== =====

This posting is provided "AS IS" with no warranties, and confers no rights.


Nov 21 '06 #4
Hello MC,

Thanks for your reply.

As for the EventValidation problem, here is my understanding:

First, the "EventValidatio n" feature in ASP.NET 2.0 will perform the
following check when a page is postback(if EnableEventVali dation is
enabled):

1. It will ensure that any data in the Request.Form collection(the data
post through html input elements....) are originated from existing web
controls on the page. For example, if you dynamically use server-side or
client-side code to output the a following html input element

"<input type='text' name='xxx'...../>",

then, when postback, since this input field is not originally in the page's
control collection, it will report Event Validation error.

2. It will ensure that a postback event (such as Button Click) is really
triggered by the corresponding html element (rendered by the server-side
control). That means, if we use our own script to call "__doPostba ck"
fundtion to simulate another button's click event, it will raise error at
server-side (if event validation is turn on).
for your scenario, are you calling the "__doPostba ck" function in your own
script to simulate another control's postback? If you're going to use own
script to trigger a button's postback event, you can consider use script to
reference the button's client element and call its "click()" method. e.g.

var btn = document.getEle mentbyId("btnSu bmit");
btn.click();

Please feel free to post here if you have any further questions.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead

=============== =============== =============== =====

Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.

=============== =============== =============== =====

This posting is provided "AS IS" with no warranties, and confers no rights.
Nov 23 '06 #5
mc
I think that my scenario is somewhat simpler than the one you explain.

Unfortunately I can't post the code as I do not have access to it from
this terminal.

I've written a web User control which contains two list boxes, two image
buttons and a TaskId property.

The image buttons have no event handler on the server side and are
purely used (client side) to move items between the lists. No postback
occurs when either of these buttons are clicked.

Upon loading the control I initialize it by loading all the tasks into
the first list box. I have two methods Load_Data and Save_Data.

The load data method shuffles the contents of the lists around
server-side so they match the list (for the specified TaskId) stored in
the db

The save data method uses the values stored in the hidden control to
update the list (for the specified TaskId) stored in the db.

A page that uses the control, sets the controls TaskId property and then
calls the controls Load_Data method (during the page_load event).

then upon completing the page the user clicks a button which fires an
event handler, this calls the controls Save_Data method.

The problem seems to stem from the fact that when the control is added
to the page the list boxes contain one "State" and then as a result of
the user adjusting the contents of the lists (via Java script) when the
server gets the post back the Lists boxes "State" has changed and as a
consequence I get the error "A potentially dangerous Request.Query.. ...."
Regards
MC

Steven Cheng[MSFT] wrote:
Hello MC,

Thanks for your reply.

As for the EventValidation problem, here is my understanding:

First, the "EventValidatio n" feature in ASP.NET 2.0 will perform the
following check when a page is postback(if EnableEventVali dation is
enabled):

1. It will ensure that any data in the Request.Form collection(the data
post through html input elements....) are originated from existing web
controls on the page. For example, if you dynamically use server-side or
client-side code to output the a following html input element

"<input type='text' name='xxx'...../>",

then, when postback, since this input field is not originally in the page's
control collection, it will report Event Validation error.

2. It will ensure that a postback event (such as Button Click) is really
triggered by the corresponding html element (rendered by the server-side
control). That means, if we use our own script to call "__doPostba ck"
fundtion to simulate another button's click event, it will raise error at
server-side (if event validation is turn on).
for your scenario, are you calling the "__doPostba ck" function in your own
script to simulate another control's postback? If you're going to use own
script to trigger a button's postback event, you can consider use script to
reference the button's client element and call its "click()" method. e.g.

var btn = document.getEle mentbyId("btnSu bmit");
btn.click();

Please feel free to post here if you have any further questions.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead

=============== =============== =============== =====

Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.

=============== =============== =============== =====

This posting is provided "AS IS" with no warranties, and confers no rights.

Nov 23 '06 #6
Hello MC,

Thanks for your reply.

So you think the problem is due to the dynamically changed list
items(through client script) on the page. I've created a simple page which
contains two Listbox(<select runat="server" .../>) and use script to add
items into one of it and postback. However, it doesn't report any
validation error. Therefore, I'm wondering the problem should be specific
to how to modify the listbox and store the data to persist from client to
server. If the complete source is not available, is it convenient that you
create a simplified page which could reproduce the issue and send me this
test page?

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead

This posting is provided "AS IS" with no warranties, and confers no rights.

Nov 27 '06 #7
Hello MC,

Have you got any further progress on this issue? Or if the problem remains,
is it convenient that you send me a simplified page that can repro the
problem? Also, If you feel this an urgent issue and will need a thorough
resolution, I would suggest you contact CSS for further assistance.

http://support.microsoft.com/

Please feel free to followup if there is anything we can help.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead

This posting is provided "AS IS" with no warranties, and confers no rights.

Nov 30 '06 #8
mc
Sorry, a "higher" priority issue has kept me away from looking at this
one for the last week, I'll post some code here in the next couple of days.

Steven Cheng[MSFT] wrote:
Hello MC,

Have you got any further progress on this issue? Or if the problem remains,
is it convenient that you send me a simplified page that can repro the
problem? Also, If you feel this an urgent issue and will need a thorough
resolution, I would suggest you contact CSS for further assistance.

http://support.microsoft.com/

Please feel free to followup if there is anything we can help.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead

This posting is provided "AS IS" with no warranties, and confers no rights.
Dec 4 '06 #9
mc
I was finding the bug very difficult to reproduce in something small
enough to post until I added an extra bit of Javascript. The function in
question sets the item (which has just been moved) as the selected value
in the destination list. The function is as follows: -

function selectListBoxIt em(ListBox,Item Value) {
for (var i = 0; i < ListBox.options .length; i++) {
if (ListBox.option s[i].value == ItemValue) {
ListBox.options[i].selected = true;
}
else{
ListBox.options[i].selected = false;
}
}
}

If I run the code below, but comment out the call to the
"selectListBoxI tem" function all is fine!

Full Code:

<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dt d">

<script runat="server">
protected void Page_Load(objec t sender, EventArgs e)
{
btnJS.OnClientC lick = "moveCurrentSel ectedItemFromTo ('" +
ListBox1.Client ID + "','" + ListBox2.Client ID + "'); return false;";
}
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>Untitl ed Page</title>
</head>

<script language="javas cript">
function moveCurrentSele ctedItemFromTo( FromListBoxName ,ToListBoxName) {
FromListBox = document.getEle mentById(FromLi stBoxName);
ToListBox = document.getEle mentById(ToList BoxName);
if(FromListBox. selectedIndex != -1){
var selectedValue = FromListBox.opt ions[FromListBox.sel ectedIndex].value;
var selectedText = FromListBox.opt ions[FromListBox.sel ectedIndex].text;
addOption(ToLis tBox,selectedTe xt,selectedValu e);
deleteOption(Fr omListBox,selec tedValue);
selectListBoxIt em(ToListBox,se lectedValue);
}
return false;
}

function addOption(selec tObject,optionT ext,optionValue ) {
var optionObject = new Option(optionTe xt,optionValue) ;
var optionRank = selectObject.op tions.length;
selectObject.op tions[optionRank]=optionObject;
}

function deleteOption(se lectObject,opti onValue) {
var rowToDelete = -1;
for (var i = 0; i < selectObject.op tions.length; i++) {
if (selectObject.o ptions[i].value == optionValue) {
rowToDelete = i;
}
}
selectObject.op tions[rowToDelete] = null;
}

function selectListBoxIt em(ListBox,Item Value) {
for (var i = 0; i < ListBox.options .length; i++) {
if (ListBox.option s[i].value == ItemValue) {
ListBox.options[i].selected = true;
}
else{
ListBox.options[i].selected = false;
}
}
}
</script>

<body>
<form id="form1" runat="server">
<div>
<asp:ListBox ID="ListBox1" runat="server">
<asp:ListItem Value="A" Text="A" />
<asp:ListItem Value="B" Text="B" />
<asp:ListItem Value="C" Text="C" />
</asp:ListBox>
<asp:Button ID="btnJS" runat="server" Text="->" />
<asp:ListBox ID="ListBox2" runat="server"> </asp:ListBox>

<asp:Button ID="btnPost" runat="server" Text="Post" />
</div>
</form>
</body>
</html>

mc wrote:
Sorry, a "higher" priority issue has kept me away from looking at this
one for the last week, I'll post some code here in the next couple of days.

Steven Cheng[MSFT] wrote:
>Hello MC,

Have you got any further progress on this issue? Or if the problem
remains, is it convenient that you send me a simplified page that can
repro the problem? Also, If you feel this an urgent issue and will
need a thorough resolution, I would suggest you contact CSS for
further assistance.

http://support.microsoft.com/

Please feel free to followup if there is anything we can help.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead

This posting is provided "AS IS" with no warranties, and confers no
rights.
Dec 4 '06 #10

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

Similar topics

2
4350
by: RAJ | last post by:
In our multi-tier application, we have several ASP.NET user controls which will update the same data source provided by middle tier logic. In this particular scenario we have one user control displaying the contents of the data source, whilst another control updates the datasource via a command buttons implementation of 'Click', an event raised in the 'Handle Postback Events' stage of the control execution life cycle (via the...
5
2609
by: Psych971 | last post by:
Hi, I'm wondering how I can generate a postback using javascript on a page that does not have any controls with the auto-postback property set to true. I know I can just use the submit() function but this triggers page validation which I do not want. What I'm trying to do is dynamically write in a hyperlink that when clicked will remove an item from the form, so I don't want any validation to occur because the form isn't being submitted at...
4
4221
by: AIM48 | last post by:
Hi. We have a framework that we work with for our project. So far we have had very good success – basically the frame work wraps many day to day tasks so that they are all included in the project. The framework is built on a modified Page Controller pattern in which a aspx page controls the loading of clients (user controls) into a placeholder (Sort of similar to .net 2.0 Master Pages except that the page loads the client not the client...
3
2576
by: Tim::.. | last post by:
Can someone please tell my why I get the following problem when I type the following piece of code! How do I get around this??? The idea is that when a user clicks a button on a form it causes a postback to occur which in turn triggers a sub in a user control... Error: Argument not specified for parameter e of Public Sub UploadDate(Sender As
7
3360
by: Tony Girgenti | last post by:
Hello. I'm trying to undetrstand ASP.NET 2.0 and javascript. When i have a button and i click on it and i see the web broswer progress bar at the bottom do something, does that mean that there is postback occurring? Does that mean a round trip to the server occurred? I keep reading javascript articles and tutorials that say "improve the client-side experience to be more responsive and quicker", but the articles
2
3924
by: John Kotuby | last post by:
Hi guys, I am converting a rather complicated database driven Web application from classic ASP to ASP.NET 2.0 using VB 2005 as the programming language. The original ASP application works quite well, so at times it is tempting just to port parts of it over mostly as-is. In fact, one MSDN article I read suggested using straight HTML wherever possible to make the app more efficient and less resource demanding. On one page there are 2...
2
1853
by: Phil | last post by:
Hi, There is a label and a textbox. There are two buttons: one HTML (buttonA), the other ASP.NET (ButtonB). Clicking on buttonA starts a javascript function, starting on ButtonB triggers nothing but of course generates a postback. Now my problem. When clicking on buttonA, the text of the textbox and of the label are modified by javascript, as expected.
3
10587
by: Lohboy | last post by:
Using ASP.NET and IE7. (Sorry if I am posting in the wrong forum but my problem seemed to be more related to the JavaScript side than the ASP.NET side.) I have two DropDownList controls the second of which resides within an UpdatePanel control which responds to a change in the first DropDownList. I also have a hidden button nested inside the UpdatePanel to force a postback via JS. The user uses these two DropDownList controls to create...
3
3314
by: =?Utf-8?B?SmFzb24gSGFydHNvZQ==?= | last post by:
I get the error: Sys.WebForms.PageRequestManagerParserErrorException: The message received from the server could not be parsed. Common causes for this error are when the response is modified by calls to Response.Write...when i postback to the page. The problem is, i need to write some dynamic javascript on postback and make sure it's rendered to the page. I'm setting up Omniture analytics for my site, <script language="JavaScript"...
0
8969
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8788
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9476
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9335
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9263
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9208
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6053
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4825
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2745
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.