DropDownList在转发器内的SelectedindexChanged不会触发

时间:2014-04-02 08:12:03

标签: vb.net repeater

我正在使用用户控制页面(.ascx)。我有一个转发器,在里面我有一个DropDownList和一些文本框。我需要触发SelectedIndexChanged事件以从文本框中获取值。

这是我得到的错误: 'ddlEye_SelectedIndexChanged'不是'ASP.controls_claims_laborder_ascx'的成员。

HTML:

<asp:Repeater ID="rptProducts" runat="server">
    <HeaderTemplate>
        <tr class="Header">
            <td>Eye</td>
            <td>Tarrif</td>
            <td>Description</td>
            <td>Lab Price</td>
        </tr>
    </HeaderTemplate>
        <ItemTemplate>
            <tr>
                <td> 
                    <asp:DropDownList ID="ddlEye" runat="server" OnSelectedIndexChanged="ddlEye_SelectedIndexChanged" AutoPostBack="true">
                        <asp:ListItem Text="N/A" />
                        <asp:ListItem Text="Left" />
                        <asp:ListItem Text="Right" />
                    </asp:DropDownList>
                </td>
                <td>
                    <asp:TextBox ID="txtTariff" runat="server" ReadOnly="true" Text='<%# DataBinder.Eval(Container.DataItem, "Code") %>' />
                </td>
                <td>
                    <asp:TextBox ID="txtDescription" runat="server" ReadOnly="true" Text='<%# DataBinder.Eval(Container.DataItem, "Description") %>' />
                </td>
                <td>
                    <asp:TextBox ID="txtLabPrice" runat="server" ReadOnly="true" Text='<%# DataBinder.Eval(Container.DataItem, "Invoice") %>' />
                </td>
            </tr>
        </ItemTemplate>
</asp:Repeater>

代码隐藏:

Private Sub ddlEye_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles ddlEye.SelectedIndexChanged
        Dim dEye As DropDownList = DirectCast(sender, DropDownList)

        If ddlEye.SelectedItem.Value = "Right" Then
            lblLeftLensDesc.Text = "Description of the left lens"
            lblRightLensDesc.Text = dEye.SelectedItem.Text
        ElseIf ddlEye.SelectedItem.Value = "Left" Then
            lblLeftLensDesc.Text = dEye.SelectedItem.Text
            lblRightLensDesc.Text = "Description of the right lens"
        Else
            lblLeftLensDesc.Text = "Description of the left lens"
            lblRightLensDesc.Text = "Description of the right lens"
        End If
End Sub

我还试图为DropDownList动态创建事件,但仍然无效。

