Link to home
Start Free TrialLog in
Avatar of NorthReno
NorthRenoFlag for United States of America

asked on

Java / Javascript methods not working with a DetailsView control

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

Avatar of raterus
raterus
Flag of United States of America image

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 %>');
Avatar of NorthReno

ASKER

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?
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");
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!
ASKER CERTIFIED SOLUTION
Avatar of raterus
raterus
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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