Format dynamically created Radio buttons in Columns in c# - c#

I am creating radio buttons programatically for each element in an array/list and place them in a windows form.
Right now every new radio button is place under the previous one. How can i manage to start a new column after e.g. 4/5 radio buttons? The new column should appear on the right side of the previous radio-buttons.
This is my code so far:
for (int i = 0; i < startShapes.Count; i++)
{
RadioButton rdb = new RadioButton();
rdb.Text = startShapes.Values.ElementAt(i).Equals("") ? startShapes.Keys.ElementAt(i) : startShapes.Values.ElementAt(i);
rdb.Size = new Size(100, 30);
this.Controls.Add(rdb);
rdb.Location = new Point(45, 70 + 35 * i);
rdb.CheckedChanged += (s, ee) =>
{
var r = s as RadioButton;
if (r.Checked)
this.selectedString = r.Text;
};
}

How about using TableLayoutPanel?
Dictionary<string, string> startShapes = new Dictionary<string, string>();
for(int i=0;i<20;i++)
startShapes.Add("Shape " +i, "Shape " +i);
int row = 0;
int col = 0;
tableLayoutPanel1.RowStyles.Clear();
tableLayoutPanel1.ColumnStyles.Clear();
tableLayoutPanel1.ColumnStyles.Add(new ColumnStyle(SizeType.AutoSize));
tableLayoutPanel1.RowCount= 0;
tableLayoutPanel1.ColumnCount = 1;
foreach (var kvp in startShapes)
{
tableLayoutPanel1.RowStyles.Add(new RowStyle(SizeType.AutoSize));
tableLayoutPanel1.RowCount++;
RadioButton rdb = new RadioButton();
rdb.Text = string.IsNullOrEmpty(kvp.Value) ? kvp.Key : kvp.Value;
rdb.Size = new Size(100, 30);
rdb.CheckedChanged += (s, ee) =>
{
var r = s as RadioButton;
if (r.Checked)
this.selectedString = r.Text;
};
tableLayoutPanel1.Controls.Add(rdb, col, row);
row++;
if (row == 5)
{
col++;
row = 0;
tableLayoutPanel1.ColumnStyles.Add(new ColumnStyle(SizeType.AutoSize));
tableLayoutPanel1.ColumnCount++;
}
}
And if you really need your solution:
int left = 45;
int idx = 0;
for (int i = 0; i < startShapes.Count; i++)
{
RadioButton rdb = new RadioButton();
rdb.Text = startShapes.Values.ElementAt(i).Equals("") ? startShapes.Keys.ElementAt(i) : startShapes.Values.ElementAt(i);
rdb.Size = new Size(100, 30);
this.Controls.Add(rdb);
rdb.Location = new Point(left, 70 + 35 * idx++);
if (idx == 5)
{
idx = 0; // reset row
left += rdb.Width + 5; // move to next column
}
rdb.CheckedChanged += (s, ee) =>
{
var r = s as RadioButton;
if (r.Checked)
this.selectedString = r.Text;
};
}

Related

how to create dynamic button in c# windows application

i have two layutflowpanel
1-flowpanel_category
2-flowpanel_products
when i use nested for loop inside button click to get products from current select category another flowpanel_prd not showing that products from categorey maybe i have some mistake in the some point
i set inside InitialComponents this code to retrive all items from category to put it inside flowpanel_category
//get all category and set it all to flowpanel_cat
FLOWPANEL_CAT.Controls.Clear();
try
{
DT = clsgetcat.get_prd_categories();
for (int i = 0; i <= DT.Rows.Count; i++)
{
Extendedbutton Eb = new Extendedbutton();//with Extendedbutton this time
Eb.prdid = DT.Rows[i][0].ToString();//this asigns product_id to extended txtprdid
Eb._prdnme = DT.Rows[i][1].ToString();//this asigns product_nme to extended txtprdnme
Eb._myval = DT.Rows[i][1].ToString();//this asigns products to extended buttondescription
Eb.Name = DT.Rows[i][1].ToString();
Eb.Text = DT.Rows[i][1].ToString();
Eb.ForeColor = Color.White;
Eb.BackColor = Color.SkyBlue;
Eb.Font = new Font("Serif", 10, FontStyle.Regular);
Eb.Width = 100;
Eb.Height = 60;
Eb.TextAlign = ContentAlignment.MiddleCenter;
Eb.Margin = new Padding(5);
Button b = addbutton(i);
Eb.Click += new System.EventHandler(this.buttonclick);
FLOWPANEL_CAT.Controls.Add(Eb);
}
}
catch
{
return;
}
iset inside buttonclick this code to show each items from category inside flowpanel_prodcuts
public void buttonclick(object sender, EventArgs e)
{
FLOWPANEL_PRD.Controls.Clear();
for (int i = 0; i <= DT.Rows.Count; i++)
{
try
{
for (int j = 0; j <= i; j++)
{
Extendedbutton Eb = new Extendedbutton();//with Extendedbutton this time
Eb._prdid = DT.Columns[0].ToString();
string prdid = ((Extendedbutton)sender)._prdid;
Eb._prdnme = DT.Columns[1].ToString();
string prdnme = ((Extendedbutton)sender)._prdnme;
Eb.Name = DT.Rows[j][1].ToString();
Eb.Text = DT.Rows[j][1].ToString();
Eb.ForeColor = Color.White;
Eb.BackColor = Color.SkyBlue;
Eb.Font = new Font("Serif", 10, FontStyle.Regular);
Eb.Width = 100;
Eb.Height = 60;
Eb.TextAlign = ContentAlignment.MiddleCenter;
Eb.Margin = new Padding(5);
Button b = addbutton(j);
Eb.Click += new System.EventHandler(this.buttonclick);
txttotalamount.Clear();
txtprdqty.Clear();
txtprdqty.Focus();
FLOWPANEL_PRD.Controls.Add(Eb);
}
}
catch
{
return;
}
}
txtprdqty.Focus();
}
Button addbutton(int i)
{
Extendedbutton EB = new Extendedbutton();
EB.Name = EB._myval;
EB.Text = EB._myval;
EB.ForeColor = Color.White;
EB.BackColor = Color.SkyBlue;
EB.Font = new Font("Serif", 10, FontStyle.Regular);
EB.Width = 100;
EB.Height = 60;
EB.TextAlign = ContentAlignment.MiddleCenter;
EB.Margin = new Padding(5);
return EB;
}
this picture show you output result but i have some mistake when i get products from each category

Creating variables from dynamically created Text boxes and labels using C#

I am wanting to use to names assigned using .Name to input from text boxes then calculate and output to the labels.
Not sure how I can refer to these inputs as they currently have names but no assigned variables.
Sorry if I don't make much sense I am not a very proficient coder.
public partial class Form1 : Form
{
List<TextBox> Txt1 = new List<TextBox>();
public Form1()
{
InitializeComponent();
for (int i = 0; i < 4; i++)
{
for(int j = 0; j < 7; j++)
{
if (j == 0)
{
var txtbox = new TextBox();
txtbox.Location = new Point(163 + (i * 220), (36));
txtbox.Name = i + "Names";
txtbox.Text = txtbox.Name;
txtbox.Width = 40;
this.Controls.Add(txtbox);
}
else if (j>0 && j<6)
{
var extratxt = new TextBox();
extratxt.Location = new Point(163 + (i * 220), (36+ 36 * j));
extratxt.Name = i + "Input" + j;
extratxt.Text = extratxt.Name;
extratxt.Width = 70;
this.Controls.Add(extratxt);
var percentbox = new Label();
percentbox.Location = new Point(163 + (90+ i * 220), (36 + 36 * j));
percentbox.Name = i + "Percent" + j;
percentbox.Text = percentbox.Name;
percentbox.Width = 50;
this.Controls.Add(percentbox);
var gradebox = new Label();
gradebox.Location = new Point(163 + (150 + i * 220), (36 + 36 * j));
gradebox.Name = i + "Grade" + j;
gradebox.Text = gradebox.Name;
gradebox.Width = 50;
this.Controls.Add(gradebox);
}
else
{
var totals = new Label();
totals.Location = new Point(163 + (i * 220), (36 + 36 * j));
totals.Name = i + "Total";
totals.Text = totals.Name;
totals.Width = 40;
this.Controls.Add(totals);
...
}
...
}
}
}
}
Hope I understand you correctly.
Basically, your difficulty is how to access the instance stored in list of the dynamically create controls by their name, right?
Well, you can use .FirstOrDefault() to select instance with a specific property. For example,
private void Form1_Load(object sender, EventArgs e)
{
List<TextBox> tbList = new List<TextBox>();
for (int i = 0; i < 3; i++)
{
TextBox tb = new TextBox();
tb.Text = "Test" + i.ToString();
tb.Name = "TextBox" + (i + 1).ToString();
tb.Location = new Point(0, 25 * i);
tb.Tag = i;
tbList.Add(tb);
this.Controls.Add(tb);
}
var tb2 = tbList.FirstOrDefault(tb => tb.Name == "TextBox2");
if (tb2 != null)
tb2.Text = "Modified text";
var sum = tbList.Sum(tb => (int)tb.Tag);
}

