Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

javascript events when using NumericUpDown extender in gridview

Posted on 2008-10-23
3
Medium Priority
?
3,038 Views
Last Modified: 2009-07-29
I have a couple of javascript functions that fire on the onkeyup event for a textbox in a gridview.  Now I've added a NumericUpDown extender to the textbox and I want the same javascript functions to fire when the value in the textbox changes when using the NumericUpDown extender control but I don't know how to link the javascript functions to the extender control.

My code is shown below.
JAVASCRIPT CODE
 
function SumItUp(textResult)
			{
				var x = 0;
				var y = 0;
				
				var myArray = new Array();
				//add array items, this calls the public server variable we build in rowdatabound
				<%= JSArray %>
				//sum it:
				for (x = 0; x < myArray.length; x++)
				{
				    //multiply the value times one to trick js into always thinking the variable is a number
				    y = y + (myArray[x] * 1);
				}
				//display the result
				document.getElementById(textResult).value = (y * 1);
			}
 
         	
         		
			function Multiply(text1, text2, textResult)
			{
				var x = document.getElementById(text1).value;
				var y = document.getElementById(text2).value;
				if(IsNumeric(x) == true && IsNumeric(y) == true)
				{
				document.getElementById(textResult).value = (x * 1) * (y * 1);
				}
				else
				{
				document.getElementById(textResult).value = '';
				}
			}
			function IsNumeric(sText)
            {
               var ValidChars = "0123456789.-";
               var IsNumber=true;
               var Char;
               for (i = 0; i < sText.length && IsNumber == true; i++) 
                  { 
                  Char = sText.charAt(i); 
                  if (ValidChars.indexOf(Char) == -1) 
                     {
                     IsNumber = false;
                     }
                  }
               return IsNumber;
               }
 
ASPX CODE
<asp:TemplateField HeaderText="# to Buy">
                                             <ItemTemplate>
                                                 <asp:TextBox ID="txtNumberToBuy" runat="server" AutoPostBack="False" BorderColor="#759CAC"
                                                     BorderWidth="1px" CausesValidation="True" SkinID="TextBoxSkin" TabIndex="20"
                                                     Width="30px">1px</asp:TextBox>
                                                 <cc1:NumericUpDownExtender ID="txtNumberToBuy_NumericUpDownExtender" 
                                                     runat="server" Enabled="True" Maximum="1.7976931348623157E+308" 
                                                     Minimum="-1.7976931348623157E+308" RefValues="" ServiceDownMethod="" 
                                                     ServiceDownPath="" ServiceUpMethod="" Tag="" TargetButtonDownID="" 
                                                     TargetButtonUpID="" TargetControlID="txtNumberToBuy" Width="75">
                                                 </cc1:NumericUpDownExtender>
                                                 <asp:HiddenField ID="hfTotal" runat="server" Value="0.0" />
                                                 <asp:CompareValidator ID="CompareValidator1" runat="server" ControlToValidate="txtNumberToBuy"
                                                     Display="Dynamic" ErrorMessage="Integer Value Required" Operator="DataTypeCheck"
                                                     Type="Integer"></asp:CompareValidator>
                                             </ItemTemplate>
                                             <ItemStyle HorizontalAlign="Center" Width="100px" />
                                         </asp:TemplateField>
 
C# Code
 protected void gvMealsForSell_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        TextBox txtNumberToBuy = (TextBox)e.Row.FindControl("txtNumberToBuy");
        if (txtNumberToBuy != null)
            txtNumberToBuy.Text = "0";
 
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            // Get values for calculation
            TextBox txtQuantity = new TextBox();
            HiddenField hfMealPrice = new HiddenField();
            HiddenField hfTotal = new HiddenField();
                      
 
            txtQuantity = (TextBox)e.Row.FindControl("txtNumberToBuy"); // quantity
            hfMealPrice = (HiddenField)e.Row.FindControl("hfMealPrice"); // meal price
            hfTotal = (HiddenField)e.Row.FindControl("hfTotal"); //total
         
       
            // Add javascript to text box with calcualtion values
            txtQuantity.Attributes.Add("onkeyup", "javascript:Multiply('" + txtQuantity.ClientID + "','" + hfMealPrice.ClientID + "','" + hfTotal.ClientID + "');SumItUp('" + this.txtTotalDue.ClientID + "');");
                
            //Set up javascript array
            JSArray += "myArray[" + counter.ToString() + "] = document.getElementById('" + hfTotal.ClientID + "').value;" + Environment.NewLine;
  
            //counter to maintain the js array index
            counter += 1; 
 
        }
        
    }

Open in new window

0
Comment
Question by:weimha
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 10

Expert Comment

by:Bane83
ID: 22788422
Below is a sample of binding events to the buttons in a NumericUpDownExtender.
function pageLoad(sender, args) 
{
	var obj = $find('NumericUpDownExtender1');
	obj._bDown.onclick = down;
	obj._bUp.onclick = up;
}
 
function up()
{
	alert('up');
}
 
function down()
{
	alert('down');
}

Open in new window

0
 

Author Comment

by:weimha
ID: 22796593
But since the extender is in the gridview rows, I don't know what name it is rendered with.  How can I find it in the $find function?
0
 
LVL 10

Accepted Solution

by:
Bane83 earned 1500 total points
ID: 22796766
View the source of your rendered page.  It should be something like ctl00_parent_gvMealsForSell_rowIdentifier_txtNumberToBuy_NumericUpDownExtender
0

Featured Post

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

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…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Suggested Courses

688 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