如何从 jquery 文件访问母版页文本框?
在我的母版页中,我有一个文本框。
<asp:TextBox ID="SearchTextBox" runat="server" class="searchtxtbox" onfocus="HideSearchWaterMark();" Text="Search" onblur="ShowSearchWaterMark(this);" />
我在代码后面添加了 jquery 引用。
TextBox SearchTextBox = this.FindControl("SearchTextBox") as TextBox;
StringBuilder objStringBuilder = new StringBuilder();
objStringBuilder.Append("<script type=\"text/javascript\" language=\"javascript\">\n");
objStringBuilder.AppendFormat("var searchTextBox = '{0}';\n", SearchTextBox.ClientID);
objStringBuilder.Append("</script>\n");
this.Page.ClientScript.RegisterClientScriptBlock(GetType(), "RegisterVariables", objStringBuilder.ToString());
this.Page.ClientScript.RegisterClientScriptInclude(this.GetType(), "Global", this.ResolveClientUrl("~/Resources/Scripts/Search.js"));
this.Page.ClientScript.RegisterClientScriptInclude(this.GetType(), "Global", this.ResolveClientUrl("~/Resources/Scripts/jquery-1.4.2.js"));
this.Page.ClientScript.RegisterClientScriptInclude(this.GetType(), "Global", this.ResolveClientUrl("~/Resources/TagsScripts/jquery.autocomplete.js"));
在 Search.js 中,我有以下方法来访问文本框母版页:
$(document).ready(function () {
$("#" + searchTextBox).autocomplete("Handlers/GenericHandler.ashx?tgt=12", {
multiple: true,
multipleSeparator: ";",
mustMatch: false,
autoFill: true
});
});
function HideSearchWaterMark() {
var control = $("#" + searchTextBox);
if (control[0].className == "searchtxtbox ac_input")
control[0].value = "";
control[0].className = "searchtxtbox ac_input";
}
function ShowSearchWaterMark(tagsTextBox) {
if (searchTextBox.value.length == 0) {
searchTextBox.value = "Search";
searchTextBox.className = "searchtxtbox ac_input";
}
当我运行我的应用程序时,我收到对象引用未设置错误。
请告诉我哪里需要更改我的代码。
In my master page i've a textbox.
<asp:TextBox ID="SearchTextBox" runat="server" class="searchtxtbox" onfocus="HideSearchWaterMark();" Text="Search" onblur="ShowSearchWaterMark(this);" />
I added jquery references in code behind.
TextBox SearchTextBox = this.FindControl("SearchTextBox") as TextBox;
StringBuilder objStringBuilder = new StringBuilder();
objStringBuilder.Append("<script type=\"text/javascript\" language=\"javascript\">\n");
objStringBuilder.AppendFormat("var searchTextBox = '{0}';\n", SearchTextBox.ClientID);
objStringBuilder.Append("</script>\n");
this.Page.ClientScript.RegisterClientScriptBlock(GetType(), "RegisterVariables", objStringBuilder.ToString());
this.Page.ClientScript.RegisterClientScriptInclude(this.GetType(), "Global", this.ResolveClientUrl("~/Resources/Scripts/Search.js"));
this.Page.ClientScript.RegisterClientScriptInclude(this.GetType(), "Global", this.ResolveClientUrl("~/Resources/Scripts/jquery-1.4.2.js"));
this.Page.ClientScript.RegisterClientScriptInclude(this.GetType(), "Global", this.ResolveClientUrl("~/Resources/TagsScripts/jquery.autocomplete.js"));
in Search.js i've the following methods to access the text box of master page:
$(document).ready(function () {
$("#" + searchTextBox).autocomplete("Handlers/GenericHandler.ashx?tgt=12", {
multiple: true,
multipleSeparator: ";",
mustMatch: false,
autoFill: true
});
});
function HideSearchWaterMark() {
var control = $("#" + searchTextBox);
if (control[0].className == "searchtxtbox ac_input")
control[0].value = "";
control[0].className = "searchtxtbox ac_input";
}
function ShowSearchWaterMark(tagsTextBox) {
if (searchTextBox.value.length == 0) {
searchTextBox.value = "Search";
searchTextBox.className = "searchtxtbox ac_input";
}
When i run my application i'm getting object reference not set error.
Please tell me where i need to change my code.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
为了使用 id 选择器访问元素,在命名容器内您必须使用 ClientID,而在 js 文件中您将无法使用它。因此最好尝试使用类名来获取元素,例如
Inorder to access an element using id selector, inside a naming container you have to use ClientID and in a js file you won't be able to use that. So better try to get the element using the class name, something like
您需要在母版页上调用FindControl:
看看这里
You need to call FindControl on the Master page :
Have a look here
要使用 id 选择器访问元素,在命名容器内您必须使用 ClientID,而在 js 文件中您将无法使用它。因此,最好尝试使用类名来获取元素,例如
$("input:text.searchtxtbox")
链接
norder to access an element using id selector, inside a naming container you have to use ClientID and in a js file you won't be able to use that. So better try to get the element using the class name, something like
$("input:text.searchtxtbox")
link