0

I have an ASP.NET control with some text fields and a checkbox. I would like to modify the checkbox state using JavaScript based on the values present in the text fields after page load.

How can I reliably read the text field values in JavaScript? I saw this answer but it didn't help:

<asp:TextBox Text='<%# Bind("something") %>' ID="txtSomething" runat="server">
</asp:TextBox>

<script type="text/javascript">
    alert($('#<%= txtSomething.ClientID %>').val());
</script>

This doesn't work; VS tells me that "txtSomething is not declared. It may be inaccessible due to its protection level."

How else can I refer to that text box in JavaScript?

Community
  • 1
  • 1
Roman Starkov
  • 59,298
  • 38
  • 251
  • 324
  • Where is this `txtSomething` located? For example, inside GridView, Panel. Please post its parent control codes. – Win Apr 05 '13 at 14:21
  • @Win It was inside an ``. I've got this working now, using `frm.FindControl("txtSomething").ClientID`. – Roman Starkov Apr 05 '13 at 15:39

1 Answers1

4

You can try to get client ID of control which contains your text box and after search in his children input which ID contains your text box server ID

<script type="text/javascript">
    alert($('#<%= parentControl.ClientID %>').find("input[id$='txtSomething']").val());
</script>

As another option you can do:

<script type="text/javascript">
    alert($('#<%= (parentControl.FindControl("txtSomething")).ClientID %>').val());
</script>
Vladimirs
  • 8,232
  • 4
  • 43
  • 79