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

Suppose i created a user control which contain two textbox, one button--Start UserControl UserDetails--

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745371048a4624795.html

相关推荐

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

    Suppose i created a user control which contain two textbox, one button--Start UserControl UserDetails--

    4小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信