I tried to create dynamic buttons after clicked on "searchbutton". But I always get returned to the homePage and do not enter the BtnBuy_ServerClick function.
GetPostBackControl() - return null if don't found control, else the control.
See at the end the adding and creating of buttons.
I am glad for any help with that issue.
Thank you!
public partial class HomePageDesignaspx : System.Web.UI.Page
{
private static List<Button> buttonList;
protected void Page_Load(object sender, EventArgs e)
{
if (buttonList != null)
{
for (int i = 0; i < buttonList.Count(); i++)
{
buttonList[i].Click += new EventHandler(this.BtnBuy_ServerClick);
}
}
else
{
buttonList = new List<Button>();
}
Control c = GetPostBackControl(Page);
if (c != null && c.ID == "SearchBtn")
{
DataBase db = DataBase.Instance;
flights = db.GetFlights(companyValue, sourceValue, destinationValue, date, minPriceS, maxPriceS);
for (int i = 0; i < flights.Count; i++)
{
if (i % 3 == 0)
{
HtmlGenericControl drow = new HtmlGenericControl("div");
drow.ID = "drow" + i.ToString();
drow.Attributes.Add("class", "row");
this.flightTable.Controls.Add(drow);
}
HtmlGenericControl dCol = new HtmlGenericControl("div");
dCol.ID = "dCol" + i.ToString();
dCol.Attributes.Add("class", "col-sm-4");
this.flightTable.Controls.Add(dCol);
HtmlGenericControl dCard = new HtmlGenericControl("div");
dCard.ID = "dCard" + i.ToString();
dCard.Attributes.Add("class", "card-section");
dCol.Controls.Add(dCard);
HtmlGenericControl dCardImg = new HtmlGenericControl("div");
dCardImg.ID = "dCardImg" + i.ToString();
dCardImg.Attributes.Add("class", "card-section-image");
dCard.Controls.Add(dCardImg);
HtmlGenericControl CardImg = new HtmlGenericControl("img");
CardImg.ID = "CardImg" + i.ToString();
CardImg.Attributes.Add("src", "../Images/plane2.jpg");
dCardImg.Controls.Add(CardImg);
HtmlGenericControl dCardDesc = new HtmlGenericControl("div");
dCardDesc.ID = "dCardDesc" + i.ToString();
dCardDesc.Attributes.Add("class", "card-desc");
dCard.Controls.Add(dCardDesc);
HtmlGenericControl dCardTitle = new HtmlGenericControl("div");
dCardTitle.ID = "dCardTitle" + i.ToString();
dCardTitle.Attributes.Add("class", "title");
dCardDesc.Controls.Add(dCardTitle);
HtmlGenericControl H5Title = new HtmlGenericControl("h5");
H5Title.ID = "H53Title" + i.ToString();
H5Title.Attributes.Add("class", "card-desc");
H5Title.InnerText = "Num flight is " + flights[i].Id;
dCardTitle.Controls.Add(H5Title);
HtmlGenericControl dCardInfo = new HtmlGenericControl("div");
dCardInfo.ID = "dCardInfo" + i.ToString();
dCardInfo.Attributes.Add("class", "card-info");
dCardDesc.Controls.Add(dCardInfo);
HtmlGenericControl UlList = new HtmlGenericControl("ul");
UlList.ID = "UlList" + i.ToString();
UlList.Attributes.Add("class", "list-unstyle");
dCardInfo.Controls.Add(UlList);
HtmlGenericControl LiDate = new HtmlGenericControl("li");
LiDate.ID = "LiDate" + i.ToString();
UlList.Controls.Add(LiDate);
HtmlGenericControl iDate = new HtmlGenericControl("i");
iDate.ID = "iDate" + i.ToString();
iDate.Attributes.Add("class", "far fa-calendar-alt");
LiDate.Controls.Add(iDate);
HtmlGenericControl LabelDate = new HtmlGenericControl("label");
LabelDate.ID = "LabelDate" + i.ToString();
LabelDate.InnerText = "Date: " + flights[i].DateFlight;
LiDate.Controls.Add(LabelDate);
HtmlGenericControl LiPlace = new HtmlGenericControl("li");
LiPlace.ID = "LiPlace" + i.ToString();
UlList.Controls.Add(LiPlace);
HtmlGenericControl iPlace = new HtmlGenericControl("i");
iPlace.ID = "iPlace" + i.ToString();
iPlace.Attributes.Add("class", "fas fa-map-marker-alt");
LiPlace.Controls.Add(iPlace);
HtmlGenericControl LabelPlace = new HtmlGenericControl("label");
LabelPlace.ID = "LabelPlace" + i.ToString();
LabelPlace.InnerText = "Place: " + flights[i].Source + "->" + flights[i].Destination;
LiPlace.Controls.Add(LabelPlace);
HtmlGenericControl LiPrice = new HtmlGenericControl("li");
LiPrice.ID = "LiPrice" + i.ToString();
UlList.Controls.Add(LiPrice);
HtmlGenericControl iPrice = new HtmlGenericControl("i");
iPrice.ID = "iPrice" + i.ToString();
iPrice.Attributes.Add("class", "fas fa-hand-holding-usd");
LiPrice.Controls.Add(iPrice);
HtmlGenericControl LabelPrice = new HtmlGenericControl("label");
LabelPrice.ID = "LabelPrice" + i.ToString();
LabelPrice.InnerText = "Price: " + flights[i].Price;
LiPrice.Controls.Add(LabelPrice);
Button BtnBuy = new Button();
BtnBuy.ID = flights[i].Id;
BtnBuy.Attributes["class"] = "cart_btn btn btn-dark";
BtnBuy.Attributes.Add("type", "submit");
BtnBuy.Text = "Buy Now";
buttonList.Add(BtnBuy);
BtnBuy.Click += new EventHandler(this.BtnBuy_ServerClick);
UlList.Controls.Add(BtnBuy);
}
}
}
}
void BtnBuy_ServerClick(object sender, EventArgs e)
{
string buttonId = ((HtmlButton)sender).ID;
Session["IdFlight"] = buttonId;
Server.Transfer("~/WebPage/BuyFlight.aspx");
}
ASPX Markup in homePage.aspx:
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<section class="card">
<div class="row" id="flightTable" runat="server">
<!--Place for tickets after the search-->
</div>
</section>
I have been developing an application using winforms c# .net 4.0.
this application use datagridview and i add combobox + textbox to the datagridview.
During binddata and resizing actions, a black rectangle will draw in the bottom portion of the datagridview.
see the image of the problems
it works perfectly on standard DPI, but problem on the high DPI.
here is the some of my code to binddata and resizing.
using (Class1.Connection = new OleDbConnection(Class1.ConnString))
{
string sql1 = "SELECT tbAuditDetails.AuditNo, tbAuditQuestions.AutoSubcontent, tbAuditQuestions.AutoID, tbAuditQuestions.Questions, tbScore.Description, tbAuditDetails.QuestionID, tbAuditQuestions.QuestAutoID, tbAuditDetails.ScoreID, tbScore.Score, tbAuditQuestions.SubContentID, tbAuditDetails.ProfileID, tbAuditDetails.ScoreRanges, tbAuditDetails.Comments FROM (tbAuditDetails INNER JOIN tbAuditQuestions ON tbAuditDetails.QuestionID = tbAuditQuestions.QuestionID) INNER JOIN tbScore ON tbAuditDetails.ScoreID = tbScore.ScoreID WHERE (([tbAuditDetails.AuditNo] = " + Class1.detailsauditno + ") AND ([tbAuditQuestions.AutoSubcontent] = '" + newautosubcontentid + "') AND ([tbAuditDetails.ProfileID] = " + proid + ")) ORDER BY [tbAuditQuestions.QuestAutoID], [tbAuditDetails.QuestionID]";
Class1.Connection.Open();
oleCommand = new OleDbCommand(sql1, Class1.Connection);
oleAdapter = new OleDbDataAdapter(oleCommand);
oleBuilder = new OleDbCommandBuilder(oleAdapter);
oleDs = new DataSet();
oleAdapter.Fill(oleDs, "tbAuditDetails");
oleTable = oleDs.Tables["tbAuditDetails"];
Class1.Connection.Close();
dataGridView1.DataSource = oleDs.Tables["tbAuditDetails"];
//SET DATAGRIDVIEW
dataGridView1.Columns[0].Visible = false;
dataGridView1.Columns[4].Visible = false;
dataGridView1.Columns[5].Visible = false;
dataGridView1.Columns[6].Visible = false;
dataGridView1.Columns[7].Visible = false;
dataGridView1.Columns[8].Visible = false;
dataGridView1.Columns[9].Visible = false;
dataGridView1.Columns[10].Visible = false;
dataGridView1.Columns[1].HeaderText = " ";
dataGridView1.Columns[1].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
dataGridView1.Columns[1].HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight;
dataGridView1.Columns[1].ReadOnly = true;
dataGridView1.Columns[1].Width = 40;
dataGridView1.Columns[2].HeaderText = "ID";
dataGridView1.Columns[2].Width = 40;
dataGridView1.Columns[2].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
dataGridView1.Columns[2].ReadOnly = true;
dataGridView1.Columns[3].Width = 600;
dataGridView1.Columns[3].ReadOnly = true;
dataGridView1.Columns[3].DefaultCellStyle.WrapMode = DataGridViewTriState.True;
dataGridView1.Columns[11].HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
dataGridView1.Columns[11].HeaderText = "Score";
dataGridView1.Columns[12].Width = 220;
for (int iii = 0; iii < dataGridView1.Rows.Count; iii++)
{
DataGridViewComboBoxCell ComboBoxCell2 = new DataGridViewComboBoxCell();
ComboBoxCell2.Items.Add("0");
ComboBoxCell2.Items.Add("10");
ComboBoxCell2.Items.Add("20");
ComboBoxCell2.Items.Add("30");
ComboBoxCell2.Items.Add("40");
ComboBoxCell2.Items.Add("50");
ComboBoxCell2.Items.Add("60");
ComboBoxCell2.Items.Add("70");
ComboBoxCell2.Items.Add("80");
ComboBoxCell2.Items.Add("90");
ComboBoxCell2.Items.Add("100");
//ComboBoxCell.Items.AddRange(new string[] { "YES", "SOME", "NO", "N/A" });
ComboBoxCell2.FlatStyle = FlatStyle.Standard;
this.dataGridView1[11, iii] = ComboBoxCell2;
ComboBoxCell2.Dispose();
}
}
Any suggestions?
I don't know this is the right solution or not...
BUT, it fix my problem.
Here :
Right click on the .exe program (after you build) and Disable HIGH DPI
Here is my case: i can retrieved the data from the database, but when i run the program and filled up the Quantity column (on the third line), the error says: index was out of range. Anyone know why is this happen? (When i tried to fill the Quantity column (on third line), the error appeared) <-- shown in the picture below.
Here is the images of my program:
Here is the images of where the error is pointed:
Here is the full code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.Drawing;
using System.Linq;
using System.Security.Principal;
using System.Text;
using System.Windows.Forms;
namespace Sell_System
{
public partial class Form2 : Form
{
string connectionString = (#"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=\Archives\Projects\Program\Sell System\Sell System\App_Data\db1.accdb;Persist Security Info=False;");
private Form1 firstForm;
private List<List<TextBox>> textBoxCodeContainer = new List<List<TextBox>>();
private List<List<TextBox>> textBoxQuantityContainer = new List<List<TextBox>>();
private List<List<TextBox>> textBoxDescContainer = new List<List<TextBox>>();
private List<List<TextBox>> textBoxSubTotalContainer = new List<List<TextBox>>();
private List<List<TextBox>> textBoxTotalContainer = new List<List<TextBox>>();
private List<List<TextBox>> textBoxAllTotalContainer = new List<List<TextBox>>();
public Form2()
{
InitializeComponent();
}
public Form2(Form1 firstForm)
: this()
{
this.firstForm = firstForm;
}
private void Form2_Load(object sender, EventArgs e)
{
UpdateTextPosition();
OleDbDataReader dReader;
OleDbConnection conn = new OleDbConnection(connectionString);
conn.Open();
OleDbCommand cmd = new OleDbCommand("SELECT [Code] FROM [Data]", conn);
dReader = cmd.ExecuteReader();
AutoCompleteStringCollection codesCollection = new AutoCompleteStringCollection();
while (dReader.Read())
{
string numString = dReader[0].ToString().PadLeft(4, '0');
codesCollection.Add(numString);
}
dReader.Close();
conn.Close();
if (firstForm.comboBox1.SelectedIndex == 0)
{
label1.Text = "Code:";
label1.Location = new Point(60, 125);
label2.Text = "Welcome to the Selling System.";
label2.Location = new Point(600, 0);
label3.Text = "Quantity:";
label3.Location = new Point(155, 125);
label4.Text = "Description:";
label4.Location = new Point(580, 125);
label5.Text = "Sub Total on Rp:";
label5.Location = new Point(1020, 125);
label6.Text = "Total on Rp:";
label6.Location = new Point(1210, 125);
label7.Text = "Total on Rp:";
label7.Location = new Point(1080, 580);
}
else if (firstForm.comboBox1.SelectedIndex == 1)
{
label1.Text = "Kode:";
label1.Location = new Point(60, 125);
label2.Text = "Selamat datang di Selling System.";
label2.Location = new Point(600, 0);
label3.Text = "Banyaknya:";
label3.Location = new Point(145, 125);
label4.Text = "Keterangan:";
label4.Location = new Point(580, 125);
label5.Text = "Sub Total di Rp:";
label5.Location = new Point(1020, 125);
label6.Text = "Total di Rp:";
label6.Location = new Point(1210, 125);
label7.Text = "Total di Rp:";
label7.Location = new Point(1080, 580);
}
//****TextBox for Code****
for (int y = 0; y <= 16; y++)
{
textBoxCodeContainer.Add(new List<TextBox>());
textBoxCodeContainer[0].Add(new TextBox());
textBoxCodeContainer[0][y].Size = new Size(100, 50);
textBoxCodeContainer[0][y].Location = new Point(25, 150 + (y * 25));
textBoxCodeContainer[0][y].TextChanged += new System.EventHandler(this.textBox_TextChanged);
textBoxCodeContainer[0][y].AutoCompleteMode = AutoCompleteMode.Suggest;
textBoxCodeContainer[0][y].AutoCompleteSource = AutoCompleteSource.CustomSource;
textBoxCodeContainer[0][y].AutoCompleteCustomSource = codesCollection;
Controls.Add(textBoxCodeContainer[0][y]);
}
//****TextBox for Quantity****
for (int y = 0; y <= 16; y++)
{
textBoxQuantityContainer.Add(new List<TextBox>());
textBoxQuantityContainer[0].Add(new TextBox());
textBoxQuantityContainer[0][y].Size = new Size(100, 50);
textBoxQuantityContainer[0][y].Location = new Point(125, 150 + (y * 25));
textBoxQuantityContainer[0][y].TextChanged += new System.EventHandler(this.textBox_TextChanged);
Controls.Add(textBoxQuantityContainer[0][y]);
}
//****TextBox for Description****
for (int y = 0; y <= 16; y++)
{
textBoxDescContainer.Add(new List<TextBox>());
textBoxDescContainer[0].Add(new TextBox());
textBoxDescContainer[0][y].Size = new Size(750, 50);
textBoxDescContainer[0][y].Location = new Point(225, 150 + (y * 25));
Controls.Add(textBoxDescContainer[0][y]);
}
//****TextBox for Sub Total****
for (int y = 0; y <= 16; y++)
{
textBoxSubTotalContainer.Add(new List<TextBox>());
textBoxSubTotalContainer[0].Add(new TextBox());
textBoxSubTotalContainer[0][y].Size = new Size(175, 50);
textBoxSubTotalContainer[0][y].Location = new Point(975, 150 + (y * 25));
Controls.Add(textBoxSubTotalContainer[0][y]);
}
//****TextBox for Total****
for (int y = 0; y <= 16; y++)
{
textBoxTotalContainer.Add(new List<TextBox>());
textBoxTotalContainer[0].Add(new TextBox());
textBoxTotalContainer[0][y].Size = new Size(175, 50);
textBoxTotalContainer[0][y].Location = new Point(1150, 150 + (y * 25));
textBoxTotalContainer[0][y].TextChanged += new System.EventHandler(this.textBox_TextChanged);
Controls.Add(textBoxTotalContainer[0][y]);
}
//****TextBox for Total All****
textBoxAllTotalContainer.Size = new Size(175, 50);
textBoxAllTotalContainer.Location = new Point(1150, 575);
textBoxAllTotalContainer.TextChanged += new System.EventHandler(this.textBox_TextChanged);
Controls.Add(textBoxAllTotalContainer);
}
private void UpdateTextPosition()
{
Graphics g = this.CreateGraphics();
Double startingPoint = (this.Width / 2) - (g.MeasureString(this.Text.Trim(), this.Font).Width / 2);
Double widthOfASpace = g.MeasureString(" ", this.Font).Width;
String tmp = " ";
Double tmpWidth = 0;
while ((tmpWidth + widthOfASpace) < startingPoint)
{
tmp += " ";
tmpWidth += widthOfASpace;
}
this.Text = tmp + this.Text.Trim();
}
private void UpdateDatas()
{
int codeValue = 0;
int index = 0;
string query = "SELECT [Description], [Price] FROM [Data] WHERE [Code] IN (";
OleDbDataReader dReader;
OleDbConnection conn = new OleDbConnection(connectionString);
conn.Open();
if (int.TryParse(this.textBoxCodeContainer[0][0].Text, out codeValue))
{
query = query + codeValue.ToString();
}
if (int.TryParse(this.textBoxCodeContainer[0][1].Text, out codeValue))
{
query = query + "," + codeValue.ToString();
}
if (int.TryParse(this.textBoxCodeContainer[0][2].Text, out codeValue))
{
query = query + "," + codeValue.ToString();
}
if (int.TryParse(this.textBoxCodeContainer[0][3].Text, out codeValue))
{
query = query + "," + codeValue.ToString();
}
if (int.TryParse(this.textBoxCodeContainer[0][4].Text, out codeValue))
{
query = query + "," + codeValue.ToString();
}
if (int.TryParse(this.textBoxCodeContainer[0][5].Text, out codeValue))
{
query = query + "," + codeValue.ToString();
}
if (int.TryParse(this.textBoxCodeContainer[0][6].Text, out codeValue))
{
query = query + "," + codeValue.ToString();
}
if (int.TryParse(this.textBoxCodeContainer[0][7].Text, out codeValue))
{
query = query + "," + codeValue.ToString();
}
if (int.TryParse(this.textBoxCodeContainer[0][8].Text, out codeValue))
{
query = query + "," + codeValue.ToString();
}
if (int.TryParse(this.textBoxCodeContainer[0][9].Text, out codeValue))
{
query = query + "," + codeValue.ToString();
}
if (int.TryParse(this.textBoxCodeContainer[0][10].Text, out codeValue))
{
query = query + "," + codeValue.ToString();
}
if (int.TryParse(this.textBoxCodeContainer[0][11].Text, out codeValue))
{
query = query + "," + codeValue.ToString();
}
if (int.TryParse(this.textBoxCodeContainer[0][12].Text, out codeValue))
{
query = query + "," + codeValue.ToString();
}
if (int.TryParse(this.textBoxCodeContainer[0][13].Text, out codeValue))
{
query = query + "," + codeValue.ToString();
}
if (int.TryParse(this.textBoxCodeContainer[0][14].Text, out codeValue))
{
query = query + "," + codeValue.ToString();
}
if (int.TryParse(this.textBoxCodeContainer[0][15].Text, out codeValue))
{
query = query + "," + codeValue.ToString();
}
if (int.TryParse(this.textBoxCodeContainer[0][16].Text, out codeValue))
{
query = query + "," + codeValue.ToString();
}
query = query + ")";
OleDbCommand cmd = new OleDbCommand(query, conn);
cmd.Parameters.Add("Code", System.Data.OleDb.OleDbType.Integer);
dReader = cmd.ExecuteReader();
while (dReader.Read())
{
if (textBoxCodeContainer[0][index].TextLength != 0)
{
this.textBoxDescContainer[0][index].Text = dReader["Description"].ToString();
this.textBoxSubTotalContainer[0][index].Text = dReader["Price"].ToString();
}
index += 1;
}
dReader.Close();
conn.Close();
}
private void UpdatePrice()
{
if (textBoxQuantityContainer[0][0].Text == "")
{
textBoxTotalContainer[0][0].Text = "";
}
else if (textBoxQuantityContainer[0][0].Text == "1")
{
textBoxTotalContainer[0][0].Text = textBoxSubTotalContainer[0][0].Text;
textBoxAllTotalContainer.Text = textBoxTotalContainer[0][0].Text;
}
if (textBoxQuantityContainer[0][1].Text == "")
{
textBoxTotalContainer[0][1].Text = "";
}
else if (textBoxQuantityContainer[0][1].Text == "1")
{
textBoxTotalContainer[0][1].Text = textBoxSubTotalContainer[0][1].Text;
textBoxAllTotalContainer.Text = textBoxTotalContainer[0][1].Text;
}
if (textBoxQuantityContainer[0][2].Text == "")
{
textBoxTotalContainer[0][2].Text = "";
}
else if (textBoxQuantityContainer[0][2].Text == "1")
{
textBoxTotalContainer[0][2].Text = textBoxSubTotalContainer[0][2].Text;
textBoxAllTotalContainer.Text = textBoxTotalContainer[0][2].Text;
}
if (textBoxQuantityContainer[0][3].Text == "")
{
textBoxTotalContainer[0][3].Text = "";
}
else if (textBoxQuantityContainer[0][3].Text == "1")
{
textBoxTotalContainer[0][3].Text = textBoxSubTotalContainer[0][3].Text;
textBoxAllTotalContainer.Text = textBoxTotalContainer[0][3].Text;
}
if (textBoxQuantityContainer[0][4].Text == "")
{
textBoxTotalContainer[0][4].Text = "";
}
else if (textBoxQuantityContainer[0][4].Text == "1")
{
textBoxTotalContainer[0][4].Text = textBoxSubTotalContainer[0][4].Text;
textBoxAllTotalContainer.Text = textBoxTotalContainer[0][4].Text;
}
if (textBoxQuantityContainer[0][5].Text == "")
{
textBoxTotalContainer[0][5].Text = "";
}
else if (textBoxQuantityContainer[0][5].Text == "1")
{
textBoxTotalContainer[0][5].Text = textBoxSubTotalContainer[0][5].Text;
textBoxAllTotalContainer.Text = textBoxTotalContainer[0][5].Text;
}
if (textBoxQuantityContainer[0][6].Text == "")
{
textBoxTotalContainer[0][6].Text = "";
}
else if (textBoxQuantityContainer[0][6].Text == "1")
{
textBoxTotalContainer[0][6].Text = textBoxSubTotalContainer[0][6].Text;
textBoxAllTotalContainer.Text = textBoxTotalContainer[0][6].Text;
}
if (textBoxQuantityContainer[0][7].Text == "")
{
textBoxTotalContainer[0][7].Text = "";
}
else if (textBoxQuantityContainer[0][7].Text == "1")
{
textBoxTotalContainer[0][7].Text = textBoxSubTotalContainer[0][7].Text;
textBoxAllTotalContainer.Text = textBoxTotalContainer[0][7].Text;
}
if (textBoxQuantityContainer[0][8].Text == "")
{
textBoxTotalContainer[0][8].Text = "";
}
else if (textBoxQuantityContainer[0][8].Text == "1")
{
textBoxTotalContainer[0][8].Text = textBoxSubTotalContainer[0][8].Text;
textBoxAllTotalContainer.Text = textBoxTotalContainer[0][8].Text;
}
if (textBoxQuantityContainer[0][9].Text == "")
{
textBoxTotalContainer[0][9].Text = "";
}
else if (textBoxQuantityContainer[0][9].Text == "1")
{
textBoxTotalContainer[0][9].Text = textBoxSubTotalContainer[0][9].Text;
textBoxAllTotalContainer.Text = textBoxTotalContainer[0][9].Text;
}
if (textBoxQuantityContainer[0][10].Text == "")
{
textBoxTotalContainer[0][10].Text = "";
}
else if (textBoxQuantityContainer[0][10].Text == "1")
{
textBoxTotalContainer[0][10].Text = textBoxSubTotalContainer[0][10].Text;
textBoxAllTotalContainer.Text = textBoxTotalContainer[0][10].Text;
}
if (textBoxQuantityContainer[0][11].Text == "")
{
textBoxTotalContainer[0][11].Text = "";
}
else if (textBoxQuantityContainer[0][11].Text == "1")
{
textBoxTotalContainer[0][11].Text = textBoxSubTotalContainer[0][11].Text;
textBoxAllTotalContainer.Text = textBoxTotalContainer[0][11].Text;
}
if (textBoxQuantityContainer[0][12].Text == "")
{
textBoxTotalContainer[0][12].Text = "";
}
else if (textBoxQuantityContainer[0][12].Text == "1")
{
textBoxTotalContainer[0][12].Text = textBoxSubTotalContainer[0][12].Text;
textBoxAllTotalContainer.Text = textBoxTotalContainer[0][12].Text;
}
if (textBoxQuantityContainer[0][13].Text == "")
{
textBoxTotalContainer[0][13].Text = "";
}
else if (textBoxQuantityContainer[0][13].Text == "1")
{
textBoxTotalContainer[0][13].Text = textBoxSubTotalContainer[0][13].Text;
textBoxAllTotalContainer.Text = textBoxTotalContainer[0][13].Text;
}
if (textBoxQuantityContainer[0][14].Text == "")
{
textBoxTotalContainer[0][14].Text = "";
}
else if (textBoxQuantityContainer[0][14].Text == "1")
{
textBoxTotalContainer[0][14].Text = textBoxSubTotalContainer[0][14].Text;
textBoxAllTotalContainer.Text = textBoxTotalContainer[0][14].Text;
}
if (textBoxQuantityContainer[0][15].Text == "")
{
textBoxTotalContainer[0][15].Text = "";
}
else if (textBoxQuantityContainer[0][15].Text == "1")
{
textBoxTotalContainer[0][15].Text = textBoxSubTotalContainer[0][15].Text;
textBoxAllTotalContainer.Text = textBoxTotalContainer[0][15].Text;
}
if (textBoxQuantityContainer[0][16].Text == "")
{
textBoxTotalContainer[0][16].Text = "";
}
else if (textBoxQuantityContainer[0][16].Text == "1")
{
textBoxTotalContainer[0][16].Text = textBoxSubTotalContainer[0][16].Text;
textBoxAllTotalContainer.Text = textBoxTotalContainer[0][16].Text;
}
}
private void textBox_TextChanged(object sender, EventArgs e)
{
UpdateDatas();
UpdatePrice();
}
}
}
Here is my problem: "i am confuse, when i try assign a value for "textBoxAllTotalContainer.Text = textBoxTotalContainer[0][0].Text", the code is working. but, when i tried "textBoxAllTotalContainer.Text = textBoxTotalContainer[0][one(1)]", the screen stucked in there (I have to stop debugging from Visual Studio)"
textBoxTotalContainer contains 17 textboxes, therefore, i used [0][0] to [0][16], and textBoxAllTotalContainer contains 1 textbox, so i just used textBoxAllTotalContainer.Text
Note: "Keyword (one), it suppose to be "1" <-- number, i wrote (one), because when i tried changed the [0][(Here supposed to be "1")], the text automatically changed to linked image"
Issue: **"When i tried the code
else if (textBoxQuantityContainer[0][0].Text == "1")
{
textBoxTotalContainer[0][0].Text = textBoxSubTotalContainer[0][0].Text;
textBoxAllTotalContainer.Text = textBoxTotalContainer[0][0].Text;
}
the textbox for alltotalcontainer is on (Total on Rp, the textbox is on after words) in the image shown above, the textboxes for subtotal is on (Sub Total on Rp), the textboxes for totalcontainer is on (Total on Rp, the textboxes are on below words), the textboxes for quantitycontainer is on (Quantity). In code above, shown that textbox 1 on textboxtotalcontainer are same with the textbox 1 on subtotalcontainer, and alltotalcontainer is same with totalcontainer.**
But, below code it not working, and the screen stucked on there (the mouse dissappear and i cant do anything, unless i press SHIFT + F5 on Visual Studio):
else if (textBoxQuantityContainer[0][1].Text == "1")
{
textBoxTotalContainer[0][1].Text = textBoxSubTotalContainer[0][1].Text;
textBoxAllTotalContainer.Text = textBoxTotalContainer[0][1].Text;
}
Thanks
You get that error when you try to access an element in a collection that doesn't exist.
For example, if you have a list of strings...
List<string> myList = new List {"one", "two", "three"};
...and try to access the fourth element with myList[3], you'll get that error.
Either textBoxAllTotalContainer[0][2] doesn't exist, or textBoxTotalContainer[0][2]. Can't tell from the error in your picture. Place a breakpoint on that line and check both objects.
Looks like your stop condition is wrong.
you have the following code for initializing textBoxAllTotalContainer:
//****TextBox for Total All****
for (int y = 0; y <= 1; y++)
{
textBoxAllTotalContainer.Add(new List<TextBox>());
textBoxAllTotalContainer[0].Add(new TextBox());
textBoxAllTotalContainer[0][y].Size = new Size(175, 50);
textBoxAllTotalContainer[0][y].Location = new Point(1150, 575);
textBoxAllTotalContainer[0][y].TextChanged += new System.EventHandler(this.textBox_TextChanged);
Controls.Add(textBoxAllTotalContainer[0][y]);
}
Note that the loop is itterating only twice.
Therefore textBoxAllTotalContainer[0][0] and textBoxAllTotalContainer[0][1] are created.
but when you try to access textBoxAllTotalContainer[0][2] you hit the exception because the location is really out of the list's range as the exception specifies.
i want to add textboxes dynamically in C# on a button click in a table row. for that i have used the following code.
[1]: http://pastie.org/7702237.
the problem is i am able to adding as many textboxes as I want but they are adding at same location, i mean the table row is not incrementing for every button click instead it is simply replacing the old table row with the new one. Please help me in how to solve this problem.
Thanks in advance
Ganesh
Web-based applications do not maintain state; to this end the state of the table and any variables is not being maintained. With each postback (generated by the button), the table's state reverts to what it was prior to adding a row and then a row is programatically added to it.
In order to achieve your goal, you will need to maintain state somehow. In the following code snippet I am making use of a session:
private List<TableRow> TableRows
{
get
{
if(Session["TableRows"] == null)
Session["TableRows"] = new List<TableRow>();
return (List<TableRow>)Session["TableRows"];
}
}
The following is your code modified to work with the session variable:
TextBox txtE, txtM, txtB;
Button btnAdd, btnDel;
TableRow trow;
TableCell tcell;
foreach(TableRow tr in TableRows)
tblEduDetails.Controls.Add(tr);
int count = TableRows.Count + 1;
txtE = new TextBox();
txtE.ID = "E" + count.ToString();
txtE.Visible = true;
txtE.Text = "E " + count.ToString();
txtE.BorderWidth = 2;
txtE.TextMode = TextBoxMode.SingleLine;
txtE.Height = 30;
txtM = new TextBox();
txtM.ID = "M" + count.ToString();
txtM.Visible = true;
txtM.Text = "M " + count.ToString();
txtM.TextMode = TextBoxMode.SingleLine;
txtM.Height = 30;
txtB = new TextBox();
txtB.ID = "E" + count.ToString();
txtB.Visible = true;
txtB.Text = "B " + count.ToString();
txtB.TextMode = TextBoxMode.SingleLine;
txtB.Height = 30;
btnAdd = new Button();
btnAdd.ID = "A" + count.ToString();
btnDel = new Button();
btnDel.ID = "D" + count.ToString();
trow = new TableRow();
trow.ID = "R" + count.ToString();
trow.BorderWidth = 1;
tcell = new TableCell();
tcell.ID = "E" + count.ToString();
tcell.Controls.Add(txtE);
trow.Controls.Add(tcell);
tcell = new TableCell();
tcell.ID = "B" + count.ToString();
tcell.Controls.Add(txtM);
trow.Controls.Add(tcell);
tcell = new TableCell();
tcell.ID = "M" + count.ToString();
tcell.Controls.Add(txtB);
trow.Controls.Add(tcell);
tblEduDetails.Controls.Add(trow);
TableRows.Add(trow);
In the below code Radio buttons are created dynamically, But I want to create the select index change event of КadioИuttons created dynamically and handle it.....below is the code not firing the event.
DataSet ds = SqlHelper.ExecuteDataset(sCon, "Ps_Quiz_QuestionsWithOptions_Get",sQuestions);
if (ds.Tables.Count > 0 ?? ds.Tables[0].Rows.Count > 0)
{
int iCnt = 0;
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
Table tblQsn = new Table();
//.....Begin Text Qsn Creation.....//
tblQsn.Width = 500;
TableRow trQsn = new TableRow();
iRowCounter++;
trQsn.ID = "Row_" + iRowCounter.ToString();
TableCell tcQsn = new TableCell();
TableCell tcQsnSNo = new TableCell();
tcQsn.CssClass = "Label";
tcQsn.BackColor = System.Drawing.Color.Gainsboro;
tcQsn.Font.Bold = true;
tcQsn.Text = ds.Tables[0].Rows[i][1].ToString();
tcQsn.Width = Unit.Percentage(99.5);
iCellCounter++;
tcQsn.ID = "Cell_" + iCellCounter.ToString();
tcQsnSNo.CssClass = "Label";
tcQsnSNo.Attributes.Add("valign", "top");
tcQsnSNo.BackColor = System.Drawing.Color.Gainsboro;
tcQsnSNo.Font.Bold = true;
tcQsnSNo.Width = Unit.Percentage(0.5);
iCellCounter++;
tcQsnSNo.ID = "Cell_" + iCellCounter.ToString();
iCnt++;
tcQsnSNo.Text = iCnt.ToString() + ".";
trQsn.Cells.Add(tcQsnSNo);
trQsn.Cells.Add(tcQsn);
tblQsn.Rows.Add(trQsn);
int rcnt = i;
int iOptCnt = 0;
string sStatus = "N";
while ((rcnt >= 0) && (rcnt < ds.Tables[0].Rows.Count))
{
if (ds.Tables[0].Rows[rcnt][2].ToString() == ds.Tables[0].Rows[i][2].ToString())
{
if (sStatus == "N")
{
sStatus = "Y";
}
TableRow trQsnOpt = new TableRow();
iRowCounter++;
trQsnOpt.ID = "Row_" + iRowCounter.ToString();
TableCell tcQsnOpt = new TableCell();
tcQsnOpt.CssClass = "Label";
iCellCounter++;
tcQsnOpt.ID = "Cell_" + iCellCounter.ToString();
tcQsnOpt.Attributes.Add("valign", "top");
tcQsnOpt.VerticalAlign = VerticalAlign.Middle;
TableCell tcQsnOptSNo = new TableCell();
tcQsnOptSNo.CssClass = "Label";
iCellCounter++;
tcQsnOptSNo.ID = "Cell_" + iCellCounter.ToString();
iOptCnt++;
RadioButton oRbOptions = new RadioButton();
oRbOptions.GroupName = ds.Tables[0].Rows[rcnt][2].ToString() + "_Group";
oRbOptions.Text = ds.Tables[0].Rows[rcnt][3].ToString().Trim();
iRbTCounter++;
oRbOptions.ID = ds.Tables[0].Rows[i][0].ToString() + "_" + ds.Tables[0].Rows[rcnt][2].ToString() + "_" + "Option" + iOptCnt.ToString() + "_" + iRbTCounter.ToString();
oRbOptions.Enabled = true;
if (ds.Tables[0].Rows[i][2].ToString() == "Option" + iRbTCounter.ToString())
{
oRbOptions.Checked = true;
}
oRbOptions.CssClass = "Label";
tcQsnOpt.Controls.Add(oRbOptions);
oRbOptions.CheckedChanged += new System.EventHandler(CheckedChanged);
oRbOptions.AutoPostBack = false;
tcQsnOptSNo.Text = iOptCnt.ToString() + ".";
trQsnOpt.Cells.Add(tcQsnOptSNo);
trQsnOpt.Cells.Add(tcQsnOpt);
rcnt++;
//.....Add Option Image.....//
tblQsn.Rows.Add(trQsnOpt);
}
else
break;
}
i = rcnt - 1;
PlPreview.Controls.Add(tblQsn);
}
}
Simple attach an eventhandler for checked change event
oRbOptions.CheckedChanged += (s,e) => {
oRbOptions.AutoPostBack = true;
}
You are setting AutoPostBack is false for radio button .So,
please change false to true
oRbOptions.AutoPostBack = true;