页面加载后如何向ASP.NET控件添加事件? [英] How do I add a event to an ASP.NET control when the page loads?

查看:550
本文介绍了页面加载后如何向ASP.NET控件添加事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ASP.Net Web表单,.NET 2.0

ASP.Net web forms, .NET 2.0

我有两难选择.我有一个带有自定义控件的aspx页面:

I have a dilemma. I have an aspx page that has a custom control:

<def:CustomControl ID="customID" runat="server" />

如果满足某些条件,则可以将自定义控件多次添加到ASPX页面:

The custom control can be added multiple times to the ASPX page if certain criteria is met:

<asp:Repeater runat="server" ID="options" OnItemDataBound="options_OnItemDataBound">
    <HeaderTemplate>
        <table border="0" cellpadding="0" cellspacing="0" width="100%">
            <tr>
    </HeaderTemplate>
    <ItemTemplate>
                <td>
                    <span>
                        <asp:Label runat="server" ID="optionName">
                        </asp:Label>
                        <asp:DropDownList runat="server" ID="optionValues" CssClass="PartOption" >
                        </asp:DropDownList>
                    </span>
                </td>
    </ItemTemplate>
    <FooterTemplate>
            </tr>
        </table>
    </FooterTemplate>
</asp:Repeater>

在aspx页面后面的代码中,我想将事件(OnSelectedItemChange)附加到下拉菜单控件中,而该控件实际上不在ASPX页面上(尚未).但是,当我尝试在后面的代码中执行以下操作时:

In the code behind of the aspx page, I wanted to append a event (OnSelectedItemChange) to the dropdown control that isn't actually on the ASPX page (yet). However, when I try to do something like the following in the code behind:

foreach(Control eachControl in customID.Controls) {
    if (eachControl.HasControls) {
        Control DdControl = eachControl.FindControl("optionValues");  //always null

        if (DdControl != null && DdControl is typeOf(DropDownList)) {
            DdControl = DdControl as DropDownlist;  //I might have the syntax wrong
                                                    //here, but you get the point.

             //add event to control.
        }
    }
}

但是eachControl所拥有的只是一个转发器,它的子控件计数显示为0.因此,在使用FindControl方法时,它始终返回null.

But All that eachControl has is a repeater, and it shows its childcontrol count as 0. So when it get to the FindControl method, it always returns null.

问题因此,我想做的是将一个事件(OnSelectedItemChange)添加到转发器内的dropdownlist控件中(这是一个自定义控件,如果满足条件,该控件将被添加到页面中).在下拉列表中进行选择时,我希望它触发Event方法.

QUESTION So what I am trying to do is add an event (OnSelectedItemChange) to the dropdownlist control inside the repeater (which is a custom control that gets added to the page if criteria is met). When a selection is made in the drop down, I want it to trigger the Event method.

我该怎么办

A)如果页面上存在dropdownlist控件,找到一种添加事件的方法吗?

A) Find a way to add the event if the dropdownlist control exists on the page?

OR

B)一种能够在后面的代码中调用方法并传递所有下拉列表中的选择的方法(因为自定义控件可以多次创建多个下拉列表而被添加?

B) A way to be able to call a method in the code behind and pass the selections from all the drop down lists (since the custom control could be added more than once creating multiple drop downs?

注意我前面没有代码,所以如果我在语法上有误,请忽略.我也知道ID是唯一的,因此我的逻辑也被认为是一个问题.呈现这些内容时,ID可能会被ASP名称约定所破坏,我认为我必须找到解决方法.

NOTE I don't have the code in front of me, so if I have something syntactically incorrect, please overlook. I also know that ID's are unique, so this is also assumed to be a problem with my logic. When it renders these, the ID's might be mangled by the asp name convention which I would assume I have to find a way around.

推荐答案

如果在自定义控件上创建该事件,则无需检查该事件是否在页面上存在.如果确实如此,它将一定会触发该事件.还是我在这里想念东西?

If you create that event on the custom control, you won't need to check if it exists on the page or not. If it does, it will definately trigger the event. Or am I missing something here?



您需要做的是在控件上声明一个公共事件,然后当SelectedIndexChanged事件被触发时,您将触发该事件并在页面上接收它.

What you'll have to do is declare a public event on your control, then when the SelectedIndexChanged Event fired, you'd fire that event, and receive it on the page.

因此,在您的控制下,您将拥有:

So on your control you'd have:

public delegate void MyIndexChangedDelegate(string value);
public event MyIndexChangedDelegate MyEvent;

protected void myDropDown_SelectedIndexChanged(object sender, EventArgs e)
{
    MyEvent(myDropDown.SelectedValue); // Or whatever you want to work with.
}

然后在页面上的控件声明中,您将拥有:

Then on your page, on your control declaration you'd have:

<usc:control1 runat="server" OnMyEvent="EventReceiver" />

以及后面的代码:

protected void EventReceiver(string value)
{
    // Do what you have to do with the selected value, which is our local variable 'value'
    ClientScript.RegisterStartupScript(typeof(Page), "Alert", string.Format("<script language='JavaScript'>alert('User selected value {0} on my DropDown!');</script>"), value);
}

应该可以.

这篇关于页面加载后如何向ASP.NET控件添加事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