简体   繁体   English

注册OnClientItemSelected为动态AutoCompleteExtender

[英]Register OnClientItemSelected for a Dynamic AutoCompleteExtender

I have created a UserControl that contains the AjaxControlToolkit's AutoCompleteExtender. 我创建了一个包含AjaxControlToolkit的AutoCompleteExtender的UserControl。 It works well when I have one or more statically designed instances on the same page. 当我在同一页面上有一个或多个静态设计的实例时,它会很好地工作。 However, if one of these controls is added dynamically (say, within an UpdatePanel), the <script> block (which I currently have embedded in the Source view) for that one control does not seem to be available (JavaScript complains that it can't find the function I am wiring up to OnClientItemSelected). 但是,如果动态添加这些控件之一(例如,在UpdatePanel中),则该控件的<script>块(我目前已将其嵌入到Source视图中)似乎不可用(JavaScript抱怨它可以找不到我要连接到OnClientItemSelected的函数)。

I suspect I need to register the scripts for the UserControl in its PreRender event, so they are available even when it is added to the page dynamically. 我怀疑我需要在UserControl的PreRender事件中注册脚本,因此即使将脚本动态添加到页面中也可以使用它们。 I'm using the ToolkitScriptManager, and imagine I need to call ScriptManager.RegisterScriptControl(this); 我正在使用ToolkitScriptManager,并想象我需要调用ScriptManager.RegisterScriptControl(this); ;。 from my control, and then implement the IScriptControl interface. 从我的控件中,然后实现IScriptControl接口。

Is this the right approach? 这是正确的方法吗? If so, to correctly implement IScriptControl, do I have to move the script block out of Source view and into its own .js file? 如果是这样,为了正确实现IScriptControl,是否需要将脚本块从“源”视图移到其自己的.js文件中? That would force me to redesign my JS function, because it calls inline code to retrieve the ClientID for a subcontrol within that specific instance: 那将迫使我重新设计我的JS函数,因为它调用内联代码来检索该特定实例内子控件的ClientID:

var hiddenField = $get('<%=this.hfItemID.ClientID%>');

Script Management and JS aren't my specialty, so I want to be sure that's the right direction before I complicate a UserControl that was working great until I started adding them programatically. 脚本管理和JS不是我的专长,因此我想确保这是正确的方向,然后再使能正常工作的UserControl复杂化,直到我开始以编程方式添加它们为止。

Update 更新资料

I've moved the javascript into code behind, and am registering it as a ClientScript, in the hopes it will get me a little closer to where I need to be: 我将javascript移到了后面的代码中,并将其注册为ClientScript,希望它可以使我更接近需要的位置:

string searchScript = "function " + this.aceItemSearch.ClientID + "_getByID(sender, e) { var hfield = $get('" + this.hfItemID.ClientID + "'); if (typeof hfield === 'undefined') return; var item = eval('(' + e._value + ')'); hfield.value = item.ID; if (typeof document != 'undefined') { var control = $get('" + this.btnSearch.ClientID + "'); } control.click(); }";    
Page.ClientScript.RegisterStartupScript(this.GetType(), aceItemSearch.ClientID + "_getByID", searchScript, true);

This works for the statically placed control, but as before, not for the dynamically added one. 这适用于静态放置的控件,但与以前一样,不适用于动态添加的控件。 The script just doesn't make it into the Source View. 该脚本只是没有进入源代码视图。

Moving the script to the code behind works when the static , 5-argument version of RegisterStartupScript is called. 当调用RegisterStartupScript的5参数静态版本时,将脚本移至后面的代码即可。 Provide the UserControl itself as the first parameter: 提供UserControl本身作为第一个参数:

string searchScript = "function " + this.aceItemSearch.ClientID + "_getByID(sender, e) { var hfield = $get('" + this.hfItemID.ClientID + "'); if (typeof hfield === 'undefined') return; var item = eval('(' + e._value + ')'); hfield.value = item.ID; if (typeof document != 'undefined') { var control = $get('" + this.btnSearch.ClientID + "'); } control.click(); }";    
ScriptManager.RegisterStartupScript(this, this.GetType(), aceItemSearch.ClientID + "_getItemByID", searchScript, true);

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

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