无法从后端获取fireDownList selectionChange事件

时间:2013-02-25 10:39:12

标签: c# asp.net

我无法在Selection-change上触发该方法。我正在从代码后面添加DropDownList本身。代码:

  private TableCell CreateMTPLTypeCell(int insurerId, string MTPLType)
        {

            TableCell rg = new TableCell();
            rg.ID = string.Concat("rg_", insurerId);
            rg.CssClass = "formItem";
            //if (insurerId == 14)
            //{
                DropDownList ddl = new DropDownList();
                ddl.ID = "MTPLTypes";
                ddl.Items.Add(new ListItem("Standard", "1")); // add list items
                ddl.Items.Add(new ListItem("DubultOCTA", "2"));
                ddl.Items.Add(new ListItem("DubultOCTA+vējst", "3"));
                //ddl.SelectedIndex =
                //    ddl.Items.IndexOf(ddl.Items.
                //        FindByValue(MTPLType));
                ddl.SelectedIndexChanged += new EventHandler(MTPLType_selectedIndexChange);
                ddl.AutoPostBack = true;
                rg.Controls.Add(ddl);
            //}
            return rg;
        }

        void MTPLType_selectedIndexChange(object sender, EventArgs e)
        {
            DropDownList ddl = (DropDownList)sender;
            int insurerId = Int32.Parse(ddl.ID.Replace("rg_", ""));

            MTPLQuotes quotes = proposal.Properties.MtplQuotes[insurerId];
            if (quotes == null) return;

            proposal.GetSingleMTPLQuote(insurerId, Helpers.PortalUtilities.CurrentUser, BrokerInfo.GetBrokerInfo().Country.ID);

            DrawMtplQuotes(mtplPaymentMappingsCount > 0);
        }

我是否会错过DropDownList对象的一些额外参数?

Atm它的工作原理如下:在选择改变之后,它开始“加载”,但它达到了方法。 :/

在同一个班级中我有onChange事件(Text_Change)的antoher tableCell - >这很好用。

private TableCell CreateInsurerMtplDiscountCell(int insurerId, decimal discount)
    {
        TableCell c = new TableCell();
        c.CssClass = "formItem";
        c.Style[HtmlTextWriterStyle.PaddingLeft] = "25px";

        TextBox txt = new TextBox();
        txt.ID = string.Format("ins_disc_{0}", insurerId);
        txt.Text = discount > 0 ? discount.ToString() : "";
        txt.TextChanged += new EventHandler(insurerDiscountPercent_TextChanged);
        txt.AutoPostBack = true;
        txt.Width = new Unit(40);
        c.Controls.Add(txt);

        Literal l = new Literal();
        l.Text = "%";
        c.Controls.Add(l);

        return c;
    }

    void insurerDiscountPercent_TextChanged(object sender, EventArgs e)
    {
        TextBox txt = (TextBox)sender;

        int insurerId = Int32.Parse(txt.ID.Replace("ins_disc_", ""));

        MTPLQuotes quotes = proposal.Properties.MtplQuotes[insurerId];
        if (quotes == null) return;

        decimal discountPercent;
        if (!Decimal.TryParse(txt.Text, out discountPercent))
            discountPercent = 0;

        quotes.InsurerDiscountPercent = discountPercent;
        foreach (MTPLQuote quote in quotes.Quotes)
            ApplyMtplInsurerDiscount(quote, discountPercent);

        DrawMtplQuotes(mtplPaymentMappingsCount > 0);
    }

解决了问题 - 问题是关于TableCell.ID(rg.ID = string.Concat(“rg_”,insurerId);)。删除后一切正常。任何人都知道为什么会这样吗?

2 个答案:

答案 0 :(得分:0)

我认为您必须在创建新控件后刷新您的网站。 我建议你使用AJAX-updatepanels(所以你不必刷新整个网站)。

  1. 要添加OnChanged-Event Name,您还应该能够添加字符串Name而不是eventhandler itselfe。

    ddl.SelectedIndexChanged =“MTPLType_selectedIndexChange”;

  2. 我认为您必须使用"protected void"代替"private void"才能获得权限限制的控制原因。

答案 1 :(得分:0)

您应该将SelectedIndexChanged事件绑定到Page_PreInit事件的下拉列表。

然后它应该工作。

相关问题