簡體   English   中英

在轉發器中捕獲下拉列表的SelectedIndexChanged

[英]catching SelectedIndexChanged of a dropdown list inside a 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。

您在這里進行的代碼完全不匹配-在ASP.NET應用程序中使用System.Windows.Forms只是問題之一。 您似乎在代碼的后面和標記中分配了事件處理程序(這不一定很糟糕,但是似乎沒有押韻或理由)。

您是Repeater的ItemCommand事件綁定到一種方法,該方法正在尋找一個DropDownList,該DropDownList的ID與標記中的ID不同。

如果您正在使用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);
}

可能還會有更多問題-但這有望簡化它以使您有所進步。

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

            }
        }
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM