繁体   English   中英

页面加载后如何向ASP.NET控件添加事件?

[英]How do I add a event to an ASP.NET control when the page loads?

ASP.Net Web表单,.NET 2.0

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

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

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

<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页面上(尚未)。 但是,当我尝试在后面的代码中执行以下操作时:

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。

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

我怎么可以:

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

要么

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

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

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



编辑:

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

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

 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. } 

然后在页面上,在控件声明上,您将具有:

 <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); } 

那应该工作。

假设您不能在控件本身中放入此逻辑,我想您需要做的就是访问该转发器并将ItemDataBound事件附加到它。 像这样:

Repeater options = customID.FindControl("options") as Repeater;
if (options != null)
{
    options.ItemDataBound += new RepeaterItemEventHandler(OptionsItemDataBound);
}

然后,在ItemDataBound事件中,您将执行以下操作:

void OptionsItemDataBound(object sender, RepeaterItemEventArgs e)
{
     DropDownList optionValues = e.Item.FindControl("optionValues") as DropDownList;
     if (optionValues != null)
     {
         //perform logic you need here
     }
}

我没有时间在这一分钟尝试一下,但是我怀疑那应该可行。

暂无
暂无

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

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