Reputation: 3746
I have an asp.net 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 accomplish this?
Upvotes: 1
Views: 9411
Reputation: 68440
This asp.net 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.
Upvotes: 6