捕获转发器中下拉列表的SelectedIndexChanged

时间:2011-09-01 23:39:18

标签: c# asp.net drop-down-menu repeater

我试图在Repeater中捕获DropDownList的SelectedIndexChanged。我搜索过互联网,但找不到具体的答案,任何帮助都会很棒。这是我的代码。

page.aspx

<asp:Repeater id="CategoryMyC" OnItemCommand="SomeEvent_ItemCommand" runat="server">
    <HeaderTemplate>
        <table><tr>
    </HeaderTemplate>
    <ItemTemplate>
        <td>
            <table width="100%">
                <tr>
                    <th>Edit Carousel Item</th>
                </tr>
                <tr>
                    <td>Choose a product:</td>
                </tr>
                <tr>
                    <td>
                        <asp:DropDownList ID="ddlMcProducts" 
                                          DataTextField="Name"
                                          onselectedindexchanged="MyListIndexChanged"
                                          AutoPostBack="true"  
                                          DataSource='<%# ProductsManager.GetMerchantProductRepeater(Convert.ToInt32(Eval("MID"))) %>'  
                                          runat="server">
                        </asp:DropDownList>
                    </td>
                </tr>
            </table>                          
        </td>                
    </ItemTemplate>
    <FooterTemplate>
        </tr>
     </table>
     </FooterTemplate>
 </asp:Repeater>

page.aspx.cs

在Page_Load:

List<CarouselProducts> CP = CarouselProductsManager.GetCarouselItems(Convert.ToInt32(Session["Mid"]));
CategoryMyC.DataSource = CP;
CategoryMyC.ItemDataBound += new  RepeaterItemEventHandler(RepeaterItemDataBound);
CategoryMyC.DataBind();

其他活动:

protected void ddlMcProducts_SelectedIndexChanged(object sender, EventArgs e)
{
    DropDownList d = (DropDownList)sender;
    // Use d here

    System.Windows.Forms.MessageBox.Show("I am changing");
}

protected virtual void PageInit(object sender, EventArgs e)
{
    //get all the Carousel item of the merchant
    List<CarouselProducts> CP = CarouselProductsManager.GetCarouselItems(Convert.ToInt32(Session["Mid"]));
    //MerchantCategoryMyCarousel.DataSource = CP;
    //MerchantCategoryMyCarousel.DataBind();

    MerchantCategoryMyCarousel.DataSource = CP;
    MerchantCategoryMyCarousel.ItemDataBound += new RepeaterItemEventHandler(RepeaterItemDataBound);
    MerchantCategoryMyCarousel.DataBind();
}

protected virtual void RepeaterItemDataBound(object sender, RepeaterItemEventArgs e)
{
    DropDownList theDropDown = sender as DropDownList;
    if (e.Item.ItemType == ListItemType.EditItem)
    {
        DropDownList MyList = (DropDownList)e.Item.FindControl("ddlMcProducts");
        if (MyList == null)
        {
            System.Windows.Forms.MessageBox.Show("Did not find the controle");
        }
        else
            MyList.SelectedIndexChanged += new EventHandler(MyListIndexChanged);
    }
}

protected virtual void MyListIndexChanged(object sender, EventArgs e)
{
    System.Windows.Forms.MessageBox.Show("I am changing");
}

protected void SomeEvent_ItemCommand(object sender, RepeaterCommandEventArgs e)
{
    if (e.CommandSource.GetType() == typeof(DropDownList))
    {
        DropDownList ddlSomething = (DropDownList)e.Item.FindControl("ddlSomething");
        System.Windows.Forms.MessageBox.Show("I am changing");
        //Now you can access your list that fired the event
        //SomeStaticClass.Update(ddlSomething.SelectedIndex);
    }
}

我需要捕获每个生成的DropDownList的SelectedIndexChanged。

2 个答案:

答案 0 :(得分:3)

你在这里遇到的代码不匹配 - 在ASP.NET应用程序中使用System.Windows.Forms只是其中一个问题。你似乎在代码隐藏和标记中分配事件处理程序(没有什么不好的,但似乎没有押韵或理由你如何做)。

