admin管理员组

文章数量:1323181

I have an asp 4.0 framework application that consists of a Masterpage and contentplaceholders. I have a .js file that does validations on the data entered by the user. How can I call a textbox "txtFirstName" (from the .js file) that is in a ContentPlaceHodler "cphBody" ?

Update (code):
.aspx

<asp:TextBox id="txtFirstName" runat="server" CssClass="webForm" Width="250px" MaxLength="100"></asp:TextBox>

<asp:Button id="btnContinue" runat="server" ClientIDMode="Static" onclientclick="document.getElementById('cphHeaderContent_AlertTimeMsgBox').value = GetSeconds(); return ValidateUser(1);" CssClass="webButton" Text="Continue" OnClick="btnContinue_Click" />

.js

function ValidateUser(valFormCount)
{
    var objTextBox;

    objTextBox = document.getElementById("txtFirstName"); 

I have tried document.getElementById("<%= txtFirstName.ClientID %>") but that did not work(a null value is passed). How can I acplish this?

I have an asp 4.0 framework application that consists of a Masterpage and contentplaceholders. I have a .js file that does validations on the data entered by the user. How can I call a textbox "txtFirstName" (from the .js file) that is in a ContentPlaceHodler "cphBody" ?

Update (code):
.aspx

<asp:TextBox id="txtFirstName" runat="server" CssClass="webForm" Width="250px" MaxLength="100"></asp:TextBox>

<asp:Button id="btnContinue" runat="server" ClientIDMode="Static" onclientclick="document.getElementById('cphHeaderContent_AlertTimeMsgBox').value = GetSeconds(); return ValidateUser(1);" CssClass="webButton" Text="Continue" OnClick="btnContinue_Click" />

.js

function ValidateUser(valFormCount)
{
    var objTextBox;

    objTextBox = document.getElementById("txtFirstName"); 

I have tried document.getElementById("<%= txtFirstName.ClientID %>") but that did not work(a null value is passed). How can I acplish this?

Share Improve this question edited Jun 21, 2012 at 18:46 DNR asked Jun 21, 2012 at 18:19 DNRDNR 3,74614 gold badges57 silver badges95 bronze badges 3
  • @ClaudioRedi Not everything has to be jQuery... – TheZ Commented Jun 21, 2012 at 18:21
  • @ClaudioRedi..... Nope... that would be too easy – DNR Commented Jun 21, 2012 at 18:22
  • 1 @TheZ: I didn't say he has to use it, just asked if he's using it now although I wasn't almost sure that not. – Claudio Redi Commented Jun 21, 2012 at 18:28
Add a ment  | 

1 Answer 1

Reset to default 6

This asp notation

document.getElementById("<%= txtFirstName.ClientID %>")

is not valid inside a js file

You could set ClientIDMode="Static" for the TextBox. If you do this, you'll be able to get the element by id with this

ASPX

<asp:TextBox ID="txtFirstName" runat="server" ClientIDMode="Static" />

JS

var textbox = document.getElementById("txtFirstName");

About ClientIDMode="Static"

This mode does exactly what you think it would, it makes the client side ID static. Meaning that what you put for the ID is what will be used for the client side ID. Warning, this means that if a static ClientIDMode is used in a repeating control the developer is responsible for ensuring client side ID uniqueness.

本文标签: aspnetUsing javascript to get the value of a textbox in Master Page ContentPlaceHoldersStack Overflow