最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - how to access usercontrol value in jsjquery - Stack Overflow

programmeradmin1浏览0评论

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" />
  1. how do i access the txtName using user control id UserDetails1?
  2. How do i make $('#UserDetails1').val() to return value of txtName?

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" />
  1. how do i access the txtName using user control id UserDetails1?
  2. How do i make $('#UserDetails1').val() to return value of txtName?
Share Improve this question edited Dec 12, 2016 at 6:17 Ataur Rahman Munna 3,9151 gold badge25 silver badges35 bronze badges asked Dec 12, 2016 at 6:12 PrasobPrasob 2,2232 gold badges13 silver badges21 bronze badges 2
  • 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
Add a ment  | 

4 Answers 4

Reset to default 3

Just 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";
                }
            }
        }

发布评论

评论列表(0)

  1. 暂无评论