Solved

Java / Javascript methods not working with a DetailsView control

Posted on 2008-06-24
6
659 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
  • 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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

743 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now