简体   繁体   English

jQuery / Javascript:如何获取动态生成的控件的ClientID

[英]Jquery/Javascript: How can I get ClientID for dynamically generated controls

How can I get the client ID for dynamically created controls using javascript/jquery. 如何使用javascript / jquery获取动态创建的控件的客户端ID。

I have a datepicker and if value change it should trigger a textchanged event and do some functionality. 我有一个日期选择器,如果值更改,它应该触发textchanged事件并执行某些功能。 Below is my script 下面是我的脚本

function BindControlEvents() {
    $(".datepicker").datepicker({
        format: 'mm/dd/yyyy',
        autoclose: true,
        todayBtn: 'linked'
    }).on('changeDate', function (ev) {
        __doPostBack('MainContent_dteFr1002', '');
    }).on('clearDate', function (ev) {
        //to do
    });
}

//Initial bind
    $(document).ready(function () {
        BindControlEvents();
    });

//Re-bind for callbacks
    var prm = Sys.WebForms.PageRequestManager.getInstance();
    prm.add_endRequest(function () {
        BindControlEvents();
    });

Code-Behind for Dynamically generated textbox Datepicker 动态生成的文本框Datepicker的代码隐藏

protected void Page_Init(object sender, EventArgs e)
{
      LoadControls(reportDefId);
}

protected void LoadControls(int reportDefId)
{
  HtmlTableCell cellDate = new HtmlTableCell();
  TextBox dte = new TextBox();
  dte.ToolTip = "If you need to filter for blank dates, please select date range 1/1/1753-1/1/1753";
  dte.Attributes.Add("type", "text");
  dte.Attributes.Add("class", "datepicker");
  if (rfl.fieldDisplayName.Contains("Start") || rfl.fieldDisplayName.Contains("End"))
      dte.ID = "dte" + id.ToString();
  else
      dte.ID = "dteFr" + id.ToString();
  dte.Style["Width"] = "85px";
  dte.Style["Height"] = "24px";
  dte.TextChanged += new EventHandler(dte_TextChanged);
  cellDate.Controls.Add(dte);
}

For testing, I have hard coded of ClientID 'MainContent_dteFr1002' to see if it fires textchnaged event. 为了进行测试,我对ClientID'MainContent_dteFr1002'进行了硬编码,以查看其是否触发textchnaged事件。 It does. 是的 So I need to pass cliendid value dynamically which ever cliendID contains dte. 因此,我需要动态传递cliendID包含dte的cliendid值。

I have searched but I didn't find appropriate answer. 我已经搜索过,但是没有找到合适的答案。

Please help! 请帮忙! Thanks 谢谢

You can store the ClientID for each control for later use somewhere. 您可以为每个控件存储ClientID以供以后在某处使用。

List<string> dynamicID = new List<string>();
dynamicID.Add(dte.ClientID);

Or give your controls self made ID and set the ClientID Mode to static so they will not be renamed by aspnet into MainContent_ID_5 或者给您的控件一个自制的ID,并将ClientID模式设置为static,这样它们就不会被ASPNET重命名为MainContent_ID_5

dte.ID = "ID_" + index;
dte.ClientIDMode = ClientIDMode.Static;

Or navigate down the control path and find the correct ID, in this example the TextBox is added to PlaceHolder1 as the first control. 或在控件路径中导航并找到正确的ID,在此示例中,将TextBox作为第一个控件添加到PlaceHolder1。

<%= PlaceHolder1.Controls[0].ClientID %>

The on change function should automatically have the reference to the dynamic control that triggered it. 变更功能应自动引用触发它的动态控件。

alert(ev.target.id);

is one way, you should see the id. 是一种方式,您应该看到ID。

$(this).attr("id");

or 要么

this.id

should also contain the dynamic id's respectively. 还应分别包含动态ID。 If your definition of CliendID means something else besides the control that is triggered in the event then you should specify what it is and how it's uniquely tied to this on change event. 如果您对CliendID的定义除了在事件中触发的控件之外还意味着其他内容,则应指定它是什么以及它如何与更改事件唯一地关联。

lets assume that you have the button in a container. 假设您将按钮放在容器中。

<div class='ButtonContainer'>
<button value="textChanges" id="MainContent_dteFr1002" />
</div>
<input type="text" class='datepicker' value='2017-05-01' />

then you event could look like this 那么你的事件可能看起来像这样

$(".datepicker").datepicker({
    format: 'mm/dd/yyyy',
    autoclose: true,
    todayBtn: 'linked'
}).on('changeDate', function (ev) { 
   $(".ButtonContainer").find("button").click();
}).on('clearDate', function (ev) {
    //to do
});

 //OR text changed on the same controller

$(".datepicker").datepicker({
format: 'mm/dd/yyyy',
 autoclose: true,
 todayBtn: 'linked'
}).on('changeDate', function (ev) { 
// (this) is the changes textbox
var id = $(this).attr("id");
 __doPostBack(id, '');
}).on('clearDate', function (ev) {
                //to do
 });

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

相关问题 如何在jQuery中的txt文件中获取动态生成的html? - How can I get dynamically generated html in txt file in jQuery? 如何使用 Javascript/jQuery 在动态生成的 forms 中动态输入字段 select? - How can I dynamically select input field in dynamically generated forms with Javascript/jQuery? 如何获得对extJS生成的元素的JQuery / Javascript访问? - How can I get JQuery/Javascript access to elements generated by extJS? 如何使用JavaScript变量在JavaScript函数中获取ASP.NET ClientID? - How can I use a JavaScript variable to get an ASP.NET ClientID inside a JavaScript function? 如何使用JavaScript或jQuery将动态生成的表转换为DataTable? - How can I convert dynamically generated table into DataTable using JavaScript or jQuery? 如何使用JavaScript字符串动态创建&#39;&lt;%= ... ClientID%&gt;&#39;? - How to use JavaScript strings to create a '<%= …ClientID %>' dynamically? 使用jQuery和Javascript获取动态生成的ID - Get dynamically generated ids with jQuery and Javascript 如何获取用javascript生成的HTML? - How can I get the HTML generated with javascript? 如何删除 JavaScript 中动态生成的事件处理程序 - How can I remove dynamically generated event handlers in JavaScript 如何下载使用javascript动态生成的文件列表? - How can I download a list of files dynamically generated with javascript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM