C# ASP.NET编程事件不工作
当dropdownlist SelectedIndex发生更改时,我正在以编程方式创建事件。现在这不起作用了,但我认为这与回发有关,但到目前为止,所有其他的都起作用了 添加新行和存储前一行的值都可以正常工作。对于以编程方式创建的事件,我找不到有效的解决方案 我的代码如下=>C# ASP.NET编程事件不工作,c#,asp.net,webforms,autopostback,C#,Asp.net,Webforms,Autopostback,当dropdownlist SelectedIndex发生更改时,我正在以编程方式创建事件。现在这不起作用了,但我认为这与回发有关,但到目前为止,所有其他的都起作用了 添加新行和存储前一行的值都可以正常工作。对于以编程方式创建的事件,我找不到有效的解决方案 我的代码如下=> static List<TableRow> TableRows = new List<TableRow>(); protected void Page_Load(object se
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);
}
}
静态列表TableRows=新列表();
受保护的无效页面加载(对象发送方、事件参数e)
{
字符串vorigePagina=Request.urlReferer.ToString();
//帕吉纳的控制者格雷弗瑞德·沃德(voor het-eerst geladen wordt)。
如果(!vorigePagina.Contains(“FactuurToevoegen.aspx”))
{
//这是一个很好的例子。
TableRows.Clear();
}
//Write(TableRows.Count.ToString());
//if(TableRows!=null)
//{
foreach(TableRow中的TableRow行)
{
tblArtikels.Rows.Add(row);
}
)
公共无效RijToevoegen()
{
尝试
{
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;
}
其他的
{
Factuur.artikelTeller=Factuur.artikelTeller+1;
}
int artikelTeller=Factuur.artikelTeller;
//所有数据都是通过类om opnieuw连接的,并符合SharePoint要求
contextToken=PassThrough.contextToken;
sharepointUrl=PassThrough.sharepointUrl;
accessToken=PassThrough.accessToken;
ClientContext ClientContext=TokenHelper.GetClientContextWithAccessToken(sharepointUrl.ToString(),accessToken);
//阿恩马肯在一个星期内会见了阿兰塔尔、阿蒂克尔、普里斯和韦尔吉文的生产商。
TableRow行=新TableRow();
TableCell cell1=新的TableCell();
TableCell cell2=新的TableCell();
TableCell cell3=新的TableCell();
TableCell cell4=新的TableCell();
TextBox txtarikelaantal=新的TextBox();
TextBox txtArtikel=新的TextBox();
TextBox txtArtikelPrijs=新建TextBox();
TextBox txtarikelbtwpercentage=新的TextBox();
DropDownList ddlArtikel=新的DropDownList();
ddlArtikel.AutoPostBack=true;
ddlArtikel.宽度=180;
ddlArtikel.SelectedIndexChanged+=新事件处理程序(ddlArtikel_SelectedIndexChanged);
ddlArtikel.EnableViewState=true;
//Breedte instellen van de Textbox en td(塞伦)
TXTATIKELAANTAL.宽度=50;
TXTATIKELPRIJS.宽度=100;
txtArtikelBTWPercentage.Width=50;
单元1.宽度=120;
单元2.宽度=364;
单元3.宽度=180;
单元4.宽度=60;
cell4.HorizontalAlign=HorizontalAlign.Right;
row.ID=“row_u”+artikelTeller;
cell1.ID=“cell1_216;”+artikelTeller;
cell2.ID=“cell2_216;”+artikelTeller;
cell3.ID=“cell3_3;”+Artikeller;
cell4.ID=“cell4_216;”+Artikeller;
txtArtikelAantal.ID=“txtArtikelAantal”+artikellantal;
txtArtikel.ID=“txtArtikel_”+artikelTeller;
txtArtikelPrijs.ID=“txtArtikelPrijs”+artikelTeller;
txtarikelprijs.Enabled=false;
txtArtikelBTWPercentage.ID=“txtArtikelBTWPercentage”+artikelTeller;
txtArtikelBTWPercentage.Visible=true;
ddlArtikel.ID=“ddlArtikel_”+artikelTeller;
cell1.Controls.Add(txtarikelaantal);
cell2.Controls.Add(ddlArtikel);
cell3.Controls.Add(txtarikelprijs);
cell4.Controls.Add(txtArtikelBTWPercentage);
//Lijst遇到了artikelen Opalen和dropdown Opvulen
List-oListArtikels=clientContext.Web.Lists.GetByTitle(“Lijst-artikels”);
clientContext.ExecuteQuery();
CamlQuery cQArtikels=新的CamlQuery();
cQArtikels.ViewXml=“”
+ ""
+ ""
+ ""
+ "";
Microsoft.SharePoint.Client.ListItemCollection artikelListItem=oListArtikels.GetItems(cQArtikels);
clientContext.Load(artikelistitem);
clientContext.ExecuteQuery();
foreach(artikelListItem中的Microsoft.SharePoint.Client.ListItem artikelItem)
{
字符串artikelomschrijing=artikelItem[“arartikelomschrijing”]。ToString();
string artikelprijsexclubtw=string.Format(“{0:0.00}”,artikelItem[“arbasisprijsexclubtw”].ToString());
ddlArtikel.Items.Add(新的System.Web.UI.WebControls.ListItem(artikelOmschrijving+“(“+artikelprijsexclubtw+””),artikelprijsexclubtw));
txtarikelaantal.Text=“1”;
Text=string.Format(“{0:0.00}”,double.Parse(artikelPrijsExclBTW.ToString());
txtArtikelBTWPercentage.Text=double.Parse((artikelItem[“arBTWcode”]作为FieldLookupValue).LookupValue.ToString()+“%”;
}
Text=(double.Parse(txtarikelaantal.Text)*double.Parse(txtarikelprijs.Text)).ToString();
row.Cells.Add(cell1);
行。单元格。添加(单元格2);
行。单元格。添加(单元格3);
行。单元格。添加(单元格4);
tblArtikels.Rows.Add(row);
TableRows.Add(行);
}
捕获(例外情况除外)
{
回复。写下(“Foutbericht artikels:+ex.Message”);
}
}
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);
}
}
}
}