简体   繁体   English

在 javascript 中引用 ASP.NET web 控件

[英]Referencing the ASP.NET web control in javascript

I have created a drop down list in my user control, see source code below,我在我的用户控件中创建了一个下拉列表,请参阅下面的源代码,

<asp:DropDownList ID="ddlInd" runat="server" DataSourceID="indXmlDS" DataTextField="text" DataValueField="text"></asp:DropDownList>

In Page_load I have done this:在 Page_load 我已经这样做了:

protected void Page_Load(object sender, EventArgs e)
    {
       ClientScriptManager cs = Page.ClientScript;
       cs.RegisterStartupScript(this.GetType(), "myScript", "<script language='javascript' src='../Scripts/myJS.js'></script>");
        ddlInd.Attributes["onchange"] = "showTextbox()";
    }

So what code should I be using, if I would like to refer to this control in my external javascript file, myJS.js?那么,如果我想在我的外部 javascript 文件 myJS.js 中引用此控件,我应该使用什么代码?

I have tried to use document.getElementById("<%=ddlInd.ClientID %>") but it would return NULL .我曾尝试使用document.getElementById("<%=ddlInd.ClientID %>")但它会返回NULL

Can anyone help?任何人都可以帮忙吗? Thanks谢谢

EDIT:编辑:

Not sure if attaching that myJS.js file would be helpful here不确定在此处附加该 myJS.js 文件是否有帮助

function showTextbox() {
    var sid = <%=ddlInd.ClientID %>;
    //alert(sid);
    var s = document.getElementById('<%=ddlInd.ClientID %>'); // <-- problem here 

    alert(s);
    if (s.options[s.selectedIndex].value == "Other") {
        myDiv.style.display = "inline";
        alert("display");
    }
    else {
        myDiv.style.display = "none";
        alert("none");
    }
}

EDIT2:编辑2:

I kinda found the workaround which is to embed scripts in the user control page instead of using external script file.我找到了一种解决方法,即在用户控制页面中嵌入脚本而不是使用外部脚本文件。 Thanks for the suggestions everyone.谢谢大家的建议。 They were all very helpful.他们都非常有帮助。

Also the modified js script is as follows, and it works:修改后的js脚本如下,并且可以正常工作:

function showTextbox(objID) {
    var s = document.getElementById(objID);
    var div = document.getElementById("myDiv");
    if (s.options[s.selectedIndex].value == "Other") {
         div.style.display = "inline";
     }
     else {
         div.style.display = "none";
     }
}

Try using ClientScriptManager.RegisterClientScriptInclude instead of RegisterStartupScript.尝试使用ClientScriptManager.RegisterClientScriptInclude而不是 RegisterStartupScript。

It seems in your case you're including an external javascript file, rather than javascript code that should directly appear in the page.在您的情况下,您似乎包含了一个外部 javascript 文件,而不是应该直接出现在页面中的 javascript 代码。

Also try changing the double quotes " to single quotes ' in document.getElementById('<%=ddlInd.ClientID %>')还可以尝试将document.getElementById('<%=ddlInd.ClientID %>')中的双引号 " 更改为单引号 '

EDIT oh, I see that you're trying to do <%=ddlIndustry.ClientID %> inside your javascript file.编辑哦,我看到您正在尝试在 javascript 文件中执行 <%=ddlIndustry.ClientID %> 。 That is not going to be interpreted in an external js file!这不会在外部 js 文件中解释! You should either include your function directly in teh page (not as an external file) or try to pass the ColntrolId as an argument to the js function您应该将 function 直接包含在页面中(而不是作为外部文件),或者尝试将 ColntrolId 作为参数传递给 js function

Try to set the ClientIdMode of the control to Predictable:尝试将控件的 ClientIdMode 设置为 Predictable:

<asp:DropDownList ID="ddlInd" ClientIDMode="Predictable" runat="server" DataSourceID="indXmlDS" DataTextField="text" DataValueField="text"></asp:DropDownList>

You can also use a static ClientId by using ClientIDMode="Static" in which case the ClientId will be equal to the id of the control, so you can say:您还可以通过使用ClientIDMode="Static"来使用 static ClientId,在这种情况下,ClientId 将等于控件的 id,因此您可以说:

document.getElementById("ddlInd");

you could use this to reference your control to.js file您可以使用它来引用您的控件 to.js 文件

$('select#ddlInd').val(....) $('select#ddlInd').val(....)

if the dropdownlist is inside a "ContentPlaceholder", you could do this如果下拉列表位于“ContentPlaceholder”内,您可以这样做

$('select#ContentPlaceholderID_ddlInd').val(...) $('select#ContentPlaceholderID_ddlInd').val(...)

place it in your.js file将它放在 your.js 文件中

Note: include of course your.js file注意:当然包括 your.js 文件

If you can't use .NET 4.0 and are stuck on 3.5/2.0, I wrote a small library called Awesome.ClientID to solve this problem.如果你不能使用 .NET 4.0 并且卡在 3.5/2.0 上,我写了一个名为 Awesome.ClientID 的小库来解决这个问题。

I've posted it before in this answer: How do I make this getElementsbyName work for IE (and FF)?我之前在这个答案中发布过: 如何让这个 getElementsbyName 为 IE(和 FF)工作?

Basically it will allow you to serialize all your controls to a JSON array, and you can change your JavaScript to:基本上,它将允许您将所有控件序列化为 JSON 数组,并且您可以将 JavaScript 更改为:

document.getElementById(controls.ddlInd);

The library can be found here: http://awesomeclientid.codeplex.com/该库可以在这里找到: http://awesomeclientid.codeplex.com/

Blog post about it: http://www.philliphaydon.com/2010/12/i-love-clean-client-ids-especially-with-net-2-0/关于它的博客文章: http://www.philliphaydon.com/2010/12/i-love-clean-client-ids-special-with-net-2-0/

You can use <%=Control.ClientID %> When you have script on the.aspx page if your using an External file then u have to use the control id <%=ddlInd.ClientID %> will not work.您可以使用 <%=Control.ClientID %> 当您在.aspx 页面上有脚本时,如果您使用外部文件,那么您必须使用控件 ID <%=ddlInd.ClientID %> 将不起作用。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM