开发者

Checkbox control in Asp.Net

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?


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>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