简体   繁体   English

asp.net控件值无法传递给javascript函数

[英]asp.net controls value not pass to javascript function

My Question is : asp.net controls value not pass to javascript function 我的问题是:asp.net控件的值不能传递给javascript函数

Asp.Net Code ASP.NET代码

<asp:TextBox ID="txtsubcategorycodeid" class="form-control " runat="server" placeholder="Sub Category Code ID"></asp:TextBox>
<asp:TextBox ID="txtsubcategoryname" class="form-control " runat="server" Height="25px" placeholder="Sub CategoryName In Letter"></asp:TextBox>
<asp:DropDownList ID="ddcategorycodeid" runat="server">
<asp:ListItem>---Select---</asp:ListItem>
</asp:DropDownList>
<asp:DropDownList ID="ddstatus" runat="server">
<asp:ListItem>---Select---</asp:ListItem>
<asp:ListItem>Active</asp:ListItem>
<asp:ListItem>In-Active</asp:ListItem>
</asp:DropDownList>
<asp:Button ID="btnSave3" runat="server" Text="Button" OnClientClick="return btnSave3_onclick()" />

JavaScript Code JavaScript代码

var txtsubcategorycodeid = $("#txtsubcategorycodeid").val();
var txtsubcategoryname = $("#txtsubcategoryname").val();
var ddcategorycodeid = $("#ddcategorycodeid").val();
var ddstatus = $("#ddstatus").val();

When I call it it display Undefined 当我称它为未定义时

This Code Work in Simple Form, But not work with integration on template and I put this code to integrated Template. 此代码以简单形式工作,但不适用于模板上的集成,因此我将此代码放入了集成的Template中。 It will Not Work. 不起作用。

    This Asp.Net Code in content placeholder. 
    If I use input html control then its works.

If you are using version ASP.NET 4 or greater then you can add ClientIDMode attribute to your controls which will not change your ids like this:- 如果您使用的是ASP.NET 4或更高版本,则可以将ClientIDMode属性添加到控件中,这不会像下面这样更改您的ID:

<asp:TextBox ID="txtsubcategorycodeid" class="form-control " runat="server" 
     placeholder="Sub Category Code ID" ClientIDMode="Static" ></asp:TextBox>

In this case you will get the correct result if you execute this:- 在这种情况下,如果执行以下操作,您将获得正确的结果:

var txtsubcategorycodeid = $("#txtsubcategorycodeid").val();

If you have a lower version of ASP.NET (<4) the you can use the ClientID property to fetch the actual ID being rendered like this:- 如果您使用较低版本的ASP.NET(<4),则可以使用ClientID属性来获取要呈现的实际ID,如下所示:

var txtsubcategorycodeid = $("#<%= txtsubcategorycodeid.ClientID %>").val();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM