Solved

Java / Javascript methods not working with a DetailsView control

Posted on 2008-06-24
6
690 Views
Last Modified: 2008-06-25
I am new to using Javascript methods in conjunction with VB code.  I do understand how most of it works and having been a C programmer in the past, I can easily read Java as well.  Bloated disclaimers aside, I need help understanding how methods and created, come into being, appear from the abyss in Javascript that are associated with an ASP control.


After you look at the snippets below, then read on here:

This ASPX page formerly used a Repeater control.  This method used to be specific to it:  dtvReceiptsEntry_getGridById(gridname) -

This is what it looked like before I changed it:
rptrReceiptsEntry_getGridById(gridname) and the reference before the underscored is the exact name of the ASP Repeater control that was used.  I assumed that I could just retrofit it by changing the name to use the detailsview control.

I am convinced that it is a method because I have searched our code database (MS VSS) for any reference to anything named *_getGridById through out the solution and the solution's history, and there is no function named anything close to that.  So, I am left to conclude that at one time this was a javascript method associated with the repeater control.

Anyone with greater knowledge of Java is encouraged to participate.  I think that would include most of the planet!

Thanks in advance.





I have this code in my VB code-behind in the page_load even:
 
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    Response.Cache.SetExpires(DateTime.Now.AddTicks(500))
    Try
        Try
            io_misc_func = New misc_func
        Catch ex As Exception
            io_misc_func = Nothing
        End Try
        ddl_schty.Attributes.Add("onchange", "javascript:schdule_onchange(ddl_schty);")
.
.
.
End Sub
 
-------------------------------------
 
This  is the drop down list referenced in the ASPX page:
 
	<asp:Table ID="tblselectTypes" runat="Server" HorizontalAlign="center" Width="45%">
		<asp:TableRow runat="server">
			<asp:TableCell HorizontalAlign="Left" BackColor="Silver" runat="server">Fuel Code:</asp:TableCell>
			<asp:TableCell HorizontalAlign="Left" BackColor="Silver" runat="server">Fuel Type:</asp:TableCell>
			<asp:TableCell HorizontalAlign="Left" BackColor="Silver" runat="server">Schedule Type:</asp:TableCell>
		</asp:TableRow>
		<asp:TableRow runat="server">
			<asp:TableCell width="25%" HorizontalAlign="Left" runat="server">
				<asp:dropdownlist id="ddl_fuelcd" Width="100%" runat="server" AutoPostBack="True"></asp:dropdownlist>
			</asp:TableCell>
			<asp:TableCell width="25%" HorizontalAlign="Left" runat="server">
				<asp:dropdownlist id="ddl_fuelty" Width="100%" runat="server" AutoPostBack="True"></asp:dropdownlist>
			</asp:TableCell>
			<asp:TableCell width="50%" HorizontalAlign="Left" runat="server">
				<asp:dropdownlist id="ddl_schty" Width="100%" runat="server" AutoPostBack="True"></asp:dropdownlist>
			</asp:TableCell>
		</asp:TableRow>
	</asp:Table>
 
