Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length

 
Advanced search

103,706 Posts in 18,327 Topics- by 8,526 Members - Latest Member: CocoChanels

July 04, 2009, 01:53:12 AM
CrystalTech ForumsProgramming/Design.NET Forum1st time using javascript - arrrr
Pages: [1]   Go Down
Print
Author Topic: 1st time using javascript - arrrr  (Read 1244 times)
laurag
Newbie
*

Karma: +0/-0
Offline Offline

Posts: 61

OS:
Windows XP
Browser:
Microsoft Internet Explorer 7.0


« on: November 19, 2007, 09:13:41 AM »

Hello again,

I'm just at work and i've had all the help possible from colleages, googlin and books and no one seems to be able to help me. Basically i have a form made in VS 2005, ASP.NET2.0 wit vb. This form has 2 sections on the saem page, personal address and work address. All i have been trying to do is copy and paste the address from the personal to the company if a checkbox is checked using javascript.

Now i admit i am no javascript guru, but, i have managed to get this working using simple html, therefore proving my method works but when i associate it with .net textboxes it just seems to totally ignore everything.

Below is the test html code i made to prove this method works, it defo works in a simple html page with simple html controls:

<SCRIPT LANGUAGE="JavaScript">
// <!--
function shipsame(form)
{
if(form.copycheck.checked)
{
     form.outtxt.value = form.intxt.value; 
}
     else
{
          form.outtxt.value = " ";
}
}
//-->
</script>
</head>
<body>
  <form name="blah">
    <input id="copycheck" name="copycheck" type="checkbox" value="checkbox"
       onClick="shipsame(this.form);">
    Copy addresses<br />
    <br />
    Copy from
    <input id="intxt" type="text" /><br />
    <br />
    Paste to &nbsp; &nbsp;
    <input name="outtxt" type="text" />
</form>
</body>
</html>

That defo works but my code is a lot more complex as it is not simple html but asp.net2.

I have tried calling the txtboxes via their given id's - did not work
I have tried looking at the source code (notepad) and getting ct100$ blah blah id - didn't work
I have tried getting the 'name' from the source code - that didn't work.
 This is what i have tried:

Remember, there is one checkbox(html) and 2 <asp:textbox>'s within a formview


function shipsame(form)
{
if(form.sameaddresscheck.checked)
{
//var publicaddress = frmIPR.ctl00$cpText$suppliergv$supplier_public_address1TextBox.Value;
//var contactaddress = frmIPR.ctl00$cpText$suppliergv$supplier_contact_address1TextBox.Value;

//var publicaddress = document.getElementById("ctl00$cpText$suppliergv$supplier_public_address1TextBox").innerText
//var contactaddress = document.frmIPR.getElementById("ctl00$cpText$suppliergv$supplier_contact_address1TextBox").innerText

//frmIPR.ctl00$cpText$suppliergv$supplier_public_address1TextBox.Value;
//var contactaddress = document.getElementById("supplier_contact_address1TextBox").Value;



    //form.supplier_contact_address1TextBox.Value = form.supplier_public_address1TextBox.Value;     
form.ctl00$cpText$suppliergv$supplier_public_address1TextBox.Value = form.ctl00$cpText$suppliergv$supplier_contact_address1TextBox.Value;

}
else
{
    form.ctl00$cpText$suppliergv$supplier_public_address1TextBox.Value = "";
}
}
-->
</script>


It does seem to recognise that the checkboc has been checked,the .net code is as follows for each control:

 <asp:TextBox ID="supplier_contact_address1TextBox" name="supplier_contact_address1TextBox" runat="server" Text='<%# Bind("supplier_contact_address1") %>' CssClass="formviewtxtboxes" MaxLength="50" Width="200px"></asp:TextBox></td>

 <asp:TextBox ID="supplier_public_address1TextBox" name="supplier_public_address1TextBox" runat="server" Text='<%# Bind("supplier_public_address1") %>' CssClass="formviewtxtboxes" MaxLength="50" Width="200px"></asp:TextBox></td>

