简体   繁体   English

ASCX结果调用在ASPX中找到的javascript

[英]ASCX result to call javascript found in ASPX

I have a user control (MyListSelect.ascx) that just has a list of radio buttons... 我有一个用户控件(MyListSelect.ascx),它只有一个单选按钮列表...

<%@ Control Language="C#" %>
<select>
    <option disabled selected>Select Options</option>
    <option>
        option 1
    </option>
    <option>
        option 2
    </option>
</select>

My ASPX page has: 我的ASPX页面具有:

<%@Register Src="~/MylistSelect.ascx" 
            TagName="listSelect" 
            TagPrefix="ListControl" %> 

<ListControl:listSelect runat="server" />

This displays in my ASPX a dropdown menu (woo hoo); 这会在我的ASPX中显示一个下拉菜单(woo hoo);

I want to be able to (form the ASPX) call a javascript function based on the selected index change event for the dropdown since the menu item selected will have different functionality in different pages. 我希望能够(形成ASPX)基于所选下拉菜单的索引更改事件调用javascript函数,因为所选菜单项在不同页面中具有不同的功能。 I dont' want to define all possible functionality in the ASCX. 我不想在ASCX中定义所有可能的功能。

Any suggestions? 有什么建议么? lso, Why does <ListControl:listSelect runat="server" /> have to runat server? lso,为什么<ListControl:listSelect runat="server" />必须要运行服务器? is there a way around this too? 有没有办法解决这个问题?

It must be runat=server because user controls are special controls processed by the .net engine on server side. 它必须是runat=server因为用户控件是服务器端.net引擎处理的特殊控件。 An ASP.NET User Control doesn't make any sense on client side, it needs a "translation" before be sent to client. ASP.NET用户控件在客户端没有任何意义,在发送给客户端之前需要进行“翻译”。

In order to respond to selected index change event first you'll need to assign an id or a css class to the select so you can identify it more accurately trough a jQuery selector (assuming you want to use jQuery since you added it to question tags). 为了选定的指数变化响应事件首先你需要一个id或CSS类分配给select这样你就可以识别它更准确地槽jQuery选择(假设你想使用jQuery因为你添加它来质疑标签)。

Something like this should do the trick 这样的事情应该可以解决问题

HTML HTML

<select id="mySelect">
    <option disabled selected>Select Options</option>
    <option>
        option 1
    </option>
    <option>
        option 2
    </option>
</select>

JAVASCRIPT JAVASCRIPT

// Shorthand for $( document ).ready()
$(function() {
    $('#mySelect').change(function(){
         // your logic when index changed here
    });
});

First, change your select to this: 首先,将您的选择更改为此:

<select onchange="yourJSFunction();">

When you use the control inside the page you'll also provide the yourJSFunction : 当您在页面内使用控件时,还将提供yourJSFunction

<script type="text/javascript">
    function yourJSFunction() {
        alert("yourJSFunction 1");
    }
</script>

<ListControl:listSelect runat="server" />

In another page: 在另一页中:

<script type="text/javascript">
    function yourJSFunction() {
        alert("yourJSFunction 2");
    }
</script>

<ListControl:listSelect runat="server" />

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

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