如何从 jquery 文件访问母版页文本框?

发布于 2024-09-05 12:50:17 字数 2096 浏览 2 评论 0原文

在我的母版页中,我有一个文本框。

 <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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一个人的旅程 2024-09-12 12:50:17

为了使用 id 选择器访问元素,在命名容器内您必须使用 ClientID,而在 js 文件中您将无法使用它。因此最好尝试使用类名来获取元素,例如

$("input:text.searchtxtbox")

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

$("input:text.searchtxtbox")
无畏 2024-09-12 12:50:17

您需要在母版页上调用FindControl:

TextBox SearchTextBox = MasterPage.FindControl("SearchTextBox") as TextBox;

看看这里

You need to call FindControl on the Master page :

TextBox SearchTextBox = MasterPage.FindControl("SearchTextBox") as TextBox;

Have a look here

唔猫 2024-09-12 12:50:17

要使用 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文