admin管理员组文章数量:1323703
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?
- @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
1 Answer
Reset to default 6This 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.
版权声明:本文标题:asp.net - Using javascript to get the value of a textbox in Master Page ContentPlaceHolders - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742130642a2422142.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论