作者: | 来源:互联网 | 2023-09-11 11:13
txtBox = new TextBox();
txtBox.Attributes["onfocus"] = "this.blur();";
HtmlImage image = new HtmlImage();
image.Attributes["style"] = "cursor: hand;";
image.Attributes["id"] = "img2";
image.Attributes["onclick"] = "WdatePicker({el:$dp.$('" +
txtBox.ClientID + "')})";
image.Attributes["src"] = "images/calendar.jpg";
这个获取的id是错误的,请教如何获得?这是内容页,使用了MasterPage.
28 个解决方案
简单,方法N种!!
你可以在生成后看页面的源代码,你可以看到控件的ID
你也可以在后台代码写空间时给他添加属性,ID,自己设置!!
谢谢。
txtBox.ClientID 获得的id是
txtBoxCustReqDate。但是实际产生的是,
ctl00_ContentPlaceHolder1_txtBoxCustReqDate,如何在程序中获得呢?
请大家帮忙。
我程序中添加的id就是
txtBoxCustReqDate,可关键是,我要添加js的click方法,里面要用到ClientID,要是直接写死成
ctl00_ContentPlaceHolder1_txtBoxCustReqDate,感觉不妥,呵呵
程序中有
txtBox.ID=txtBoxCustReqDate,请大家关注!
估计你是因为没有添加就获取ClientID了。
必须要先添加完再获取ClientID。
不好意思,让大家以为没有添加txtBox.ID,其实是有的,不然我如何获得id为txtBoxCustReqDate呢?
把程序重新贴出,
txtBox = new TextBox();
txtBox.ID = txtPrefix + dataRow["keyword"].ToString();
txtBox.Attributes["style"] = "width:200px;";
txtBox.Attributes["onfocus"] = "this.blur();";
HtmlImage image = new HtmlImage();
image.Attributes["style"] = "cursor: hand;";
image.Attributes["id"] = "img2";
image.Attributes["onclick"] = "WdatePicker({el:$dp.$('" + txtBox.ClientID + "')})";
image.Attributes["src"] = "images/calendar.jpg";
txtBox 添加到父控件后
再取 txtBox.ClientID 试试
没看到添加到父控件的代码
大家的回答没有什么用,算了,结贴,暂且把名字写死,如果有问题,那就写个js的函数吧。
添加控件的顺序问题
http://topic.csdn.net/u/20100721/10/eca1f48f-6b1f-41cf-a760-4128bd4fae09.html
var month = document.getElementById("<%=ddlMonth.ClientID %>");
这是楼主想要的吗?还有就是要注意顺序,比如说你必须保证在后台是先添加了控件的,然后在前台js中读取和访问
你可以把js访问aspx页面的最后,或者说在后台写js代码添加完控件后再Page.ClientScript.Regis......
试下,不知行不