[英]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.