I'm creating programmatically event(s) when a dropdownlist SelectedIndexChanged. Now that's not working but i think it has something todo with the postback but all the rest is working so far.
Adding a new row and storing the value of the previous row are working fine. I don't find a working solution for my programmatically created event(s).
My code is like this =>
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);
}
}