捕获中继器内下拉列表的 SelectedIndexChanged

发布于 2024-12-02 23:32:35 字数 3725 浏览 3 评论 0原文

我试图捕获 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 。

I am trying to catch the SelectedIndexChanged of a DropDownList inside a Repeater. I have searched the internet but could not find a specific answer any help would be great. This is my code.

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

In the Page_Load:

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

Other events:

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

I need to catch the SelectedIndexChanged of the populated DropDownList for each one generated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

嗳卜坏 2024-12-09 23:32:35

这里发生的代码相当不匹配 - 在 ASP.NET 应用程序中使用 System.Windows.Forms 只是问题之一。您似乎在代码隐藏和标记中分配事件处理程序(这不一定是坏事,但您的做法似乎没有任何押韵或理由)。

您将 Repeater 的 ItemCommand 事件绑定到一种方法,该方法正在查找 ID 与标记中的 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);
}

手头可能还有更多问题 - 但这有望解决简化它足以让你取得一些进步。

You've got quite a mismatch of code going on here - using System.Windows.Forms in an ASP.NET application is just one of the issues. You appear to be assigning event handlers in the code-behind and in the markup (nothing necessarily bad about that, but there's seems to be no rhyme or reason to how you're doing it).

You're Repeater's ItemCommand event is bound to a method that is looking for a DropDownList that has a different ID than the one in your markup.

If you're using the System.Windows.Forms.MessageBox to debug (ala old school JavaScript and other language "debugging" methods), save yourself a world-class headache (not to mention a lot of unnecessary code cleanup when you're done with development) and step through your code in the debugger.

I'm not sure how the page will render, but I don't think you're using the HeaderTemplate and FooterTemplate quite the way they're intended.

All that said, try something like this:

Markup (ASPX page):

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

Code Behind (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);
}

There may be more issues at hand as well - but this will hopefully streamline it enough for you to make some progress.

猥︴琐丶欲为 2024-12-09 23:32:35
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)
            { 

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

            }
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文