我怎样才能得到一个UpdatePanel拦截的CompositeControl的DropDownList的 [英] How can I get an UpdatePanel to intercept a CompositeControl's DropDownList

查看:129
本文介绍了我怎样才能得到一个UpdatePanel拦截的CompositeControl的DropDownList的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含一个DropDownList一个CompositeControl的。

我已经将DropDownList的AutoPostBack属性设置为true。

在页面上,我有:

 < ASP:的UpdatePanel ID =的UpdatePanel=服务器>
    <&的ContentTemplate GT;
        < MyControl:控制ID =CustomControl=服务器/>
    < /&的ContentTemplate GT;
< / ASP:的UpdatePanel>

我也试着设置的 ChildrenAsTriggers =真正的的UpdateMode =永远但是没有解决问题。

的问题是,在UpdatePanel未拦截的CompositeControl的DropDownList的交回来。 (当DropDownList的改变正在执行一个完整的POST)

我怎样才能在UpdatePanel处理回发?

谢谢!

编辑 - 请求信息

国家与国家是在CompositeControl的两个DropDownLists。

  country.SelectedIndexChanged + =新的EventHandler(country_SelectedIndexChanged);保护无效country_SelectedIndexChanged(对象发件人,EventArgs的发送)
{
    states.DataSource = XXX;
    states.DataBind();
}


解决方案

好了,所以这可能不是最好的答案,但我认为你遇到它的问题,在UpdatePanel只是不能看到孩子控件的事件。好消息是,它很容易修复。假设你有一个控制(CatchMyEvent,它的方式是一个疯狂的聪明的名称),其上有一个DropDownList。现在,您想父页面看到,名单上的SelectedIndexChanged事件消防和更新标签相匹配的SelectedItem.Text。正因为如此,家长不能真正做到这一点。因此,让我们改变:

 公共部分类CatchMyEvent:System.Web.UI.UserControl
{
    公共委托无效ChangedIndex(对象发件人,EventArgs e)条;
    公共事件ChangedIndex的SelectedIndexChanged;    保护覆盖无效的OnInit(EventArgs的发送)
    {
        base.OnInit(E);
        dropDownListThrow.SelectedIndexChanged + =新的EventHandler(dropDownListThrow_SelectedIndexChanged);
        labelOutput.Text =无;
    }    公共无效dropDownListThrow_SelectedIndexChanged(对象发件人,EventArgs的发送)
    {
        labelOutput.Text =((DropDownList的)寄件人).SelectedItem.Text;
        如果(的SelectedIndexChanged!= NULL)
        {
            的SelectedIndexChanged(发件人,E);
        }
    }
}

基本上所有我做的是有控制捕获DropDownList的SelectedIndexChanged事件并发射它,这样任何父母网页或控制可以看到它。基本上所有我所做的就是把它传给了。现在父页面上,它真的很容易捕捉到。

您只需要与持有的触发器添加一个UpdatePanel:

 < ASP:AsyncPostBackTrigger控件ID =catchMyEventMain事件名称=的SelectedIndexChanged/>

...当然这增加的背后父页面code:

 保护覆盖无效的OnInit(EventArgs的发送)
{
    base.OnInit(E);
    catchMyEventMain.SelectedIndexChanged + = dropDownListThrow_SelectedIndexChanged;
}公共无效dropDownListThrow_SelectedIndexChanged(对象发件人,EventArgs的发送)
{
    labelSelectedValue.Text =((DropDownList的)寄件人).SelectedItem.Text;
}

其中标签是之前提到的标签。然后让魔术发生。

还有两个注意事项:

1)像我在测试时,这一点,忘记设置DropDownList的真AutoPostBack属性不要被一个白痴。

2)确保标签是在UpdatePanel中的的ContentTemplate。

希望这有助于。

I have a CompositeControl that contains a DropDownList.

I have set the AutoPostBack property of the DropDownList to true.

On the page, I have:

<asp:UpdatePanel ID="UpdatePanel" runat="server">
    <ContentTemplate>
        <MyControl:Control ID="CustomControl" runat="server" />
    </ContentTemplate>
</asp:UpdatePanel>

I've also tried setting ChildrenAsTriggers="true" and UpdateMode="Always," but neither resolved the problem.

The problem is that the UpdatePanel is not intercepting the CompositeControl's DropDownList's post back. (A full POST is being performed when the DropDownList is changed)

How can I get the UpdatePanel to handle the postback?

Thanks!

Edit -- Requested Info

Country and states are both DropDownLists in the CompositeControl.

country.SelectedIndexChanged += new EventHandler(country_SelectedIndexChanged);

protected void country_SelectedIndexChanged(Object sender, EventArgs e)
{
    states.DataSource = XXX;
    states.DataBind();
}

解决方案

Ok so this may not be the best answer, but I think the problem you're having it that the UpdatePanel just can't see the child control's event. Good news is, it's easy to fix. Say you have a control (CatchMyEvent, which by the way is a crazy clever name) and it has a DropDownList on it. Now you want the parent page to see the SelectedIndexChanged event fire on that list and update a label to match the SelectedItem.Text. As it is, the parent can't really do that. So let's change that:

public partial class CatchMyEvent : System.Web.UI.UserControl
{
    public delegate void ChangedIndex(object sender, EventArgs e);
    public event ChangedIndex SelectedIndexChanged;

    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
        dropDownListThrow.SelectedIndexChanged += new EventHandler(dropDownListThrow_SelectedIndexChanged);
        labelOutput.Text = "no";
    }

    public void dropDownListThrow_SelectedIndexChanged(object sender, EventArgs e)
    {
        labelOutput.Text = ((DropDownList)sender).SelectedItem.Text;
        if(SelectedIndexChanged != null)
        {
            SelectedIndexChanged(sender, e);
        }
    }
}

Basically all I did is have the control capture the DropDownList's SelectedIndexChanged event and fired it so that any parent page or control could see it. Essentially all I've done is passed it on. Now on the parent page, it's really easy to capture.

You just need to add an UpdatePanel with a trigger that holds:

<asp:AsyncPostBackTrigger ControlID="catchMyEventMain" EventName="SelectedIndexChanged" />

...and of course add this to the code behind for the parent page:

protected override void OnInit(EventArgs e)
{
    base.OnInit(e);
    catchMyEventMain.SelectedIndexChanged += dropDownListThrow_SelectedIndexChanged;
}

public void dropDownListThrow_SelectedIndexChanged(object sender, EventArgs e)
{        
    labelSelectedValue.Text = ((DropDownList)sender).SelectedItem.Text;
}

Where the label is the before mentioned label. And then let the magic happen.

Also two notes:

1) Don't be an idiot like I was when testing this and forget to set the AutoPostBack property on the DropDownList to true.

2) Make sure the label is in the UpdatePanel's ContentTemplate.

Hope this helps.

这篇关于我怎样才能得到一个UpdatePanel拦截的CompositeControl的DropDownList的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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