Suppose i created a user control which contain two textbox, one button
--Start UserControl UserDetails--
<asp:TextBox runat="server" id="txtName"></asp:TextBox>
<asp:TextBox runat="server" id="txtAddress"></asp:TextBox>
--End UserControl UserDetails-
in aspx if i use the same user control,
<uc1:UserDetails runat="server" ID="UserDetails1" />
- how do i access the
txtName
using user control idUserDetails1
? - How do i make
$('#UserDetails1').val()
to return value oftxtName
?
Suppose i created a user control which contain two textbox, one button
--Start UserControl UserDetails--
<asp:TextBox runat="server" id="txtName"></asp:TextBox>
<asp:TextBox runat="server" id="txtAddress"></asp:TextBox>
--End UserControl UserDetails-
in aspx if i use the same user control,
<uc1:UserDetails runat="server" ID="UserDetails1" />
- how do i access the
txtName
using user control idUserDetails1
? - How do i make
$('#UserDetails1').val()
to return value oftxtName
?
-
I believe it is
$("#txtName").val()
but the rendered html displayed on the page would reveal the final input ID. – Steve0 Commented Dec 12, 2016 at 6:18 - $("#txtName").val() will work, that I'm well aware of. but with respect to user control id i should be able to access text box value. $('userControl').Val() should return first textbox value – Prasob Commented Dec 12, 2016 at 6:21
4 Answers
Reset to default 3Just add a public property for you TextBox
in you user control code behind
public TextBox Name
{
get { return txtName; }
set { txtName = value; }
}
Now you can access your TextBox
like this:
<uc1:UserDetails runat="server" ID="UserDetails1" />
<script>
$(function () {
$("#<%= UserDetails1.Name.ClientID%>").val("set you text here");
//or
var name = $("#<%= UserDetails1.Name.ClientID %>").val();
});
</script>
Try this, using Jquery
$("[id$='txtName']").val();
$("[id$='txtAddress']").val();
Use data keys inside your text boxes.
--Start UserControl UserDetails--
<asp:TextBox runat="server" id="txtName" data-key="name"></asp:TextBox>
<asp:TextBox runat="server" id="txtAddress" data-key="address"></asp:TextBox>
--End UserControl UserDetails-
<uc1:UserDetails runat="server" ID="UserDetails1" data-usercontrol = "UserDetails1" />
in jquery,
$("[data-usercontrol='UserDetails1']").find("[data-key='name']").val();
$("[data-usercontrol='UserDetails1']").find("[data-key='address']").val();
You can use id selector. But since you have used asp, it might be rendered with different id in HTML.
Hope this helps!
Copy the id of particular textbox from Developer Tools and then just use that id like below
var textLength = $("#ContentBody_ucMFA1_txtACode").val().length;
function Disable() {
var textLength = $("#ContentBody_ucMFA1_txtACode").val().length;
if (textLength > 0) {
if (Page_ClientValidate()) {
document.getElementById("divButtons").style.display = "none";
}
}
}