javascript - Dynamically check asp net checkbox without postback - Stack Overflow

admin2025-04-20  0

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

Share Improve this question edited Aug 22, 2011 at 16:06 Adam Schiavone asked Aug 20, 2011 at 16:43 Adam SchiavoneAdam Schiavone 2,4624 gold badges34 silver badges67 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 1

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.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1745085214a284098.html

最新回复(0)