Private Sub rptProducts_ItemDataBound(sender As Object, e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles rptProducts.ItemDataBound
        Dim dlEye As DropDownList = e.Item.FindControl("ddlEye")
        Dim txtDesc As TextBox = e.Item.FindControl("txtDescription")

        If Not dlEye Is Nothing AndAlso Not txtDesc Is Nothing Then
            dlEye.SelectedIndexChanged += New EventHandler(ddlEye_SelectedIndexChanged)
            dlEye.SelectedValue = txtDesc.Text
        End If

End Sub

转发器绑定代码:

 Private Sub loadLineItems(ByVal strTransactionID As String)
     Dim objLineItems As ArrayList = Managers.LineItem.GetLineItems(CInt(strTransactionID))

     If objLineItems.Count > 0 Then
         rptProducts.DataSource = objLineItems
         rptProducts.DataBind()
     Else
         rptProducts.DataSource = Nothing
         rptProducts.DataBind()
     End If
End Sub

If Not IsPostBack Then
    loadLineItems(Session("pstrTransactionID").ToString())
End If

2 个答案:

答案 0 :(得分:1)

  

这是我得到的错误:&#39; ddlEye_SelectedIndexChanged&#39;不是一个   ASP.controls_claims_laborder_ascx&#39;的成员。

原因是您的事件处理程序是Private。您需要将其设为ProtectedPublic,或者您无法在ascx上以声明方式添加事件处理程序,但您需要在代码隐藏中以编程方式添加它(使用HandlesAddHandler)。

例如:

Protected Sub ddlEye_SelectedIndexChanged(sender As Object, e As System.EventArgs) 
    ' ...
End Sub

或(注意Handles

Private Sub ddlEye_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles ddlEye.SelectedIndexChanged
    ' ...
End Sub

您需要从ascx

中删除以下内容
OnSelectedIndexChanged="ddlEye_SelectedIndexChanged"

旁注:您使用

AddHandler dlEye.SelectedIndexChanged, AddressOf ddlEye_SelectedIndexChanged
<>在VB.NET中。

<强>更新

  

非常感谢你的工作。也许你可以帮助我另一个   问题,我想获取与该行相同的文本框值   当setectedIndex改变时DropDownList?

您可以通过RepeaterItem获取NamingContainer,您只需要相应地投射它。然后,您可以使用FindControl("ID")获取TextBox

Protected Sub ddlEye_SelectedIndexChanged(sender As Object, e As System.EventArgs) 
        Dim dEye As DropDownList = DirectCast(sender, DropDownList)
        Dim item = DirectCast(dEye.NamingContainer, RepeaterItem)
        Dim txtTariff = DirectCast(item.FindControl("txtTariff"), TextBox)
        ' ...
End Sub

答案 1 :(得分:-1)

我不知道出了什么问题,但DropDownListSelectedIndexChanged事件即使发生了 启动了ddlGarmentList控件:

背后的代码:

public partial class NewStyleItem : System.Web.UI.Page
{
    protected StyleTemplateManager styleTempMngr = new StyleTemplateManager();

    protected StyleFabricationManager styleFabMngr = new StyleFabricationManager();

    protected List<ComponentReferenceView> ReferenceLookUpList
    {
        get
        {
            return (List<ComponentReferenceView>)ViewState["ReferenceLookUpView"];
        }
        set
        {
            ViewState["ReferenceLookUpView"] = value;
        }
    }

    protected List<ComponentReferenceView> ReferenceViewList
    {
        get
        {
            return (List<ComponentReferenceView>)ViewState["CompReferenceView"];
        }
        set
        {
            ViewState["CompReferenceView"] = value;
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (this.IsPostBack == false)
        {
            LoadBrandList();
            ddlGarmentList.Enabled = false;

            //ReferenceLookUpList = new List<ComponentReferenceView>();
        }
        else
        {
        }

        //rptStyleComponents.DataSource = ReferenceLookUpList;
        //rptStyleComponents.DataBind();
    }

    protected void LoadBrandList()
    {
        BrandManager brandMngr = new BrandManager();
        ddlBrandList.Items.Clear();

        ddlBrandList.Items.Add(new ListItem { Text = "Select brand", Value = "" });
        foreach (Brand item in brandMngr.Brands())
        {
            ddlBrandList.Items.Add(new ListItem
            {
                Text = item.BrandDescription,
                Value = item.BrandCode
            });
        }
    }

    protected void LoadComponentInfo(string BrandCode)
    {
        if (styleTempMngr.FindTemplateByBrandCode(BrandCode) == true)
        {
            ReferenceLookUpList = (from StyleComponent i in styleFabMngr.ComponentList
                                   join StyleFabReference x in styleTempMngr.ReferenceList on
                                   i.SectionCode equals x.SectionCode
                                   select new ComponentReferenceView
                                   {
                                       InOrder = x.SortOrder,
                                       IsNeeded = x.IsRequired,
                                       SCode = x.SeriesCode,
                                       SecCode = x.SectionCode,
                                       SecName = i.SectionName,
                                       EntryValue = x.DataCharacter
                                   }).OrderBy(p => p.InOrder).ToList();

            ReferenceLookUpList.Where(row => row.SecCode == "BCODE").ToList()
                .ForEach(refItem => refItem.EntryValue = ddlBrandList.SelectedValue);

            var DistinctList = (from StyleFabReference x in styleTempMngr.ReferenceList.OrderBy(p => p.SortOrder).ToList()
                                where x.SectionCode != "SPCHAR" && x.SectionCode != "BCODE"
                                select x.SectionCode).Distinct().ToList();

            ReferenceViewList = (from StyleComponent i in styleFabMngr.ComponentList
                                 join x in DistinctList on
                                 i.SectionCode equals x
                                 select new ComponentReferenceView
                                 {
                                     SecCode = x,
                                     SecName = i.SectionName
                                 }).OrderBy(p => p.InOrder).ToList();
        }
        else
            ReferenceViewList = new List<ComponentReferenceView>();

        rptStyleComponents.DataSource = ReferenceViewList;
        rptStyleComponents.DataBind();

    }

    protected void LoadGarments(string ApparelCode)
    {
        if (string.IsNullOrEmpty(ddlBrandList.SelectedValue) == true)
        {
            ddlGarmentList.SelectedIndex = 0;
            ddlGarmentList.Enabled = false;
        }
        else
        {
            ddlGarmentList.Enabled = true;
            GarmentManager gmMngr = new GarmentManager();
            List<Garment> garmentList = new List<Garment>();
            garmentList = gmMngr.GetGarmentsByBrand(ddlBrandList.SelectedValue, ApparelCode);
            ddlGarmentList.Items.Clear();
            ddlGarmentList.Items.Add(new ListItem { Text = "Select garment", Value = "" });

            foreach (Garment item in garmentList)
            {
                ddlGarmentList.Items.Add(new ListItem { Text = item.GarmentDescription, Value = item.GarmentCode });
            }
        }
    }

    protected void ddlBrandList_SelectedIndexChanged(object sender, EventArgs e)
    {
        txtNewStyleNo.Text = string.Empty;
        if (string.IsNullOrEmpty(ddlBrandList.SelectedValue) == false)
            LoadComponentInfo(ddlBrandList.SelectedValue);
    }

    [Serializable]
    public class ComponentReferenceView
    {
        public string SCode
        {
            get;
            set;
        }
        public string SecCode
        {
            get;
            set;
        }
        public string SecName
        {
            get;
            set;
        }
        public int InOrder
        {
            get;
            set;
        }
        public bool IsNeeded
        {
            get;
            set;
        }
        public string EntryValue
        {
            get;
            set;
        }
    }

    protected void rptStyleComponents_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {

    }

    protected void rptStyleComponents_ItemCreated(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            DropDownList ddlControl = (DropDownList)e.Item.FindControl("ddlDataSelection");

            if (ddlControl != null)
            {
                ddlControl.AppendDataBoundItems = true;
                ddlControl.Items.Add(new ListItem { Text = "Select " + ReferenceViewList[e.Item.ItemIndex].SecName.ToLowerInvariant(), Value = "" });
                var itemlist = (from x in styleFabMngr.ComponentRowItems(ReferenceViewList[e.Item.ItemIndex].SecCode).AsEnumerable()
                                select new ListItem
                                {
                                    Text = x.Field<string>("col_name"),
                                    Value = x.Field<string>("col_value")
                                });
                ddlControl.Items.AddRange(itemlist.ToArray<ListItem>());

                ddlControl.EnableViewState = false;
                ddlControl.AutoPostBack = true;
                ddlControl.SelectedIndexChanged += new EventHandler(DropDownListSelectedIndexChanged);
            }
        }
    }

    protected virtual void DropDownListSelectedIndexChanged(object sender, EventArgs e)
    {
        DropDownList ddl = (DropDownList)sender;
        RepeaterItem item = (RepeaterItem)ddl.NamingContainer;

        ReferenceViewList[item.ItemIndex].EntryValue = ddl.SelectedValue;
        ConstructStyleNo();

        var Apparelfound = ReferenceViewList.Where(x => x.SecCode == "APCODE" &&
           string.IsNullOrEmpty(x.EntryValue) == false).FirstOrDefault();

        if (Apparelfound != null)
        {
            ddlGarmentList.Enabled = true;
            LoadGarments(Apparelfound.EntryValue);
        }
        else
        {
            ddlGarmentList.Enabled = false;
        }
    }

    private void ConstructStyleNo()
    {
        foreach (ComponentReferenceView item in ReferenceViewList)
        {
            ReferenceLookUpList.Where(row => row.SecCode == item.SecCode).ToList()
                .ForEach(refItem => refItem.EntryValue = item.EntryValue);
        }

        string sResult = string.Empty;
        var result = (from x in ReferenceLookUpList.OrderBy(y => y.InOrder)
                      select x.EntryValue);

        txtNewStyleNo.Text = string.Empty;
        if (result != null)
            txtNewStyleNo.Text = String.Join("", result) + "0001";

    }

    private void ConstructStyleDescription()
    {
        txtStyleDescription.Text = ddlBrandList.SelectedItem.Text + "-" + ddlGarmentList.SelectedItem.Text;
    }

    protected void ddlGarmentList_SelectedIndexChanged(object sender, EventArgs e)
    {
        txtStyleDescription.Text = string.Empty;
        if (string.IsNullOrEmpty(ddlGarmentList.SelectedValue) == false)
            ConstructStyleDescription();
    }
}

ASPX:

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    <asp:UpdatePanel ID="upnlNewFabrication" runat="server">
        <ContentTemplate>
            <h1>
            New Style Item
            </h1>
            <br />
            <%--<usrCtrl:style_generation ID="stylegen_frm" runat="server">
            </usrCtrl:style_generation>--%>

        <table>
            <tr>
                <td>
                  Brand :
                </td>
                <td>
                    <asp:DropDownList ID="ddlBrandList" runat="server" 
                        onselectedindexchanged="ddlBrandList_SelectedIndexChanged" 
                        AutoPostBack="True">
                    </asp:DropDownList> 
                </td>
            </tr>           
            <tr>
                <td colspan="2">
                &nbsp;
                </td>
            </tr>
            <tr>
                <td>
                    Generated Style# :
                </td>
                <td>
                    <asp:TextBox ID="txtNewStyleNo" runat="server" ReadOnly="true"></asp:TextBox>                   
                </td>
            </tr>
            <tr>
                <td>
                    Style Description :
                </td>
                <td>
                    <asp:TextBox ID="txtStyleDescription" runat="server" ReadOnly="true"></asp:TextBox>                   
                </td>
            </tr>
                    <asp:Repeater ID="rptStyleComponents" runat="server" 
                        onitemdatabound="rptStyleComponents_ItemDataBound" 
                        onitemcreated="rptStyleComponents_ItemCreated" 
                EnableViewState="True">
                        <ItemTemplate>
                            <asp:UpdatePanel ID="upnlComponent" runat="server">
                                <ContentTemplate>
                                     <tr>
                                        <td>
                                            <asp:Label ID="lblSectionName" runat="server" Text='<%# Eval("SecName")%>' ></asp:Label>
                                        </td>
                                        <td>
                                            <asp:DropDownList ID="ddlDataSelection" runat="server">
                                            </asp:DropDownList>                    
                                        </td>
                                    </tr>                               
                                </ContentTemplate>
                                <Triggers>
                                    <asp:PostBackTrigger ControlID="ddlDataSelection" />
                                </Triggers>
                            </asp:UpdatePanel>                           
                        </ItemTemplate>
                    </asp:Repeater>   
        </table>
        <br />
        <table>
            <tr>
            <td colspan="2">Description Items</td>
            </tr>
            <tr>
                <td colspan="4"></td>
            </tr>
            <tr>
                <td>Garment Type:</td>
                <td>
                    <asp:UpdatePanel ID="upGarment" runat="server" >
                    <ContentTemplate>
                        <asp:DropDownList ID="ddlGarmentList" runat="server" 
                            AppendDataBoundItems="True" AutoPostBack="True" 
                            onselectedindexchanged="ddlGarmentList_SelectedIndexChanged">
                            <asp:ListItem Text = "Select garment" Value = ""></asp:ListItem>    
                        </asp:DropDownList>
                        <asp:SqlDataSource ID="sdsGarment" runat="server" 
                            ConnectionString="<%$ ConnectionStrings:IRMSConnectionString %>" 
                            SelectCommand="Select * from Garments"></asp:SqlDataSource>
                    </ContentTemplate>
                    <Triggers>
                        <asp:PostBackTrigger ControlID="ddlGarmentList"  />
                    </Triggers>
                    </asp:UpdatePanel>

                </td>
            </tr>
            <tr>
                <td>Cost Price:</td>
                <td>
                    <asp:TextBox ID="txtCostPrice" runat="server"></asp:TextBox></td>
                <td>SRP :</td>
                <td><asp:TextBox ID="txtSRP" runat="server"></asp:TextBox></td>
            </tr>
            <tr>
                <td>Quantity :</td>
                <td><asp:TextBox ID="TextBox1" runat="server"></asp:TextBox></td>
                <td>Week :</td>
                <td><asp:TextBox ID="TextBox2" runat="server"></asp:TextBox></td>
            </tr>
        </table>

        </ContentTemplate>
    </asp:UpdatePanel>
</asp:Content>