开发者

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

开发者 https://www.devze.com 2023-02-12 23:15 出处:网络
ASP.Net web forms, .NET 2.0 I have a dilemma.I have an aspx page that has a custom control: <def:CustomControl ID=\"customID\" runat=\"server\" />

ASP.Net web forms, .NET 2.0

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

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

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>

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

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.

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.

How can I either:

A) Find 开发者_StackOverflowa way to add the event if the dropdownlist control exists on the page?

OR

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?

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?



EDIT:

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" />

And on the code behind:

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

That should work.


Assuming that you can't put this logic in the control itself, what you need to do I think is access that repeater and attach an ItemDataBound event to it. Something like this:

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

Then in the ItemDataBound event you would do something like this:

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

I don't have time to try that out right this minute, but I suspect that should work.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号