Solved

Javascript to set a hidden field value on change to an item in a Repeater

Posted on 2006-11-06
13
851 Views
Last Modified: 2008-02-01
Hi Experts,
     Could you please give me an example of how to use Javascript to set a hidden field value on change to an item in a Repeater
0
Comment
Question by:JamesJMcDonnell
  • 7
  • 5
13 Comments
 
LVL 13

Assisted Solution

by:jeebukarthikeyan
jeebukarthikeyan earned 250 total points
ID: 17881617
hi,
in this sample when u click on the server side check box it will fire script

private void grdAttachments_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
  CheckBox chk;
  if(e.Item.ItemIndex>=0)
  {
     chk=(CheckBox)e.Item.Cells[0].FindControl("CheckBox1");
    chk.Attributes.Add("Onclick","javascript:myfunction('" + e.Item.Cells[0].Text  + "')
    // in this i am passing a value in the zeroth cell to the javascript function

b u d d h a
0
 
LVL 10

Expert Comment

by:jnhorst
ID: 17886880
Is your hidden field repeated in your repeater such that you have one hidden field for each item repeated?

John
0
 

Author Comment

by:JamesJMcDonnell
ID: 17887822
Hi John, No there is just the one hidden field on the page
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 10

Expert Comment

by:jnhorst
ID: 17890469
So each time you make a change in an item rendered by the repeater, you want to save something to this hidden field?

John
0
 

Author Comment

by:JamesJMcDonnell
ID: 17893817
No John, Just if there is at least one change.
On the page there are can be one to many items that have been added to a shopping cart. Each item has a quantity associated with it. The quantity is originaally defaulted and the customer can change it. There is a requirement to make the user click a button to update the cart if they make any changes to a quantity and another requirement to disable a 'continue' link until they have updated the cart if they have changed a quantity. So I thought to use a hidden field as an indicator and if it changes to 'on' to then disable the link until the update cart button is clicked. I now think it would be better to directly disable the link if the cart needs updating and dispense with the hidden field if this is as easy as changing a value in a hidden field.
0
 
LVL 10

Accepted Solution

by:
jnhorst earned 250 total points
ID: 17894963
Here's a thought.  In your aspx page, in the <head> section have a script block and set a variable like this:

<script language="javascript">
// note that this is not in a function.  it will scope to the page.
var cartChanged = false;
</script>

Your repeater should have something like an OnItemDataBounnd event that first for each item repeated.  You will have a text box being repeated by the repeater for the item qty.  Let's call this textbox "txtQty".  In the OnItemDataBound event, do this:

// "e" should be available from the event args.  resolve the text box.
TextBox tb = (textBox)e.Item.FindControl("txtQty");

// since cartChanged was declared in the <head> section outside of a funtion, you should be able to change it like this.
tb.Attributes.Add("onchange", "cartChanged = true;");

Then in the button you click to continue you can code up a client-side function to check that variable, and if it is true, prompt the user to first update the cart.

John

0
 
LVL 10

Expert Comment

by:jnhorst
ID: 17894966
Corrections:

"that first for each item..."  should have been "that fires for each item..."

John
0
 

Author Comment

by:JamesJMcDonnell
ID: 17900005
Thanks John - The page is a Master Page (in Asp.net 2.0)  and I don't know how to get a head section into it at the moment.
0
 
LVL 10

Expert Comment

by:jnhorst
ID: 17902868
Just do it in the HTML of the page.  The <head></head> section goes before the <body></body> section.

John
0
 

Author Comment

by:JamesJMcDonnell
ID: 17913193
Thanks John, At present the page has aa <asp:Hyperlink> element rather than a button to continue. Is it possible to use this (simply disabling the link rather than putting out a message) rather than converting to a button and if so what would the code look like. Also when the user clicks on a button to run the update basket function what would the code look like to reset the variable?
0
 
LVL 10

Expert Comment

by:jnhorst
ID: 17916575
Since your hyperlink is a server control you would access it in JavaScript by way of its ID attribute.  Current versions of IE and Firefox (I believe) support the function getElementById(), but older Firefox and Netscape do not, so what I will show you here may not work the same in all browsers.

Take this from my origin post:

// since cartChanged was declared in the <head> section outside of a funtion, you should be able to change it like this.
tb.Attributes.Add("onchange", "cartChanged = true;");

and add a statement to disable the link...

tb.Attributes.Add("onchange", "cartChanged = true; document.getElementById('linkid').disabled = true;");

This should disable the link when a change is made to the qty in the textbox.  Using this method, you may not even need the cartChanged variable.  Assuming the continue button is a server control button you would not need to worry about resetting anything because when the contrinue button is clicked and the page posts back, everything returns to its default.  The variable will return to false and the hyperlink will return to enabled.

John
0
 

Author Comment

by:JamesJMcDonnell
ID: 17928817
Hi John,

In header of master page I have:

<script type="text/javascript" language="javascript">
        // note that this is not in a function.  it will scope to the page.
        var cartChanged = false;
        </script>
In code behind for content page I have:

Dim tb As TextBox = e.Item.FindControl("txtDelegates")
tb.Attributes.Add("onchange", "cartChanged = true;")
tb.Attributes.Add("onchange", "cartChanged = true; document.getElementById('lnkCheckout').disabled = true;")

However, this not not disable the link. Not sure why using two attributes.add but with just 2nd also get no disabling.
0
 
LVL 10

Expert Comment

by:jnhorst
ID: 17935100
I will need to do some checking on this later this evening.... just don't want you to think I have forgotten...  I will post later.

John
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question