【问题标题】:ASP.NET programmatically event not workingASP.NET 以编程方式事件不起作用
【发布时间】:2014-12-29 10:44:12
【问题描述】:

当下拉列表 SelectedIndexChanged 时,我正在以编程方式创建事件。现在这不起作用,但我认为它与回发有关,但到目前为止所有其他的都在起作用。

添加新行并存储前一行的值工作正常。我没有为我以编程方式创建的事件找到可行的解决方案。

我的代码是这样的 =>

    static List<TableRow> TableRows = new List<TableRow>();


    protected void Page_Load(object sender, EventArgs e)
    {
            string vorigePagina = Request.UrlReferrer.ToString();

            //Controleer of de pagina gerefreshd werd of voor het eerst geladen wordt.
            if (!vorigePagina.Contains("FactuurToevoegen.aspx"))
            {
                //Als de pagina voor de eerste keer geladen wordt moet de tabel leeggemaakt worden.
                TableRows.Clear();
            }

            //Response.Write(TableRows.Count.ToString());

            //if (TableRows != null)
            //{
            foreach (TableRow row in TableRows)
            {
                tblArtikels.Rows.Add(row);
            }
    )

    public void RijToevoegen()
    {
        try
        {

            if (Factuur.artikelTeller == null && tblArtikels.Rows.Count != null)
            {
                Factuur.artikelTeller = 0;
            }
            else if (Factuur.artikelTeller != 0 && tblArtikels.Rows.Count != 0)
            {
                Factuur.artikelTeller = Factuur.artikelTeller + 1;
            }
            else
            {
                Factuur.artikelTeller = Factuur.artikelTeller + 1;
            }

            int artikelTeller = Factuur.artikelTeller;

            //Alle data halen uit de PassThrough class om opnieuw een connectie te maken met SharePoint
            contextToken = PassThrough.contextToken;
            sharepointUrl = PassThrough.sharepointUrl;
            accessToken = PassThrough.accessToken;
            ClientContext clientContext = TokenHelper.GetClientContextWithAccessToken(sharepointUrl.ToString(), accessToken);

            //Dynamisch de rijen aanmaken met de producten waar het aantal, artikel, prijs en btw worden weergegeven.
            TableRow row = new TableRow();
            TableCell cell1 = new TableCell();
            TableCell cell2 = new TableCell();
            TableCell cell3 = new TableCell();
            TableCell cell4 = new TableCell();
            TextBox txtArtikelAantal = new TextBox();
            TextBox txtArtikel = new TextBox();
            TextBox txtArtikelPrijs = new TextBox();
            TextBox txtArtikelBTWPercentage = new TextBox();

            DropDownList ddlArtikel = new DropDownList();
            ddlArtikel.AutoPostBack = true;
            ddlArtikel.Width = 180;
            ddlArtikel.SelectedIndexChanged += new EventHandler(ddlArtikel_SelectedIndexChanged);
            ddlArtikel.EnableViewState = true;

            //Breedte instellen van de textboxes en td's (cellen)
            txtArtikelAantal.Width = 50;
            txtArtikelPrijs.Width = 100;
            txtArtikelBTWPercentage.Width = 50;
            cell1.Width = 120;
            cell2.Width = 364;
            cell3.Width = 180;
            cell4.Width = 60;
            cell4.HorizontalAlign = HorizontalAlign.Right;

            row.ID = "row_" + artikelTeller;
            cell1.ID = "Cell1_" + artikelTeller;
            cell2.ID = "Cell2_" + artikelTeller;
            cell3.ID = "Cell3_" + artikelTeller;
            cell4.ID = "Cell4_" + artikelTeller;
            txtArtikelAantal.ID = "txtArtikelAantal_" + artikelTeller;
            txtArtikel.ID = "txtArtikel_" + artikelTeller;
            txtArtikelPrijs.ID = "txtArtikelPrijs_" + artikelTeller;
            txtArtikelPrijs.Enabled = false;
            txtArtikelBTWPercentage.ID = "txtArtikelBTWPercentage_" + artikelTeller;
            txtArtikelBTWPercentage.Visible = true;
            ddlArtikel.ID = "ddlArtikel_" + artikelTeller;

            cell1.Controls.Add(txtArtikelAantal);
            cell2.Controls.Add(ddlArtikel);
            cell3.Controls.Add(txtArtikelPrijs);
            cell4.Controls.Add(txtArtikelBTWPercentage);

            //Lijst met artikelen ophalen en dropdown opvullen
            List oListArtikels = clientContext.Web.Lists.GetByTitle("Lijst artikels");

            clientContext.ExecuteQuery();


            CamlQuery cQArtikels = new CamlQuery();
            cQArtikels.ViewXml = "<View>"
            + "<Query>"
            + "<OrderBy><FieldRef Name='arArtikelOmschrijving'/></OrderBy>"
            + "</Query>"
            + "</View>";

            Microsoft.SharePoint.Client.ListItemCollection artikelListItem = oListArtikels.GetItems(cQArtikels);

            clientContext.Load(artikelListItem);

            clientContext.ExecuteQuery();


            foreach (Microsoft.SharePoint.Client.ListItem artikelItem in artikelListItem)
            {
                string artikelOmschrijving = artikelItem["arArtikelomschrijving"].ToString();
                string artikelPrijsExclBTW = string.Format("{0:0.00}", artikelItem["arBasisprijsExclBTW"].ToString());

                ddlArtikel.Items.Add(new System.Web.UI.WebControls.ListItem(artikelOmschrijving + " (" + artikelPrijsExclBTW + ")", artikelPrijsExclBTW));

                txtArtikelAantal.Text = "1";
                txtArtikelPrijs.Text = string.Format("{0:0.00}", double.Parse(artikelPrijsExclBTW).ToString());

                txtArtikelBTWPercentage.Text = double.Parse((artikelItem["arBTWcode"] as FieldLookupValue).LookupValue).ToString() + "%";
            }

            txtArtikelPrijs.Text = (double.Parse(txtArtikelAantal.Text) * double.Parse(txtArtikelPrijs.Text)).ToString();


            row.Cells.Add(cell1);
            row.Cells.Add(cell2);
            row.Cells.Add(cell3);
            row.Cells.Add(cell4);
            tblArtikels.Rows.Add(row);
            TableRows.Add(row);
        }
        catch (Exception ex)
        {
            Response.Write("Foutbericht artikels: " + ex.Message);
        }
    }

    protected void ddlArtikel_SelectedIndexChanged(object sender, EventArgs e)
    {

        try
        {
            txtTotaalinclBTW.Text = "125";
            Response.Write("Artikel Changed !!!");
        }
        catch (Exception ex)
        {
            Response.Write("Foutbericht Artikelchanged: " + ex.Message);
        }
    }

