views:

50

answers:

1

I'm trying to capture a person's current and permanent address.

If the current and permanent address are the same, it means there is no need to enter the same information twice. I'm using a checkbox to indicate that the addresses are the same.

If the checkbox gets checked, I would like to put the current address into the permanent address controls. How do I do that?

+4  A: 

You can achieve this functionality in numerous ways with both Server Side code (C#) or with Client Side code (JavaScript or jQuery). Without seeing what you currently have built, it is hard to tell you what the best fit is... Below is a sample of how to populate a text box with data from another with the Check Box is checked...

SERVER SIDE EXAMPLE

C#

protected void MyCheckBox_CheckedChanged(object sender, EventArgs e)
{
    this.MySecondTextBox.Text = this.MyFirstTextBox.Text;
}

ASP

<asp:TextBox ID="MyFirstTextBox" runat="server"></asp:TextBox>
<asp:CheckBox ID="MyCheckBox" runat="server" 
    oncheckedchanged="MyCheckBox_CheckedChanged" />
<asp:TextBox ID="MySecondTextBox" runat="server"></asp:TextBox>

CLIENT SIDE EXAMPLE

C#

protected void Page_Load(object sender, EventArgs e)
{
    this.MyCheckBox.Attributes.Add("onClick", "CopyText()");
}  

ASP

<asp:TextBox ID="MyFirstTextBox" runat="server"></asp:TextBox>
<asp:CheckBox ID="MyCheckBox" runat="server" />
<asp:TextBox ID="MySecondTextBox" runat="server"></asp:TextBox>
<script type="text/javascript" language="javascript">
    function CopyText() {
        var txt2 = document.getElementById("<%= this.MySecondTextBox.ClientID %>");
        txt2.value = document.getElementById("<%= this.MyFirstTextBox.ClientID %>").value;
    }
</script>
RSolberg
Good code. Just out of curiosity is there a reason why you put "this." all over the place - I have generally been of the view that is optional unless you need to clarify an ambiguity?
rtpHarry
Ya, but using ur sample code itself its not placed.
Sivakumar
@rtpHarry - when I do quick `garbage code samples` I tend to use `this` to make sure my variables were correct, etc. I wouldn't deploy this code to anything other than a `<pre>` and `<code>` block on a stack overflow post :)
RSolberg
@sivakumar: Both samples will do what you need to do. I tend not to provide a full coded solution for questions on StackOverflow as this isn't a `Give Me The Code` type of site. It is a Question and Answer platform and the above samples should be more than enough to get you started. If I was coding this, I'd probably have an address control of some kind and that control would have a "clone" or "copy" function to do just this..
RSolberg
Thanks a lot RSolberg... Nw its working..
Sivakumar