你是Repeater的ItemCommand事件绑定到一个方法,该方法正在寻找一个与你的标记中的ID不同的DropDownList。

如果您正在使用System.Windows.Forms.MessageBox进行调试(旧学校的JavaScript和其他语言“调试”方法),请为自己省去世界级的头痛(更不用说在进行大量不必要的代码清理时)你完成了开发)并在调试器中逐步执行代码。

我不确定页面是如何呈现的,但我认为你没有像他们想要的那样使用HeaderTemplate和FooterTemplate。

所有这一切,尝试这样的事情:

标记(ASPX页面)

<asp:Repeater id="CategoryMyC" OnItemCommand="CategoryMvC_ItemCommand"  OnItemDataBound="CategoryMvC_ItemDataBound" runat="server">
    <HeaderTemplate>
        <table>
            <tr>
                <th>Edit Carousel Item</th>
            </tr>
        </table>
    </HeaderTemplate>
    <ItemTemplate>
        <table width="100%">
            <tr>
                <td>Choose a product:</td>
            </tr>
            <tr>
                <td>
                    <asp:DropDownList ID="ddlMcProducts" 
                                      DataTextField="Name"
                                      OnSelectedIndexChanged="ddlMcProducts_SelectedIndexChanged"
                                      AutoPostBack="true"  
                                      DataSource='<%# ProductsManager.GetMerchantProductRepeater(Convert.ToInt32(Eval("MID"))) %>'  
                                      runat="server">
                    </asp:DropDownList>
                </td>
            </tr>
        </table>                                          
    </ItemTemplate>
</asp:Repeater>

代码背后(APSX.CS)

protected void Page_Load(object sender, EventArgs e)
{

    List<CarouselProducts> CP = CarouselProductsManager.GetCarouselItems(Convert.ToInt32(Session["Mid"]));
    CategoryMyC.DataSource = CP;
    //This can be assigned in the markup
    //CategoryMyC.ItemDataBound += new  RepeaterItemEventHandler(RepeaterItemDataBound);
    CategoryMyC.DataBind();
}

protected void ddlMcProducts_SelectedIndexChanged(object sender, EventArgs e)
{
    DropDownList d = (DropDownList)sender;
    // Use d here
}

protected void CategoryMyC_ItemDataBound(object sender, RepeaterItemEventArgs e)
{

    DropDownList theDropDown = sender as DropDownList;

    if (e.Item.ItemType == ListItemType.EditItem)
    {
        DropDownList MyList = (DropDownList)e.Item.FindControl("ddlMcProducts");

        // This section is not needed for what you are doing with it:
        // If the control is null, handle it as an error
        // There's no need to give it an event handler if it does exist, because
        // you already did so in the markup
        //if (MyList == null)
        //{
            //System.Windows.Forms.MessageBox.Show("Did not find the controle");
        //}
        //else
            //MyList.SelectedIndexChanged += new EventHandler(MyListIndexChanged);
        //}
    }
}

protected void CategoryMyC_ItemCommand(object sender, RepeaterCommandEventArgs e)
{

    if (e.CommandSource.GetType() == typeof(DropDownList))
    {
        // Note the correct control name is being passed to FindControl
        DropDownList ddlSomething = (DropDownList)e.Item.FindControl("ddlMcProducts");
        //System.Windows.Forms.MessageBox.Show("I am changing");
        //Now you can access your list that fired the event
        //SomeStaticClass.Update(ddlMcProducts.SelectedIndex);
}

手头上可能还有更多问题 - 但这有望简化它,以便您取得一些进展。

答案 1 :(得分:1)

protected void drpOrganization_SelectedIndexChanged(object sender, EventArgs e)
    {
        DropDownList ddl = (DropDownList)sender;
        RepeaterItem item = (RepeaterItem)ddl.NamingContainer;
        if (item != null)
        {
            CheckBoxList list = (CheckBoxList)item.FindControl("chkSite");
            if (list != null)
            { 

            }
        }
    }