&nbsp; <input id="sameaddresscheck" type="checkbox"value="checkbox" onClick="shipsame(this.form);" title="Address same as above" lang="uk"> <b>Same address as above</b>

Please help me as i have a deadline and i've neva done any javascript before today - arrrr,lol.

I will have a look in the moring for replies and will be looking forward to reading peoples suggestions and opinions.

Regards
xx
 Cheesy
Logged
MattK [CT]
Jr. Member
**

Karma: +4/-0
Offline Offline

Posts: 228


OS:
Windows Vista
Browser:
Firefox 2.0.0.9


WWW
« Reply #1 on: November 19, 2007, 09:28:32 AM »

Now, this is very lightly tested, but it seems to work.


In the on load event of the page you can just add an attribute to the check box that calls a JavaScript method which takes in the "intext", "outtext" and check box controls.

Something like this:
Code:
protected void Page_Load(object sender, EventArgs e)
{
   chkTest.Attributes.Add("onClick", string.Format("changeText({0},{1},{2});", chkTest.ClientID, txtIn.ClientID, txtOut.ClientID));
}

Then in your aspx page use this JavaScript function:
Code:
    <script language="javascript" type="text/javascript">
        function changeText(copycheck, inTxt, outTxt)
        {
            if(copycheck.checked)
            {
                outTxt.value = inTxt.value;
            }
            else
            {
                outTxt.value = "";
            }
        }
    </script>
</head>

It looks to me like the "ClientID" property piece was the one you were missing.
Logged

Matt Kellogg
Sr. Developer
CrystalTech Web Hosting
laurag
Newbie
*

Karma: +0/-0
Offline Offline

Posts: 61

OS:
Windows XP
Browser:
Microsoft Internet Explorer 7.0


« Reply #2 on: November 20, 2007, 01:39:13 AM »

Arrr, still not working. It says the :

copycheck.Attributes.Add("onClick", String.Format("changeText({0},{1},{2});", copycheck.ClientID, txtIn.ClientID, txtOut.ClientID))

gets an error basically saying that the object reference is null?Huh?
I trued declaring the checkbox and textboxes but didn't seem to make a difference

Any ideas?
Logged
MorningZ
Hero Member
*****

Karma: +118/-20
Offline Offline

Posts: 5,235


OS:
Windows XP
Browser:
Firefox 2.0.0.9


WWW
« Reply #3 on: November 20, 2007, 03:58:55 AM »

First off....  maybe you shouldn't have promised a javascript solution to someone when you admit, to us anyways and we are totally the wrong people you should have done that to, have zero clue what you are doing...

Second, in your first bit of straight up HTML in your first post, you have the click event on the first textbox, that makes no common sense

And lastly, in the post right above, you are using the id of "copycheck", but in your code same above you have "sameaddresscheck", that could be the cause of the error....   and to look for the source of "object reference null", turn on tracing, where it will give you a nice tree of the document like this:



the Control Tree section is what will tell you exactly what your code has access to
Logged

"When I get fired, i want people to say: Wow! did that guy get canned!!!!" - George Castanza
ak732
Happily Verbose
*****

Karma: +211/-39
Offline Offline

Posts: 1,290

OS:
Windows XP
Browser:
Firefox 2.0.0.9


WWW
« Reply #4 on: November 20, 2007, 04:26:41 AM »

Laura,

You might consider using an Ajax Update Panel and just handle the copy server-side instead.  Then you can write the copy logic in VB or C# code-behind, whatever you're more comfortable with.

If you're dead set on hand writing the Javascript, and if you are willing to use Firefox for testing, grab the Firebug add-in for Firefox and use it.  I think you'll find it very helpful.  I believe there's something similar for IE although I don't know the name of it.

Andy
Logged

Andy
Pages: [1]   Go Up
Print
Jump to: