I have created dynamically a Grid control with two columns and populate its Rows using data from the database in first column and a textbox control in the second column. But the last two rows of grid gets overlapped.
The following is my code snippet.
if (comboBox1.SelectedIndex > 0)
{
// Create the Grid
Grid DynamicGrid = new Grid();
DynamicGrid.Width = 400;
DynamicGrid.HorizontalAlignment = HorizontalAlignment.Left;
DynamicGrid.VerticalAlignment = VerticalAlignment.Top;
DynamicGrid.ShowGridLines = true;
DynamicGrid.Background = new SolidColorBrush(Colors.LightSteelBlue);
//Adding columns to datagrid
ColumnDefinition gridCol1 = new ColumnDefinition();
ColumnDefinition gridCol2 = new ColumnDefinition();
DynamicGrid.ColumnDefinitions.Add(gridCol1);
DynamicGrid.ColumnDefinitions.Add(gridCol2);
// Add first column header
TextBlock txtBlock1 = new TextBlock();
txtBlock1.Text = "Particulars";
txtBlock1.FontSize = 14;
txtBlock1.FontWeight = FontWeights.Bold;
txtBlock1.Foreground = new SolidColorBrush(Colors.Green);
txtBlock1.VerticalAlignment = VerticalAlignment.Top;
Grid.SetRow(txtBlock1, 0);
Grid.SetColumn(txtBlock1, 0);
// Add second column header
TextBlock txtBlock2 = new TextBlock();
txtBlock2.Text = "Amount";
txtBlock2.FontSize = 14;
txtBlock2.FontWeight = FontWeights.Bold;
txtBlock2.Foreground = new SolidColorBrush(Colors.Green);
txtBlock2.VerticalAlignment = VerticalAlignment.Top;
Grid.SetRow(txtBlock2, 0);
Grid.SetColumn(txtBlock2, 1);
//// Add column headers to the Grid
DynamicGrid.Children.Add(txtBlock1);
DynamicGrid.Children.Add(txtBlock2);
RowDefinition gridRow;
TextBlock par;
TextBox amt;
int i = 1;
string pjt = comboBox1.SelectedItem.ToString();
//Display all the Particulars
SqlCeConnection con;
con = dbConnection();
SqlCeCommand cmd;
string sql = "select * from " + pjt;
try
{
cmd = new SqlCeCommand(sql, con);
SqlCeDataReader rdr = cmd.ExecuteReader();
int ordName = rdr.GetOrdinal("Name");
while (rdr.Read())
{
string nam = rdr.GetString(ordName);
gridRow = new RowDefinition();
gridRow.Height = new GridLength(45);
DynamicGrid.RowDefinitions.Add(gridRow);
par = new TextBlock();
par.Text = nam;
par.FontSize = 12;
par.FontWeight = FontWeights.Bold;
Grid.SetRow(par, i);
Grid.SetColumn(par, 0);
amt = new TextBox();
amt.Height = 20;
amt.Width = 190;
amt.Foreground = new SolidColorBrush(Colors.Black);
Grid.SetRow(amt, i);
Grid.SetColumn(amt, 1);
DynamicGrid.Children.Add(par);
DynamicGrid.Children.Add(amt);
i++;
}
}
catch (Exception ex)
{
MessageBox.Show("Error: " + ex);
}
this.Content = DynamicGrid;
}
I can't figure out why this overlapping occurs.
Please help me out.
I got the solution. I just added code to clear the children, rowDefinition and columndefinition of grid before initializing with values.
Here i the code I used.
grid1.Children.Clear();
grid1.RowDefinitions.Clear();
grid1.ColumnDefinitions.Clear();
//Adding columns to datagrid
ColumnDefinition gridCol1 = new ColumnDefinition();
ColumnDefinition gridCol2 = new ColumnDefinition();
grid1.ColumnDefinitions.Add(gridCol1);
grid1.ColumnDefinitions.Add(gridCol2);
// Add first column header
TextBlock txtBlock1 = new TextBlock();
txtBlock1.Text = "Particulars";
txtBlock1.FontSize = 14;
txtBlock1.FontWeight = FontWeights.Bold;
txtBlock1.Foreground = new SolidColorBrush(Colors.Green);
txtBlock1.VerticalAlignment = VerticalAlignment.Top;
Grid.SetRow(txtBlock1, 0);
Grid.SetColumn(txtBlock1, 0);
// Add second column header
TextBlock txtBlock2 = new TextBlock();
txtBlock2.Text = "Amount";
txtBlock2.FontSize = 14;
txtBlock2.FontWeight = FontWeights.Bold;
txtBlock2.Foreground = new SolidColorBrush(Colors.Green);
txtBlock2.VerticalAlignment = VerticalAlignment.Top;
Grid.SetRow(txtBlock2, 0);
Grid.SetColumn(txtBlock2, 1);
//// Add column headers to the Grid
grid1.Children.Add(txtBlock1);
grid1.Children.Add(txtBlock2);
RowDefinition gridRow = new RowDefinition();
grid1.RowDefinitions.Add(gridRow);
TextBlock par;
TextBox amt;
int i = 1;
string pjt = comboBox1.SelectedItem.ToString();
//Display all the Particulars
SqlCeConnection con;
con = dbConnection();
SqlCeCommand cmd;
string sql = "select * from " + pjt;
try
{
cmd = new SqlCeCommand(sql, con);
SqlCeDataReader rdr = cmd.ExecuteReader();
int ordName = rdr.GetOrdinal("Name");
while (rdr.Read())
{
string nam = rdr.GetString(ordName);
gridRow = new RowDefinition();
gridRow.Height = new GridLength(45);
grid1.RowDefinitions.Add(gridRow);
par = new TextBlock();
par.Text = nam;
par.FontSize = 12;
par.FontWeight = FontWeights.Bold;
Grid.SetRow(par, i);
Grid.SetColumn(par, 0);
amt = new TextBox();
amt.Height = 20;
amt.Width = 190;
amt.Foreground = new SolidColorBrush(Colors.Black);
Grid.SetRow(amt, i);
Grid.SetColumn(amt, 1);
grid1.Children.Add(par);
grid1.Children.Add(amt);
i++;
}
}
catch (Exception ex)
{
MessageBox.Show("Error: " + ex);
}
I placed a grid in the XAML file itself instead of creating dynamically. Then added rows and columns dynamically.
Thanks for the help.
Related
I have a problem about getting values from gridview that i created in code behind.
I created dynamic gridview. I filled DataSource from Database. These are my codes:
SqlCommand cmd = new SqlCommand("xxx", noz);
cmd.CommandType = CommandType.StoredProcedure;
noz.Open();
i = 1;
SqlDataReader rd = cmd.ExecuteReader();
if (rd.HasRows)
{
while (rd.Read())
{
string isim = "name" + i;
Label name4 = new Label();
name4.ID = isim;
name4.Text = rd3[1].ToString() + " için değerlendirme yapınız";
name4.Font.Bold = true;
ph.Controls.Add(name4);
noz2.Open();
SqlDataAdapter sda = new SqlDataAdapter("xxx", noz2);
sda.SelectCommand.CommandType = CommandType.StoredProcedure;
GridView gv = new GridView();
gv.ID = "_gridview" + i;
Queue q = new Queue();
q.Enqueue(i);
DataTable dt = new DataTable();
sda.Fill(dt);
dt.Columns.Add("Puanlar", typeof(string));
gv.DataSource = dt;
gv.DataBind();
ph.Controls.Add(gv); //ph is my placeholder//
gv.Height = 500;
gv.Width = 980;
i++;
noz2.Close();
foreach (GridViewRow r in gv.Rows)
{
TextBox ddl3Selection = new TextBox();
ddl3Selection.Width = 800;
r.Cells[2].Controls.Add(ddl3Selection);
}
}
noz.Close();
I have no problem so far. But when i want to use click button, i can't get gridviews's rows. I get rowcount 0.
for (int a = 1; a < i; a++)
{
GridView gv = new GridView();
gv.ID = "_gridview" + a;
Label lbl = new Label();
lbl.ID = "name" + a;
for (int n = 0; n < gv.Rows.Count; n++)
{
SqlCommand cmd = new SqlCommand("xxx", noz);
cmd.CommandType = CommandType.StoredProcedure;
GridViewRow row = gv.Rows[n];
int soruid =Convert.ToInt16(row.Cells[0].Text);
string cevap = row.Cells[2].Text;
string firmaadi = lbl.Text;
cmd.Parameters.AddWithValue("#Firma_Ad", firmaadi);
cmd.Parameters.AddWithValue("#Soru_Id", soruid);
cmd.Parameters.AddWithValue("#Soru_Cevap", cevap);
cmd.Parameters.AddWithValue("#Cevaplayan", "");
noz.Open();
int rd2 = cmd.ExecuteNonQuery();
noz.Close();
}
How can i get values from gridview?
there is two different grid. First grid have data source and the second is nothing.
I am working on a website that will act as a blog. My issue is with the comments section and it deals with the alignment of child comment panels. I try to add CSS to float the panels right, but they stay on the left. I have also tried setting the HorizontalAlign of the parent panels to right.
For more context, this is an image of the comments section:
As you can see, the child comments panels are sticking to the left.
This is the method where I am dynamically creating the comments section into an exiting panel called Panel1.
protected void drawComments(string ID, int NumTabs, Panel parentPanel) {
string hash = ID;
SqlConnection conn = new SqlConnection(Secret Stuff);
string cmdStr = "SELECT * FROM Comments WHERE ParentID=#searchHash";
SqlCommand cmd = new SqlCommand(cmdStr, conn);
cmd.Parameters.Add("#searchHash", SqlDbType.NVarChar).Value = hash;
try
{
conn.Open();
SqlDataReader reader = cmd.ExecuteReader();
Panel tempPanel;
if (reader.HasRows)
{
while (reader.Read())
{
ID = reader.GetString(4);
tempPanel = new Panel();
tempPanel.BorderStyle = BorderStyle.Solid;
tempPanel.BorderColor = System.Drawing.ColorTranslator.FromHtml("#2461bf");
tempPanel.Width = new Unit((100 - (NumTabs * 5)).ToString() + "%");
tempPanel.Attributes.Add("style", "margin-left:auto;");
tempPanel.Attributes.Add("style", "margin-right:auto;");
if (NumTabs > 0)
{
tempPanel.Attributes.Add("style", "margin-bottom:5px");
// tempPanel.Attributes.Add("style", "border-top-style:none");
tempPanel.Attributes.Add("style", "border-left-style:none");
// tempPanel.Attributes.Add("style", "border-right-style:solid");
tempPanel.Attributes.Add("style", "border-bottom-style:none");
}
else
{
tempPanel.Attributes.Add("style", "margin-top:50px");
// Panel1.Controls.Add(new LiteralControl("<BR />"));
}
Label currComment = new Label();
Label currAuthor = new Label();
currComment.Text = reader.GetString(0);
currAuthor.Text = reader.GetString(3).Split('#')[0];
Table tbl = new Table();
tbl.Width = new Unit("100%");
tbl.Attributes.Add("style", "margin-left:auto");
tbl.Attributes.Add("style", "margin-right:auto");
TableRow tblrow1 = new TableRow();
TableCell tblcell11 = new TableCell();
TableCell tblcell12 = new TableCell();
tblcell11.HorizontalAlign = HorizontalAlign.Right;
tblcell11.Width = new Unit("30%");
tblcell11.Text = currAuthor.Text + " Says:";
tblcell12.HorizontalAlign = HorizontalAlign.Left;
tblcell12.Text = currComment.Text;
tblcell12.Width = new Unit("70%");
tblrow1.Cells.Add(tblcell11);
tblrow1.Cells.Add(tblcell12);
tbl.Rows.Add(tblrow1);
TableCell tblcell21 = new TableCell();
tblcell21.Width = new Unit("30%");
ImageButton replyButton = new ImageButton();
replyButton.ImageUrl = "~/images/replybutton.png";
replyButton.Attributes.Add("style", "float:right");
replyButton.Width = 77;
replyButton.ID = ID;
replyButton.Command += addReply;
ImageButton likeButton = new ImageButton();
likeButton.ImageUrl = "~/images/likebutton.png";
likeButton.Attributes.Add("style", "float:right");
likeButton.Width = 65;
Label likeCount = new Label();
likeCount.Attributes.Add("style", "float:right");
likeCount.BorderStyle = BorderStyle.Groove;
likeCount.Text = "0";
TableCell tblcell22 = new TableCell();
tblcell22.HorizontalAlign = HorizontalAlign.Left;
tblcell22.Controls.Add(likeCount);
tblcell22.Controls.Add(likeButton);
tblcell22.Controls.Add(replyButton);
tblcell22.Width = new Unit("70%");
TableRow tblrow2 = new TableRow();
tblrow2.Cells.Add(tblcell21);
tblrow2.Cells.Add(tblcell22);
tbl.Rows.Add(tblrow2);
tempPanel.Controls.Add(tbl);
//tempPanel.Controls.Add(currComment);
if (NumTabs>0)
{
parentPanel.Controls.Add(tempPanel);
}
else
Panel1.Controls.Add(tempPanel);
drawComments(reader.GetString(4), NumTabs + 1, tempPanel);
}
}
}
catch (Exception ex)
{
lblDebug.Text = ex.ToString();
}
finally { conn.Close(); }
}
Any suggestions to get these child panels right aligning would be greatly appreciated. I hope you can understand what I am going for through this poorly written code by a 15 year old.
try
{
string constr1 = ConfigurationManager.ConnectionStrings["constring"].ConnectionString;
SqlConnection conn1 = new SqlConnection(constr1);
conn1.Open();
//passing a query to fetch the table from database,which is entered in TextBox
DataSet ds = new DataSet();
string s2 = "select neid,keyholder from tbl_controller_settings where axxesstype='2'";
SqlDataAdapter da = new SqlDataAdapter(s2, conn1);
da.Fill(ds);
DataTable dt = new DataTable();
dt = ds.Tables[0];
//creating a table dynamically
HtmlTable table = new HtmlTable();
HtmlTableRow tr = null;
HtmlTableCell tc = null;
//displaying labels for displaying column names in the table
tr = new HtmlTableRow();
for (int i = 0; i <=64; i++)
{
tc = new HtmlTableCell();
Label lbl = new Label();
if(i!=0)
lbl.Text = "Key"+" "+i.ToString();
lbl.ID = "lbl" +" "+i.ToString();
lbl.Style.Add("writing-mode", "rt-tb");
lbl.Style.Add("filter", "flipv");
tc.Height = "50px";
tc.Width = "150px";
tc.Controls.Add(lbl);
tr.Controls.Add(tc);
table.Controls.Add(tr);
}
//creating textboxes for displaying records information
for (int j = 0; j < dt.Rows.Count; j++)
{
tr = new HtmlTableRow();
tc = new HtmlTableCell();
Label ksid = new Label();
ksid.ID = "ksid"+j;
ksid.Text = dt.Rows[j][0].ToString();
tc.Controls.Add(ksid);
tr.Controls.Add(tc);
for (int k = 1; k <= 64;k++ )
{
tc = new HtmlTableCell();
CheckBox chk = new CheckBox();
chk.ID = "txt" + j + k;
chk.CheckedChanged += new EventHandler(CheckBox_CheckedChanged);
if (k <= Convert.ToInt32(dt.Rows[j][1]))
chk.Enabled = true;
else
chk.Enabled = false;
tc.Controls.Add(chk);
tr.Controls.Add(tc);
}
table.Controls.Add(tr);
}
pnlkeys.Controls.Add(table);
pnlkeys.Visible = true;
//}
}
catch (Exception ex)
{
throw;
}
Hello
In the Above code i have created the check box and a label button dynamically,in this 64 iteration i will be having the 64 check box.i want to get the value has when a check box is checked it should be 1 and when it is not checked it should be 0 to store in 64 columns ..please help me
try add the method in the same class file :
public static void CheckBox_CheckedChanged(object sender, EventArgs e) {
}
If you want help on how to generate the "1" when checked, please specify first where that value is to be stored.
You do not need OnCheckedChanged if you want to store their values altogether.
You can access your CheckBox's value in PostBack on Request.Form with their ID
if(Request.Form["txt11"]==null)
{
//checkbox is not checked
}
if(Request.Form["txt11"]=="on")
{
//checkbox is checked
}
You need to write this code in a loop to get all your CheckBoxes
I want to display a simple color on the background of the cells int the color column only?
how do I display the color instead of the color code atleast the background color of the cell itself? btw i'm using the fullrowselect..
my code for loading the database
SuspendLayout();
using (MySqlConnection conn = new MySqlConnection(myConnection))
{
//string cell = dataGridView3.CurrentCell.Value.ToString();
conn.Open();
string query = "SELECT productid,description,color,quantity,unitprice FROM deqor.tblproducts where category=?cat;";
using (MySqlCommand cmd = new MySqlCommand(query, conn))
{
cmd.Parameters.AddWithValue("?cat", comboBox1.SelectedItem.ToString());
try
{
sda = new MySqlDataAdapter();
sda.SelectCommand = cmd;
datset = new DataTable();
sda.Fill(datset);
bsource = new BindingSource();
bsource.DataSource = datset;
dataGridView1.DataSource = bsource;
DataGridViewColumn column = dataGridView1.Columns[0];
column.HeaderText = "Code";
column.Width = 160;
DataGridViewColumn column1 = dataGridView1.Columns[1];
column1.HeaderText = "Brand";
column1.Width = 220;
DataGridViewColumn column2 = dataGridView1.Columns[2];
column2.HeaderText = "Color";
column2.Width = 100;
DataGridViewColumn column3 = dataGridView1.Columns[3];
column3.HeaderText = "Quantity";
column3.Width = 50;
DataGridViewColumn column4 = dataGridView1.Columns[4];
column4.HeaderText = "Price";
column4.Width = 50;
sda.Update(datset);
if (dataGridView1.RowCount < 1)
{
datset.Clear();
string row = "NO items found";
datset.Rows.Add(row);
}
}
catch (Exception ex)
{
MessageBox.Show("" + ex);
}
}
conn.Close();
}
ResumeLayout();
You could set the color during the CellFormatting event, see here for an explanation
Example
private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
//Check if we're formatting the color column
if (this.dataGridView1.Columns[e.ColumnIndex].Name == "Color")
{
//Make sure there's a value set
if (e.Value != null)
{
string colorCode = (string)e.Value;
ColorConverter cc = new ColorConverter();
e.CellStyle.BackColor = (Color)cc.ConvertFromString("#" + colorCode);
//If you don't want the code to show
e.Value = "";
e.FormattingApplied = true;
}
}
}
you can use foreach for every cell in column[2] and fill it like that:
dataGridView1.Rows[count].DefaultCellStyle.BackColor = (Color)ColorConverter.ConvertFromString("#FFDFD991");
I am developing a shopping cart kind of thing. For that, on selecting products they get added to a temporary table, and so on. Then a popup window opens which loads dynamic labels, text-boxes and linkbutton based on the number of rows in the temporary table in the page load event. I want to allow users to change the textbox field which is the quantity of the product, and immediately update in the database using the dynamically created linkbutton.
I tried using Viewstate but I am unable to do it according to my need. Also , I read many other articles and posts on stack-overflow and other blogs too, but could not work out.
Here is my code:-
protected void Page_Load(object sender, EventArgs e)
{
Session["date"] = DateTime.Now.ToShortDateString();
productListing();
LabelCost.Text = total.ToString("F");
}
public void productListing()
{
//...................................Product Listing...............................
con2.Close();
con2.Open();
SqlDataAdapter adp12 = new SqlDataAdapter("select * from db_temporary_cart where(IP_address='" + Request.QueryString["ip"].ToString() + "' AND date='" + Session["date"] + "') ORDER BY counter DESC", con2);
DataSet ds12 = new DataSet();
adp12.Fill(ds12, "db_temporary_cart");
int count12 = ds12.Tables[0].Rows.Count;
if (count12 > 0)
{
totalProductRpwsincategory = count12;
//................storing products id of the category.....................
arr_product_id = new string[count12];
arr_product_name = new string[count12];
arr_product_qty = new int[count12];
arr_product_price = new double[count12];
arr_product_subtotal = new double[count12];
for (int i = 0; i < count12; i++)
{
arr_product_id[i] = ds12.Tables[0].Rows[i]["product_id"].ToString();
arr_product_qty[i] = Convert.ToInt32(ds12.Tables[0].Rows[i]["quantity"]);
}
con2.Close();
for (int i = 0; i < arr_product_id.Length; i++)
{
con2.Close();
con3.Close();
con3.Open();
SqlDataAdapter adp123 = new SqlDataAdapter("select * from db_product_management where(product_id='" + arr_product_id[i].ToString() + "' AND product_stock_availability='" + "In-Stock" + "')", con3);
DataSet ds123 = new DataSet();
adp123.Fill(ds123, "db_product_management");
int count123 = ds123.Tables[0].Rows.Count;
if (count123 > 0)
{
arr_product_name[i] = ds123.Tables[0].Rows[0]["product_name"].ToString();
arr_product_price[i] = Convert.ToDouble(ds123.Tables[0].Rows[0]["product_price"]);
arr_product_subtotal[i] = Convert.ToDouble((arr_product_price[i])*(arr_product_qty[i]));
total = total + arr_product_subtotal[i];
con3.Close();
}
con3.Close();
}
//................storing products id of the category.....................
Table table = new Table();
var collection = new List<string>();
for (int i = 0; i < totalProductRpwsincategory; i++)
{
TableRow row = new TableRow();
row.CssClass = "dynamicMain";
System.Web.UI.HtmlControls.HtmlGenericControl createDivMain = new System.Web.UI.HtmlControls.HtmlGenericControl("DIV");
createDivMain.Attributes["class"] = "dynamicMain";
ImageButton imgLink = new ImageButton();
imgLink.Attributes.Add("width", "150px");
imgLink.Attributes.Add("height", "104px");
imgLink.Attributes["class"] = "dynamicIMG";
imgLink.CommandArgument = arr_product_id[i];
imgLink.CommandName = arr_product_name[i];
imgLink.Command += new CommandEventHandler(this.OnDynamicLinkButtonClick);
System.Web.UI.HtmlControls.HtmlGenericControl createDiv1 = new System.Web.UI.HtmlControls.HtmlGenericControl("DIV");
createDiv1.Attributes["class"] = "sub-category-productsDisplayBlockText1_Dynamic";
Label linkProduct = new Label();
linkProduct.Text = arr_product_name[i];
LinkButton linkSaveQty = new LinkButton();
linkSaveQty.Text = arr_product_name[i];
linkSaveQty.Font.Underline = false;
linkSaveQty.ID = i.ToString();
linkSaveQty.Text = "Save";
linkSaveQty.CommandArgument = arr_product_id[i];
linkSaveQty.Attributes["class"] = "dynamicSaveQty";
linkSaveQty.Command += new CommandEventHandler(this.OnDynamicLinkButtonClickSave);
//linkSaveQty.CausesValidation = false;
System.Web.UI.HtmlControls.HtmlGenericControl createDivTb = new System.Web.UI.HtmlControls.HtmlGenericControl("DIV");
TextBox tb = new TextBox();
//tb.ID = arr_product_id[i] + Guid.NewGuid().ToString("N");
tb.ID = arr_product_id[i];
tb.Text = arr_product_qty[i].ToString();
tb.Attributes["class"] = "dynamicTB";
Label linkQty = new Label();
linkQty.Text = "Quantity";
linkQty.Attributes["class"] = "dynamicQty";
collection.Add(tb.ID);
System.Web.UI.HtmlControls.HtmlGenericControl createDiv3 = new System.Web.UI.HtmlControls.HtmlGenericControl("DIV");
createDiv3.Attributes["class"] = "sub-category-productsDisplayBlockText1_Dynamic_Price";
Label linkPrice = new Label();
linkPrice.Text = arr_product_price[i].ToString();
System.Web.UI.HtmlControls.HtmlGenericControl createDiv2 = new System.Web.UI.HtmlControls.HtmlGenericControl("DIV");
createDiv2.Attributes["class"] = "sub-category-productsDisplayBlockText1_Dynamic_SubTotal";
Label linkSubTotal = new Label();
linkSubTotal.Text = arr_product_subtotal[i].ToString();
ImageButton imgLinkDelete = new ImageButton();
imgLinkDelete.Attributes.Add("width", "25px");
imgLinkDelete.Attributes.Add("height", "25px");
imgLinkDelete.CommandArgument = arr_product_id[i];
imgLinkDelete.Command += new CommandEventHandler(this.OnDynamicLinkButtonClickDelete);
for (int j = 0; j < 1; j++)
{
imgLink.ImageUrl = "NImageMainProductImage.ashx?PhotoId=" + arr_product_id[i];
TableCell cell = new TableCell();
cell.Controls.Add(imgLink);
row.Cells.Add(cell);
cell.VerticalAlign = VerticalAlign.Top;
}
for (int j = 1; j < 2; j++)
{
TableCell cell = new TableCell();
cell.Controls.Add(createDiv1);
createDiv1.Controls.Add(linkProduct);
row.Cells.Add(cell);
cell.VerticalAlign = VerticalAlign.Top;
}
for (int j = 2; j < 3; j++)
{
TableCell cell = new TableCell();
cell.Controls.Add(linkQty);
cell.Controls.Add(new LiteralControl("<br>"));
cell.Controls.Add(tb);
cell.Controls.Add(new LiteralControl("<br>"));
cell.Controls.Add(linkSaveQty);
row.Cells.Add(cell);
cell.VerticalAlign = VerticalAlign.Top;
}
for (int j = 3; j < 4; j++)
{
TableCell cell = new TableCell();
cell.Controls.Add(createDiv3);
createDiv3.Controls.Add(linkPrice);
row.Cells.Add(cell);
cell.VerticalAlign = VerticalAlign.Top;
}
for (int j = 4; j < 5; j++)
{
TableCell cell = new TableCell();
cell.Controls.Add(createDiv2);
createDiv2.Controls.Add(linkSubTotal);
row.Cells.Add(cell);
cell.VerticalAlign = VerticalAlign.Top;
}
for (int j = 5; j < 6; j++)
{
imgLinkDelete.ImageUrl = "../../images/delete.png";
TableCell cell = new TableCell();
cell.Controls.Add(imgLinkDelete);
row.Cells.Add(cell);
cell.VerticalAlign = VerticalAlign.Top;
}
table.Rows.Add(row);
}
TextBoxIdCollection = collection;
PanelItemsCart.Controls.Add(table);
con2.Close();
}
con2.Close();
//...................................Product Listing...............................
}
This was my code for loading dynamic controls on page load event. Here is the Enumeration string and click event of the dynamically created link button, which is not working.
private List<string> TextBoxIdCollection
{
get
{
var collection = ViewState["TextBoxIdCollection"] as List<string>;
return collection ?? new List<string>();
}
set { ViewState["TextBoxIdCollection"] = value; }
}
void OnDynamicLinkButtonClickSave(object sender, CommandEventArgs e)
{
LinkButton clickedButton = (LinkButton)sender;
String commandArgument = clickedButton.CommandArgument;
int value = 0;
foreach (Control c in PanelItemsCart.Controls)
{
if (c.GetType() == typeof(TextBox) && c.ID == (commandArgument))
{
value = Convert.ToInt32(((TextBox)c).Text);
con4.Close();
con4.Open();
SqlDataAdapter adp = new SqlDataAdapter("select * from db_temporary_cart where((IP_address='" + Request.QueryString["ip"].ToString() + "' AND date='" + DateTime.Now.ToShortDateString() + "') AND (product_id='" + commandArgument + "'))", con4);
DataSet ds = new DataSet();
adp.Fill(ds, "db_temporary_cart");
int cd = ds.Tables[0].Rows.Count;
if (cd > 0)
{
int sendQty = value;
con4.Close();
con3.Close();
con3.Open();
SqlCommand cmd4 = new SqlCommand("UPDATE db_temporary_cart SET quantity='" + sendQty + "' where((IP_address='" + Request.QueryString["ip"].ToString() + "' AND date='" + DateTime.Now.ToShortDateString() + "') AND (product_id='" + commandArgument + "'))", con3);
cmd4.ExecuteNonQuery();
con3.Close();
}
con4.Close();
PanelItemsCart.Controls.Clear();
productListing();
}
}
}
Kindly, help me enter all the textboxes' and labels', which are dynamically created, values in the database on click event of dynamically created linkbuttons.