I have an account management page that will let users change information about themselves. I want to have a checkbox checked when they click on a textbox to start editing it, so that they know, and the program knows what will be updated. I think I can do this with a postback, but I would like to avoid the extra postback if possible.
I figure that this could be done with javascript... but I dont know how.
--Edit--
@Muhammad Akhtar
I made a blank page and tried this here is the code:
<%@ Page Title="" Language="C#" MasterPageFile="~/BlueBlack.Master" AutoEventWireup="true" CodeBehind="WebForm2.aspx.cs" Inherits="LinkFactory.WebForm2" %>
<asp:Content ID="Content1" ContentPlaceHolderID="Head" runat="server">
<script type="text/javascript">
function update()
{
if(document.getElementById('<%=check.ClientID %>').value != '')
{
document.getElementById('<%=check.ClientID %>').checked = true;
}
else
{
document.getElementById('<%=check.ClientID %>').checked = false;
}
}
</script>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<asp:CheckBox ID="check" runat="server" />
<asp:TextBox ID="TextBox1" runat="server" onkeypress="update();"></asp:TextBox>
</asp:Content>
-- Edit 2 --
About the answer:
I used his script in the header of my page. For some reason I need to type 2 characters for it to work, but I think that's alright
I have an account management page that will let users change information about themselves. I want to have a checkbox checked when they click on a textbox to start editing it, so that they know, and the program knows what will be updated. I think I can do this with a postback, but I would like to avoid the extra postback if possible.
I figure that this could be done with javascript... but I dont know how.
--Edit--
@Muhammad Akhtar
I made a blank page and tried this here is the code:
<%@ Page Title="" Language="C#" MasterPageFile="~/BlueBlack.Master" AutoEventWireup="true" CodeBehind="WebForm2.aspx.cs" Inherits="LinkFactory.WebForm2" %>
<asp:Content ID="Content1" ContentPlaceHolderID="Head" runat="server">
<script type="text/javascript">
function update()
{
if(document.getElementById('<%=check.ClientID %>').value != '')
{
document.getElementById('<%=check.ClientID %>').checked = true;
}
else
{
document.getElementById('<%=check.ClientID %>').checked = false;
}
}
</script>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<asp:CheckBox ID="check" runat="server" />
<asp:TextBox ID="TextBox1" runat="server" onkeypress="update();"></asp:TextBox>
</asp:Content>
-- Edit 2 --
About the answer:
I used his script in the header of my page. For some reason I need to type 2 characters for it to work, but I think that's alright
You can use onkeypress
event and call JS function
<asp:TextBox ID="TextBox1" runat="server" onkeypress="update();"></asp:TextBox>
function update()
{
if(document.getElementById('<%=TextBoxID.ClientID %>').value != '')
{
document.getElementById('<%=CheckBoxID.ClientID %>').checked = true;
}
else
{
document.getElementById('<%=CheckBoxID.ClientID %>').checked = false;
}
}
You can do this in Javascript without having to add inline events to your markup, which makes it look better and is easier to maintain:
document.getElementById('<%=TextBoxID.ClientID %>').focus = function() {
document.getElementByID('<%=CheckBoxID.ClientID %>').checked = true;
}
This assumes that you want the event to fire when focus is put on the textbox.