Cannot fire runtime radiobutton checkedchanged event inside linkbutton click event

I am trying to fire radiobutton checked changed event on linkbutton click event but instead it goes to the page load and the radiobutton checkedchanged event does not fires.
protected void Page_Load(object sender, EventArgs e)
{
string Query = "select Q101004,Q101005 from Q101 where Q101001<110000013";
DataTable dt = ExecuteDataset(Query).Tables[0];
ViewState["dt"] = dt;
Table t = new Table();
TableRow r = new TableRow();
t.Rows.Add(r);
TableCell c = new TableCell();
lnkbtn = new LinkButton();
r.Cells.Add(c);
lnkbtn.Text = "Click Here";
lnkbtn.Visible = true;
lnkbtn.CommandName = "Test";
lnkbtn.CommandArgument = "Hi";
lnkbtn.ID = "Hi";
PlaceHolder2.Controls.Add(lnkbtn);
for (int i = 0; i < dt.Rows.Count; i++)
{
rb = new RadioButton();
rb.AutoPostBack = true;
rb.ID = "m" +i;
rb.GroupName = "a";
rb.Text = dt.Rows[0][0].ToString();
CbxList = new CheckBoxList();
CbxList.ID = "Cbx"+i;
CbxList.Enabled = false;
CbxList.RepeatDirection = RepeatDirection.Horizontal;
CbxList.RepeatColumns = 2;
CbxList.CellPadding = 10;
CbxList.CellSpacing = 5;
CbxList.RepeatLayout = RepeatLayout.Table;
options = dt.Rows[0][1].ToString().Split('~');
PlaceHolder2.Controls.Add(new LiteralControl("<br/>"));
for (int j = 0; j < options.Length; j++)
{
CbxList.Items.Add(new ListItem(options[j], options[j]));
}
PlaceHolder2.Controls.Add(rb);
PlaceHolder2.Controls.Add(CbxList);
if (i ==0)
rb.CheckedChanged += new EventHandler(rb_CheckedChanged);
else
lnkbtn.Click += new EventHandler(lnkbtn_Click);
}
}
void lnkbtn_Click(object sender, EventArgs e)
{
DataTable dt = (DataTable)ViewState["dt"];
lnkbtn = (LinkButton)PlaceHolder2.FindControl("Hi");
string str=((LinkButton)sender).CommandArgument;
//lnkbtn.Enabled = true;
if (lnkbtn.ID == str)
{
rb = new RadioButton();
rb.AutoPostBack = true;
rb.ID = "m";
rb.GroupName = "a";
rb.Text = dt.Rows[0][0].ToString();
CbxList = new CheckBoxList();
CbxList.ID = "Cbx";
CbxList.Enabled = false;
CbxList.RepeatDirection = RepeatDirection.Horizontal;
CbxList.RepeatColumns = 2;
CbxList.CellPadding = 10;
CbxList.CellSpacing = 5;
CbxList.RepeatLayout = RepeatLayout.Table;
options = dt.Rows[0][1].ToString().Split('~');
PlaceHolder2.Controls.Add(new LiteralControl("<br/>"));
for (int i = 0; i < options.Length; i++)
{
CbxList.Items.Add(new ListItem(options[i], options[i]));
}
PlaceHolder2.Controls.Add(rb);
PlaceHolder2.Controls.Add(CbxList);
if (lnkbtn.CommandName == "Test")
{
rb.CheckedChanged += new EventHandler(rb_CheckedChanged);
}
}
}
Your code subscribes to the checked changed but does not invoke it.
If you want to do this, you could call the rb_CheckedChanged() method directly.
hope this helps....its working fine for me.
Just Modify your code with this code and You will be able to achieve your desired output
private void InitPage()
{
string a1, b,a2,b2;
_objSession = (ClsSession)Session["Login"];
ds = _objSession._DataSet;
dtAll = ds.Tables[3];
dr = dtAll.NewRow();
string str2 = (string)ViewState["str1"];
if (str2 != null)
{
string[] str3 = str2.Split('~');
a2 = str3[0];
b2 = str3[1];
}
else
{
a2 = "a0";
b2 = "b0";
}
str = (string)ViewState["str"];
if (str == null)
{
a1 = "a0";
b = "b";
}
else
{
string[] str1 = str.Split('~');
a1 = str1[0];
b = str1[1];
}
if (str==null)
{
for (int j = 0; j < dtAll.Rows.Count; j++)
{
Table t = new Table();
TableRow r = new TableRow();
t.Rows.Add(r);
TableCell c = new TableCell();
lnkbtn = new LinkButton();
r.Cells.Add(c);
lnkbtn.Text = (j + 1).ToString();
lnkbtn.Visible = true;
lnkbtn.CommandName = "Test";
lnkbtn.CommandArgument = "Hi" + j;
lnkbtn.ID = "Hi" + j;
lnkbtn.ForeColor = Color.Blue;
lnkbtn.Width = 30;
lnkbtn.Font.Bold = true;
lnkbtn.Font.Size = 14;
lnkbtn.Font.Underline = false;
lnkbtn.Click += new EventHandler(lnkbtn_Click);
c.Controls.Add(lnkbtn);
plcHdrLinkButton.Controls.Add(lnkbtn);
}
ViewState["a"] = 0;
}
if (str2 != null)
{
string[] str3 = str2.Split('~');
a2 = str3[0];
a1 = a2;
}
string resultString = Regex.Match(a1, #"\d+").Value;
int a = int.Parse(resultString);
ViewState["a"] = a;
plcHdrQuestion.Controls.Clear();
rb = new RadioButton();
rb.ID = "m" + a;
rb.AutoPostBack = true;
rb.GroupName = "a";
rb.Text = (a + 1) + "." + " " + dtAll.Rows[a][4].ToString();
CbxList = new CheckBoxList();
CbxList.ID = "Cbx" + a;
CbxList.Enabled = false;
CbxList.RepeatDirection = RepeatDirection.Horizontal;
CbxList.RepeatColumns = 2;
CbxList.CellPadding = 10;
CbxList.CellSpacing = 5;
CbxList.RepeatLayout = RepeatLayout.Table;
options = dtAll.Rows[a][5].ToString().Split('~');
plcHdrQuestion.Controls.Add(new LiteralControl("<br/>"));
for (int i = 0; i < options.Length; i++)
{
CbxList.Items.Add(new ListItem(options[i], options[i]));
}
plcHdrQuestion.Controls.Add(rb);
plcHdrQuestion.Controls.Add(CbxList);
rb.CheckedChanged += new EventHandler(lnkbtn_Click);
string st = (string)ViewState["str"];
ViewState["str1"] = st;
ViewState["str"] = null;
}
protected void lnkbtn_Click(object sender, EventArgs e)
{
Boolean _flag=true;
ds = _objSession._DataSet;
dt1 = ds.Tables[3];
dr = dt1.NewRow();
StringBuilder sb=new StringBuilder ();
for (int i = 0; i < dt1.Rows.Count; i++)
{
Cbx = (RadioButton)plcHdrQuestion.FindControl("m" + i);
Cbx1 = (CheckBoxList)plcHdrQuestion.FindControl("Cbx" + i);
if (Cbx != null)
{
if (Cbx.Checked == true)
{
Cbx1.Enabled = true;
_flag = false;
string st1 = (string)ViewState["st"];
string st="c";
if (st1 != null)
st = st1 + "~" + st;
ViewState["st"] = st;
st1 = (string)ViewState["st"];
sb.Append(st);
}
break;
}
}
int count=(sb.ToString().Count());
if (count>2)
{
_flag = true;
}
if ((lnkbtn.CommandName == "Test") && (_flag ==true))
{
for (int j = 0; j < dt1.Rows.Count; j++)
{
lnkbtn = (LinkButton)plcHdrLinkButton.FindControl("Hi" + j);
string str = ((LinkButton)sender).CommandArgument;
lnkbtn.Enabled = true;
if (lnkbtn.ID == str)
{
ViewState["str"] = str + "~" + lnkbtn.ID;
InitPage();
ViewState["st"] = null;
_flag = false;
break;
}
}
}
}
You need to read up on your ASP.NET Page Life Cycle - http://msdn.microsoft.com/en-us/library/ms178472(VS.100).aspx
Very roughly, the following events get called during page lifecycle.
Init -> Controls are created & wired up with event handlers
Load ViewState/ControlState -> Controls are reset to their previous state from the last round trip (including whether or not they need to fire their events)
Load -> Controls are loaded into the Page's Control Tree
Control Events (Clicks etc...) are executed.
The problem you have is that you are creating your control and wiring it up to fire dynamically in the 4th step there, which is too late in the page lifecycle.
On the next round trip, if someone does interact with that control, and the lifecycle starts over, that control won't exist by the time the page is preparing to execute commands.
You'll need to move the creation of your RadioButton to a much earlier stage in the pages creation. In your updated code, try moving your Page_Load code into the override oninit method instead.

How can I get ID's of Dynamically generated Controls?

Hiiii,
In the following image the table & controls in it are generated dynamically.
Table is created onclick of "create table" button using values from dropdownlist which has no. of rows as values.
How can I get particular “Fileupload” & “Upload” button’s ID.
On click of “upload” button only fileupload control in same row as of that button should be accessed & it should not loop through all the fileupload controls in the table.
In my code, when I click Upload button in lower rows(say 3rd row) then it uploads the file selected in upper fileupload controls( here 1st n 2nd row along with 3rd row ) also.
I don’t want this. I just want to upload file from filupload control in the same row of clicked button.
CODE :
public partial class stable : System.Web.UI.Page
{
private int tblRow;
private int tblCol = 9;
private int i, j;
private bool CTflag;
Table table = new Table();
TableRow row,rrow;
TableCell cell,rcell;
FileUpload fileUp;
Button UpLdButton;
Button btnCal;
TextBox tb;
Label tbr;
string cmdArg; // for passing filuploaders id with Command button
private string filename = "fileUpLoader";
private string tbRowId = "row";
private string tbColId = "col";
protected int Rows
{
get
{
return ViewState["Rows"] != null ? (int)ViewState["Rows"] : 0;
}
set
{
ViewState["Rows"] = tblRow;
}
}
// Columns property to hold the Columns in the ViewState
protected int Columns
{
get
{
return ViewState["Columns"] != null ? (int)ViewState["Columns"] : 0;
}
set
{
ViewState["Columns"] = tblCol;
}
}
protected void Page_Load(object sender, EventArgs e)
{
if (Page.IsPostBack)
{
if (CTflag == false)
{
this.Rows = tblRow;
this.Columns = tblCol;
CreateDynamicTable();
}
else
{
CTflag = true;
}
}
//LoadViewState(object this);
//CreateDynamicTable();
}
protected void Button1_Click(object sender, EventArgs e)
{
clrControls();
CreateDynamicTable();
}
protected void CreateDynamicTable()
{
tblRow = Convert.ToInt32(DropDownList1.SelectedValue);
//Creat the Table and Add it to the Page
if (CTflag == false)
{
table.Caption = "Challan Entry";
table.ID = "Challan Entry";
table.BackColor = System.Drawing.Color.BurlyWood;
Page.Form.Controls.Add(table);
// Now iterate through the table and add your controls
for (i = 0; i < 1; i++)
{
row = new TableRow();
row.BorderStyle = BorderStyle.Ridge;
for (j = 0; j <= tblCol; j++)
{
cell = new TableCell();
cell.BorderWidth = 5;
cell.BorderStyle = BorderStyle.Ridge;
cell.BorderColor = System.Drawing.Color.Azure;
for (j = 0; j <= tblCol; j++)
{
string[] Header = { "CC NO.", "DATE", "TotalAmt", "NoOfRecpt", "Energy", "New", "Theft", "Misc", "SelectFile", "Upload", "Status" };
Label lbl = new Label();
lbl.ID = "lblHeader" + j;
if (j == 8)
{
lbl.Width = 220;
}
else if (j == 9)
{
lbl.Width = 50;
}
else
{
lbl.Width = 100;
}
lbl.Text = Header[j];
// Add the control to the TableCell
cell.Controls.Add(lbl);
}
row.Cells.Add(cell);
}
// Add the TableRow to the Table
table.Rows.Add(row);
}
for (i = 0; i < tblRow; i++)
{
row = new TableRow();
row.ID = tbRowId + i;
row.BorderStyle = BorderStyle.Ridge;
for (j = 0; j <= tblCol; j++)
{
cell = new TableCell();
cell.ID = tbColId + i + j;
cell.BorderWidth = 5;
cell.BorderStyle = BorderStyle.Ridge;
cell.BorderColor = System.Drawing.Color.Azure;
for (j = 0; j <= 0; j++)
{
Label lbl = new Label();
lbl.ID = "lblCCRow" + i + "Col" + j;
lbl.Text = "CC NO. " + i + " ";
lbl.Width = 100;
// Add the control to the TableCell
cell.Controls.Add(lbl);
}
for (j = 1; j <= 1; j++)
{
Label lbl = new Label();
lbl.ID = "lblRow" + i + "Col" + j;
lbl.Width = 100;
lbl.Text = Convert.ToString(DateTime.Now.Day) + "/" + Convert.ToString(DateTime.Now.Month) + "/" + Convert.ToString(DateTime.Now.Year);
// Add the control to the TableCell
cell.Controls.Add(lbl);
}
for (j = 2; j <= 7; j++)
{
tb = new TextBox();
tb.Width = 100;
tb.ID = "txtBoxRow" + i + "Col" + j;
//txtbxNames[i,j] = Convert.ToString(tb.ID);
tb.Text = "0";
// Add the control to the TableCell
cell.Controls.Add(tb);
}
for (j = 8; j <= 8; j++)
{
fileUp = new FileUpload();
//m = i; n = j;
fileUp.ID = filename + i + j;
fileUp.Width = 220;
cell.Controls.Add(fileUp);
cmdArg = fileUp.ID;
UpLdButton = new Button();
UpLdButton.Width = 100;
UpLdButton.Text = "Upload" + i + j;
UpLdButton.ID = UpLdButton.Text;
UpLdButton.CommandArgument= cmdArg;
cell.Controls.Add(UpLdButton);
UpLdButton.Click += new EventHandler(UpLdButton_Click);
}
for (j = 9; j <= 9; j++)
{
Label lbl = new Label();
lbl.ID = "lblRow" + i + j;
lbl.Text = "[ Status ]";
lbl.Width = 100;
// Add the control to the TableCell
cell.Controls.Add(lbl);
}
row.Cells.Add(cell);
}
// Add the TableRow to the Table
table.Rows.Add(row);
} //outer for-loop end
for (i = 0; i < 1; i++)
{
rrow = new TableRow();
rrow.ID = "ResultRow";
rrow.BorderStyle = BorderStyle.Ridge;
for (j = 0; j <= tblCol; j++)
{
rcell = new TableCell();
rcell.ID = "resultCol" + j;
rcell.BorderWidth = 5;
rcell.BorderStyle = BorderStyle.Ridge;
rcell.BorderColor = System.Drawing.Color.Azure;
for (j = 0; j <= 0; j++)
{
Label lbl = new Label();
//lbl.ID = "lblCCRow" + i + "Col" + j;
lbl.Text = "<b>Total</b>";
lbl.Width = 100;
// Add the control to the TableCell
rcell.Controls.Add(lbl);
}
for (j = 1; j <= 1; j++)
{
Label lbl = new Label();
//lbl.ID = "lblRow" + i + "Col" + j;
lbl.Width = 100;
lbl.Text = Convert.ToString(DateTime.Now.Day) + "/" + Convert.ToString(DateTime.Now.Month) + "/" + Convert.ToString(DateTime.Now.Year);
// Add the control to the TableCell
rcell.Controls.Add(lbl);
}
for (j = 2; j <= 7; j++)
{
tbr = new Label();
tbr.Width = 100;
tbr.ID = "txtResult" +i+j;
tbr.Text = tbr.ID;
tbr.EnableTheming = true;
tbr.BackColor = System.Drawing.Color.White;
//txtResNames[i, j] = Convert.ToString(tbr.ID);
// Add the control to the TableCell
rcell.Controls.Add(tbr);
}
for (j = 8; j <= 8; j++)
{
btnCal = new Button();
btnCal.Width = 100;
btnCal.Text = "Calculate";
btnCal.ID = btnCal.Text;
rcell.Controls.Add(btnCal);
btnCal.Click += new EventHandler(btnCal_Click);
}
rrow.Cells.Add(rcell);
}
// Add the TableRow to the Table
table.Rows.Add(rrow);
}
//flag seetting
CTflag = true;
ViewState["dynamictable"] = true;
}
}
void btnCal_Click(object sender, EventArgs e)
{
TextBox tbres = new TextBox();
TextBox tbTemp = new TextBox();
double TotAmt = 0, NoofRect = 0, Energy = 0,New1 = 0, Theft = 0, Misc = 0;
for (int i = 0; i < tblRow; i++)
{
for (int j = 2; j <= 7; j++)
{
TextBox tb = (TextBox)FindControlRecursive(this, string.Format("txtBoxRow{0}Col{1}", i, j));
Label tbr = (Label)FindControlRecursive(this, string.Format("txtResult{0}{1}", 0, j));
switch (j)
{
case 2:
TotAmt += Convert.ToDouble(tb.Text);
Label1.Text = Convert.ToString(TotAmt);
tbr.Text = Convert.ToString(TotAmt);
break;
case 3:
NoofRect += Convert.ToDouble(tb.Text);
//Label1.Text = Convert.ToString(NoofRect);
tbr.Text = Convert.ToString(NoofRect);
break;
case 4:
Energy+= Convert.ToDouble(tb.Text);
//Label1.Text = Convert.ToString(TotAmt);
tbr.Text = Convert.ToString(Energy);
break;
case 5:
New1+= Convert.ToDouble(tb.Text);
//Label1.Text = Convert.ToString(TotAmt);
tbr.Text = Convert.ToString(New1);
break;
case 6:
Theft+= Convert.ToDouble(tb.Text);
//Label1.Text = Convert.ToString(TotAmt);
tbr.Text = Convert.ToString(Theft);
break;
case 7:
Misc+= Convert.ToDouble(tb.Text);
//Label1.Text = Convert.ToString(TotAmt);
tbr.Text = Convert.ToString(Misc);
break;
}
}
}
}
protected void clrControls()
{
Label1.Text = "";
for (int i = 0; i < tblRow; i++)
{
for (int j = 2; j <= 7; j++)
{
fileUp = (FileUpload)FindControlRecursive(this, string.Format("fileUpLoader{0}{1}", i, 8));
fileUp.Enabled = true;
Button btn = (Button)FindControlRecursive(this, string.Format("Upload{0}{1}", i, 8));
btn.Enabled=true;
TextBox tb = (TextBox)FindControlRecursive(this, string.Format("txtBoxRow{0}Col{1}", i, j));
tb.Text = "0";
Label tbr = (Label)FindControlRecursive(this, string.Format("txtResult{0}{1}", 0, j));
tbr.Text = "0";
Label statlbl = new Label();
statlbl = (Label)FindControlRecursive(this, string.Format("lblRow{0}{1}", i, 9));
statlbl.Text = "[status]";
}
}
for (i = 0; i < 1; i++)
{
for (j = 8; j <= 8; j++)
{
btnCal.Enabled = true;
}
}
}
protected override void LoadViewState(object earlierState)
{
base.LoadViewState(earlierState);
if (ViewState["dynamictable"] == null)
{
CreateDynamicTable();
}
}
void UpLdButton_Click(object sender, EventArgs e)
{
Button btnUpLD = sender as Button;
for (int i = 0; i < tblRow; i++)
{
Button tb = (Button)FindControlRecursive(this, string.Format("Upload{0}{1}", i, 8));
fileUp = (FileUpload)FindControlRecursive(this, string.Format("fileUpLoader{0}{1}", i, 8));
Label statlbl = new Label();
statlbl = (Label)FindControlRecursive(this, string.Format("lblRow{0}{1}", i, 9));
if (!fileUp.HasFile)
{
//statlbl.Text = "[status]";
continue;
}
else
{
UploadFile(fileUp,tb);
if (tb.Enabled == true && fileUp.Enabled == true)
{
statlbl.Text = "[status]";
}
else
{
statlbl.Text = "Uploaded";
}
}
}
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
}
public static Control FindControlRecursive(Control root, string id)
{
if (root.ID == id)
{
return root;
}
foreach (Control c in root.Controls)
{
Control t = FindControlRecursive(c, id);
if (t != null)
{
return t;
}
}
return null;
}
protected void UploadFile(FileUpload xyz, Button btn)
{
if (xyz.HasFile)
{
string extension = System.IO.Path.GetExtension(xyz.FileName);
if (extension == ".dat" || extension == ".B60")
{
if (File.Exists(Server.MapPath("~\\") + xyz.FileName))
{
Label1.Text = "File " + xyz.FileName + " Already Exists!";
}
else
{
xyz.PostedFile.SaveAs(Server.MapPath("~\\") + xyz.FileName);
Label1.Text = "The " + xyz.FileName + " Has been uploaded";
btn.Enabled = false;
xyz.Enabled = false;
}
}
else
{
Label1.Text = "* You can select only '.dat' & '.B60' type files";
btn.Enabled = true;
xyz.Enabled = true;
}
}
else
{
Label1.Text = "Select a file";
}
}
}
plz Help me Out !!!
Thanx in Advance...
why not store a Row ID in the command argument for the button and then when the button is click you can use this to loop through all the rows untill the id matchs the row you are looking for, for example
foreach (DateRow test in table.Rows)
if ((test.findcontrol(uploadbutton) as Button).commandArgument = (Sender as Button).commandArgumrnt
{
//do stuff here
}
else
{
\\ do nothing
}
Hope this helps

How to create list of checkboxes with scroll?

I'm trying this code:
arrList = new List<CheckBox>();
for (int j = 0; j < 20; j++)
{
CheckBox check = new CheckBox();
arrList.Add(check);
}
CheckBox[] cb = arrList.ToArray();
for (int i = 0; i < 20; i++)
{
cb[i].Text = "sometext";
cb[i].Location = new System.Drawing.Point(10, 15 + i * 20);
cb[i].BackColor = System.Drawing.Color.Silver;
cb[i].Name = "somename";
cb[i].Size = new System.Drawing.Size(59, 17);
cb[i].Checked = true;
groupBox1.Controls.Add(cb[i]);
}
How can I add a vertical scroll bar there? (maybe I should use something instead of groupbox?)
You could use a Panel control and set it's AutoScroll property to True

Categories