-----------------------------------------------
And finally, here is a piece of the Javascript called on the OnChange event of the button:
 
		<script type="text/javascript" language="JavaScript">
			var gridname = '';
			var k = 0;
			function schdule_onchange(ddl)
			{	 
				var opt = ddl.options[ddl.selectedIndex].value;
				var msg = "";
				
 
				if (opt == "01") 
				{
					msg = "Note: You are using the gallons tax paid schedule type.  The Nevada " 
					msg += "Revised Statutes specifically prohibit a licensed supplier from selling " 					
					msg += "tax paid to another licensed supplier.  Do you wish to continue?"
					
					var rValue = confirm(msg);
 
					if(rValue)
					{
						//If clicked "OK", perform the following....
						for (var i=0; i<10; i++)
						{
					        msg = "Got Here...value of i is: "
					        msg += i
					        alert(msg);
 
							var grid = dtvReceiptsEntry_getGridById(gridname);
							msg = "Value in gridname" 
							msg += gridname
							alert (msg);
							alert(["Value in gridname " + gridname]);
							
							var row = grid.Rows.getRow(i);
							//Set the Origin State to NV and disable the field.
							row.getCell(4).setValue("NV");
							row.getCell(4).Column.AllowUpdate = 2;
							//Set the Destination State to NV and disable the field.
							row.getCell(6).setValue("NV");
							row.getCell(6).Column.AllowUpdate = 2;

Open in new window

0
Comment
Question by:NorthReno
[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
  • 3
  • 3
6 Comments
 
LVL 33

Expert Comment

by:raterus
ID: 21861724
It's "similar" to the javascript document.getElementById(id) function

perhaps you can simply make the switch to using something like this (note asp.net will rename your ids, so take note how I'm getting it from the server.  Also, this javascript will need to be directly in the aspx page, if you link it somewhere else, it won't work (without changes).

var grid = document.getElementById('<%= someServerControl.ClientID %>');
0
 

Author Comment

by:NorthReno
ID: 21866661
Hello raterus:

I have tried a few versions of what you suggested.  This is the latest and it at least loads an object into the variable:

var ctrl1 = document.getElementById('<%= dtvReceiptsEntry.Rows(1).FindControl("ddlOrigState").ClientID %>');

I tried this and it wouldn't even compile:

var ctrl1 = document.getElementById('<%= ddlOrigState.ClientID %>');

It told me that the object ddlOrigState is not declared.

Any other suggestions?
0
 

Author Comment

by:NorthReno
ID: 21866885
My last post was incomplete.  Here is some more of the code.  It is actually failing on the setValue line and the error message is "Object does not support this property or method".  So,as you can see, the control is a dropdown list, it may have something to do with the SelectedValue or something.

How can I get a list of valid properties and methods for these objects?

var ctrl1 = document.getElementById('<%= dtvReceiptsEntry.Rows(1).FindControl("ddlOrigState").ClientID %>');
                                    
alert(["Value in ctrl1 " + ctrl1]);
                                    
//Set the Origin State to NV and disable the field.
ctrl1.setValue("NV");
0
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 
LVL 33

Expert Comment

by:raterus
ID: 21868616
there is no setValue method in Javascript that I know of.

The only way I know to select the value is to iterate through the options, find the value you are interested in selecting, and select it.  Sorry I don't have code for this off the top of my head, I usually just select the items on the server.

If you wanted to see all the properties, I suggest getting FireFox and the FireBug extension.  You can "Inspect" the current page, click on the dropdownlist, and view all the actual properties of the current selection, it's very handy for web development/javascript!
0
 
LVL 33

Accepted Solution

by:
raterus earned 500 total points
ID: 21868645
I think this code will work to select the item, if not something close to it should!

for(i=0;i<ctrl1.options.length;i++)
{
  var itm = ctrl1.options[i];
  if (itm.value == "somevalue")
  {
    itm.selected=true;
  } else {
    itm.selected=false;
  }
}
0
 

Author Comment

by:NorthReno
ID: 21869394
Well raterus, that worked very well.  I included the completed function below.  The drop down list is a complete listing of the two-letter codes for the states in the US.  It iterates through them and when it finds the right state (in this case Nevada), it sets it as selected and when it exits the function, the DetailsView magically displays the proper states!

I upgraded my Firefox to version 3 and installed Firebug.  I look forward to having some pseudo-intellisense while I learn Javascript.

Thanks so much!
						var ctrl1 = document.getElementById('<%= dtvReceiptsEntry.Rows(1).FindControl("ddlOrigState").ClientID %>');
						alert(["Value in ctrl1 " + ctrl1]);
						//Set the Origin State to NV and disable the field.
						for(i=0;i<ctrl1.options.length;i++)
                        {
                          var itm1 = ctrl1.options[i];
						  alert(["Value in itm1 " + itm1.value]);
                          if (itm1.value == "NV")
                          {
                            itm1.selected=true;
                            break;
                          
                          }
                        }
						
						var ctrl2 = document.getElementById('<%= dtvReceiptsEntry.Rows(1).FindControl("ddlDestState").ClientID %>');
						//Set the Destination State to NV and disable the field.
						for(i=0;i<ctrl1.options.length;i++)
                        {
                          var itm2 = ctrl2.options[i];
						  alert(["Value in itm2 " + itm2.value]);
                          if (itm2.value == "NV")
                          {
                            itm2.selected=true;
                            break;
                          
                          }
                        }					
							
					}

Open in new window

0

Featured Post

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses
Course of the Month5 days, 6 hours left to enroll

635 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