【问题讨论】:

  • 请澄清“不工作”是什么意思。你有什么例外吗?你的代码到底是哪一行抛出的?
  • 我的意思是事件没有放回任何数据。按照这里的要求 => protected void ddlArtikel_SelectedIndexChanged(object sender, EventArgs e) { try { txtTotaalinclBTW.Text = "125"; Response.Write("Artikel 改变了!!!"); } catch (Exception ex) { Response.Write("Foutbericht Artikelchanged:" + ex.Message); } }
  • 我能听懂荷兰语,但很多人不懂,给你一个建议:用英文写你的代码:)

标签: c# asp.net webforms autopostback


【解决方案1】:

听起来你没有发回服务器。根据这个答案 (DropDownList doesn't postback on SelectedIndexChanged),您必须在页面上的控件上启用自动回发才能发生这种情况

【讨论】:

  • 好吧,我在这里设置自动回发 => DropDownList ddlArtikel = new DropDownList(); ddlArtikel.AutoPostBack = true;
  • @user3512855 如果您在事件处理程序中放置断点,它会被命中吗?如果不是,则该页面不会回发。您能否扩展您的问题以显示您用于控件的 html 以及它不起作用的意思?
  • 该函数受保护 void ddlArtikel_SelectedIndexChanged(object sender, EventArgs e) { try { txtTotaalinclBTW.Text = "125"; Response.Write("Artikel 改变了!!!"); } catch (Exception ex) { Response.Write("Foutbericht Artikelchanged:" + ex.Message); } } 什么也没做。
  • 我在这里加载这个函数 ddlArtikel.SelectedIndexChanged += new EventHandler(ddlArtikel_SelectedIndexChanged);
  • 被击中但没有做任何事情,或者没有被击中?
【解决方案2】:

我尝试了一个我在网上找到的带有回发和动态创建控件的示例。它适用于文本框和复选框,但不适用于我正在创建的事件。请参阅下面的示例。我的活动没有被解雇。 =>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace testDebugger
{
public partial class WebForm1 : System.Web.UI.Page
{
    private int numOfRows = 1;

    protected void Page_Load(object sender, EventArgs e)
    {
        //Generate the Rows on Initial Load
        if (!Page.IsPostBack)
        {
            GenerateTable(numOfRows);
        }
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        if (ViewState["RowsCount"] != null)
        {
            numOfRows = Convert.ToInt32(ViewState["RowsCount"].ToString());
            GenerateTable(numOfRows);
        }
    }

    protected void Button2_Click(object sender, EventArgs e)
    {
        GetSelectedRows();
    }

    private void SetPreviousData(int rowsCount, int colsCount)
    {
        Table table = (Table)Page.FindControl("Table1");
        if (table != null)
        {
            for (int i = 0; i < rowsCount; i++)
            {
                for (int j = 0; j < colsCount; j++)
                {
                    if (j == 0)
                    {
                        //Get the Checked value of the CheckBox using the Request objects
                        string check = Request.Form["CheckBoxRow_" + i + "Col_" + j];
                        //Extract the CheckBox Control from within the Table
                        CheckBox cb = (CheckBox)table.Rows[i].Cells[j].FindControl("CheckBoxRow_" + i + "Col_" + j);
                        if (check == "on") //If selected
                        {
                            cb.Checked = true;
                        }
                    }
                    else if (j == 1)
                    {
                        DropDownList ddl = (DropDownList)table.Rows[i].Cells[j].FindControl("DropDown_" + i + "Col_" + j);
                        ddl.SelectedValue = Request.Form["DropDown_" + i + "Col_" + j];
                    }
                    else
                    {
                        TextBox tb = (TextBox)table.Rows[i].Cells[j].FindControl("TextBoxRow_" + i + "Col_" + j);
                        tb.Text = Request.Form["TextBoxRow_" + i + "Col_" + j];
                    }
                }
            }
        }
    }

    private void GenerateTable(int rowsCount)
    {
        //Creat the Table and Add it to the Page
        Table table = new Table();
        table.ID = "Table1";
        Page.Form.Controls.Add(table);

        //The number of Columns to be generated
        const int colsCount = 3;//You can changed the value of 3 based on you requirements

        // Now iterate through the table and add your controls
        for (int i = 0; i < rowsCount; i++)
        {
            TableRow row = new TableRow();
            for (int j = 0; j < colsCount; j++)
            {
                TableCell cell = new TableCell();

                if (j == 0) //means the first column of the Table
                {
                    //Create the CheckBox
                    CheckBox cb = new CheckBox();
                    // Set a unique ID for each CheckBox
                    cb.ID = "CheckBoxRow_" + i + "Col_" + j;
                    // Add the control to the FIRST TableCell
                    cell.Controls.Add(cb);
                    // Add the TableCell to the TableRow
                    row.Cells.Add(cell);

                }
                else if (j == 1) //means the first column of the Table
                {
                    //Create the CheckBox
                    DropDownList ddl = new DropDownList();
                    // Set a unique ID for each CheckBox
                    ddl.ID = "DropDown_" + i + "Col_" + j;
                    ddl.EnableViewState = true;
                    ddl.EnableTheming = true;
                    ddl.AutoPostBack = true;
                    ddl.SelectedIndexChanged += new EventHandler(this.ddlArtikel_SelectedIndexChanged);
                    ddl.Items.Add("testttttttt");
                    ddl.Items.Add("testttttttt22");
                    ddl.Items.Add("testttttttt33");

                    // Add the control to the FIRST TableCell
                    cell.Controls.Add(ddl);
                    // Add the TableCell to the TableRow
                    row.Cells.Add(cell);

                }
                else
                {
                    //Create the TextBox
                    TextBox tb = new TextBox();
                    // Set a unique ID for each TextBox
                    tb.ID = "TextBoxRow_" + i + "Col_" + j;
                    // Add the control to the TableCell
                    cell.Controls.Add(tb);
                    // Add the TableCell to the TableRow
                    row.Cells.Add(cell);
                }
            }

            // And finally, add the TableRow to the Table
            table.Rows.Add(row);
        }

        //Set Previous Data on PostBacks
        SetPreviousData(rowsCount, colsCount);

        //Sore the current Rows Count in ViewState
        rowsCount++;
        ViewState["RowsCount"] = rowsCount;
    }

    private void GetSelectedRows()
    {
        if (ViewState["RowsCount"] != null)
        {
            numOfRows = Convert.ToInt32(ViewState["RowsCount"].ToString());
            int removedRows = numOfRows - 1;

            //Re create the Table on Postbacks
            GenerateTable(numOfRows - 1);

            Table table = (Table)Page.FindControl("Table1");
            if (table != null)
            {
                if (table.Rows.Count > 0)
                {
                    for (int i = table.Rows.Count - 1; i >= 0; i--)
                    {
                        //Get the Checked value of the CheckBox using the Request objects
                        string check = Request.Form["CheckBoxRow_" + i + "Col_" + 0];
                        //Extract the CheckBox Control from within the Table
                        if (check == "on") //If selected
                        {
                            table.Rows.Remove(table.Rows[i]);
                            removedRows--;
                        }
                    }
                    ViewState["RowsCount"] = removedRows + 1;
                }
            }
        }
    }


    protected void ddlArtikel_SelectedIndexChanged(object sender, EventArgs e)
    {
        try
        {

            numOfRows = Convert.ToInt32(ViewState["RowsCount"].ToString()) - 1;

            //Set Previous Data on PostBacks
            GenerateTable(numOfRows);

            Response.Write("Artikel Changed !!!");
        }
        catch (Exception ex)
        {
            Response.Write("Foutbericht Artikelchanged: " + ex.Message);
        }
    }



 }

}

【讨论】:

    猜你喜欢
    • 2021-09-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-21
    相关资源
    最近更新 更多