I am unable to get the ID of dynamically generated textboxes. Hence I cant get their values. Posting my code below . Please help .
protected void txt_Freq_SelectedIndexChanged(object sender, EventArgs e)
{
try
{
int rem = Convert.ToInt16(ddl_freq.SelectedItem.Value.ToString());
Table tbl1 = new Table();
form1.Controls.Add(tbl1);
for (i = 1; i <= rem; i++)
{
TableRow row = new TableRow();
TableCell cell = new TableCell();
TextBox tb = new TextBox();
tb.ID = "rem" + i.ToString();
Label lb = new Label();
CalendarExtender calex = new CalendarExtender();
calex.ID = "calexRem" + i.ToString();
calex.TargetControlID = "rem" + i.ToString();
DropDownList txt_Time_rem = new DropDownList();
txt_Time_rem.ID = "ddl" + i.ToString();
for (int hr = 0; hr <= 23; hr++)
{
for (int min = 0; min < 60; min += 15)
{
if (hr <= 9 && min == 0)
txt_Time_rem.Items.Add(new ListItem('0' + hr.ToString() + ':' + '0' + min.ToString()));
else if (hr <= 9 && min != 0)
txt_Time_rem.Items.Add(new ListItem('0' + hr.ToString() + ':' + min.ToString()));
else if (hr > 9 && min == 0)
txt_Time_rem.Items.Add(new ListItem(hr.ToString() + ':' + '0' + min.ToString()));
else
txt_Time_rem.Items.Add(new ListItem(hr.ToString() + ':' + min.ToString()));
}
}
txt_Time_rem.Items.Insert(0, new ListItem("Time"));
lb.Text = "Reminder " + i.ToString();
cell.Controls.Add(lb);
cell.Controls.Add(tb);
cell.Controls.Add(calex);
cell.Controls.Add(txt_Time_rem);
row.Cells.Add(cell);
tbl1.Rows.Add(row);
Response.Write(tb.ID);
}
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
}
protected void btntemp_Click(object sender, EventArgs e)
{
TextBox tb = new TextBox();
for(x=1;x<=Convert.ToInt16(ddl_freq.SelectedItem.Value.ToString());x++)
{
TextBox txtUserName = form1.FindControl("rem"+x.ToString()) as TextBox;
if (txtUserName != null)
{
Response.Write("ID found!");
}
else
{
Response.Write("Failed");
}
I have tried replacing form1 with Page.Master as I have a Master Page but nothing works. It will print "Failed". Please Help.
Related
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);
}
I am trying to insert values from dynamic textbox to database using Entity Framework in Web Forms.
On one click it make a table row with three column and every column has one textbox. Every another click is one row more.
public static int rowCnt = 0;
protected void BtnAddNewBuildItem_Click(object sender, EventArgs e)
{
// Current row count.
int rowCtr;
// Total number of cells per row (columns).
int cellCtr;
// Current cell counter
int cellCnt;
rowCnt = rowCnt + 1;
cellCnt = 3;
for (rowCtr = 1; rowCtr <= rowCnt; rowCtr++)
{
// Create new row and add it to the table.
TableRow tRow = new TableRow();
TableBuildItems.Rows.Add(tRow);
for (cellCtr = 1; cellCtr <= cellCnt; cellCtr++)
{
// Create a new cell and add it to the row.
TableCell tCell = new TableCell();
TextBox tb = new TextBox();
// Set a unique ID for each TextBox added
tb.ID = "txtBuisniesItem_Row" + rowCtr + "Cell" + cellCtr;
// Add the control to the TableCell
tCell.Controls.Add(tb);
tRow.Cells.Add(tCell);
}
}
}
Then, on another button click i tried to insert one row values to database, but allways is empty string.
using (VODOMONTEntities context = new VODOMONTEntities())
{
BuildItem bi = new BuildItem();
for (int i = 0; i < rowCnt; i++)
{
TextBox tb1 = new TextBox();
tb1.ID = "txtBuisniesItem_Row" + (i + 1).ToString() + "Cell" + 1;
bi.Name = tb1.Text; //there is a empty string allways
context.BuildItems.Add(bi);
}
context.SaveChanges();
}
So, my question is how to insert a dynamic texbox values to database, like in this case?
Thank you!
protected void CreateNewBuild_Click(object sender, EventArgs e)
{
using (VODOMONTEntities context = new VODOMONTEntities())
{
Build b = new Build();
BuildItem bi = new BuildItem();
b.BuildSubject = txbSubject.Text;
b.BuildNumber = txbBuildNumber.Text;
b.City = txbCityBuild.Text;
// b.BuildDate = txbBuildDate.Text;
b.ClientId = Int32.Parse(ddlClient.SelectedValue);
context.Builds.Add(b);
for (int i = 0; i < (RowIndexNumber-1)/3; i++)
{
TextBox[] tb = new TextBox[RowIndexNumber];
tb[i*3] = new TextBox();
tb[i*3] = (TextBox)FindControl("txtBuisniesItem_Row" + (i * 3 + 1).ToString());
bi.Name = tb[i*3].Text;
tb[i*3+1] = new TextBox();
tb[i*3+1] = (TextBox)FindControl("txtBuisniesItem_Row" + (i * 3 + 2).ToString());
bi.Quantity = Convert.ToInt32(tb[i * 3 + 1].Text);
tb[i*3+2] = new TextBox();
tb[i*3+2] = (TextBox)FindControl("txtBuisniesItem_Row" + (i * 3 + 3).ToString());
bi.Price = decimal.Parse(tb[i * 3 + 2].Text.ToString());
bi.Build = b;
context.BuildItems.Add(bi);
context.SaveChanges();
}
}
}
public int RowIndexNumber = 1;
protected void Page_PreInit(object sender, EventArgs e)
{
List<string> keys = Request.Form.AllKeys.Where(key => key.Contains("txtBuisniesItem_Row")).ToList();
foreach (string key in keys)
{
this.CreateTextBox("txtBuisniesItem_Row" + RowIndexNumber);
if (RowIndexNumber % 3 == 0)
{
Literal lt = new Literal();
lt.Text = "<br />";
pnlTextBoxes.Controls.Add(lt);
}
RowIndexNumber++;
}
}
protected void BtnAddNewBuildItem_Click(object sender, EventArgs e)
{
int index = pnlTextBoxes.Controls.OfType<TextBox>().ToList().Count + 1;
this.CreateTextBox("txtBuisniesItem_Row" + index);
this.CreateTextBox("txtBuisniesItem_Row" + (index + 1).ToString());
this.CreateTextBox("txtBuisniesItem_Row" + (index + 2).ToString());
Literal lt = new Literal();
lt.Text = "<br />";
pnlTextBoxes.Controls.Add(lt);
}
private void CreateTextBox(string id)
{
TextBox txt = new TextBox();
txt.ID = id;
txt.Attributes.Add("runat", "Server");
pnlTextBoxes.Controls.Add(txt);
}
I am working on a project.I am creating an asp table dynamically and in table cell I am adding link button depending on condition.But while adding the Click event to link button it is giving an error saying-
System.Web.UI.WebControls.LinkButton.OnClick(System.EventArgs)' is inaccessible due to its protection level
Following is my code of making the table
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
setmonthname();
}
makeCalendar();
}
public void makeCalendar()
{
tblcalendar.Rows.Clear();
//for current month
DateTime startingdate = StartDateOfMonth(DateTime.Now.AddMonths(monthclickedno));
DateTime enddate = EndDateOfMonth(DateTime.Now.AddMonths(monthclickedno));
string startingday = startingdate.DayOfWeek.ToString();
int startingdayno = Convert.ToInt32(startingdate.DayOfWeek);
string endday = enddate.DayOfWeek.ToString();//like saturday is 6,stating is from monday with 1 and ending si sunday with 7
int enddayno = Convert.ToInt32(enddate.DayOfWeek);
//for prevoius month
DateTime enddateprevious = (EndDateOfMonth(DateTime.Now.AddMonths(monthclickedno)));
//for next month
DateTime startingdatenext = StartDateOfMonth(DateTime.Now.AddMonths(1));
DateTime dtstart=startingdate.AddDays(-(startingdayno+1));
//sMonthName = "January";
//int iMonthNo = Convert.ToDateTime("01-" + sMonthName + "-2011").Month;
for (int i = 0; i <7;i++)
{
TableRow tr = new TableRow();
for (int j = 0; j < 7;j++ )
{
TableCell tc = new TableCell();
clickablecell ctCell = new clickablecell();
//tc.ID = idtc.ToString();
idtc++;
if(i==0)
{
tr.CssClass = "firstrow";
tc.CssClass = "firstrowcell";
if (j == 0)
tc.Text = "Sun";
else if (j == 1)
tc.Text = "Mon";
else if (j == 2)
tc.Text = "Tue";
else if (j == 3)
tc.Text = "Wed";
else if (j == 4)
tc.Text = "Thu";
else if (j == 5)
tc.Text = "Fri";
else if (j == 6)
tc.Text = "Sat";
tr.Cells.Add(tc);
}
else{
tc.CssClass = "othercells";
dtstart=dtstart.AddDays(1);
//if date is single digit like 1,2
if (dtstart.ToString("dd").Substring(0, (dtstart.ToString("dd").Length)-1) == "0")
ctCell.Text = (dtstart.ToString("dd").Substring(1));
else
ctCell.Text = (dtstart.ToString("dd"));
ctCell.Attributes.Add("onmouseover", "defColor=this.style.backgroundColor; this.style.backgroundColor='LightGray';");
ctCell.Attributes.Add("onmouseout", "this.style.backgroundColor=defColor;");
//ctCell.ID = k.ToString();
k++;
ctCell.Click += new clickablecell.ClickEventHandler(textcell_Click);
//check for events in this date
DataTable dtevents = checkEvents(dtstart.ToString("dd-MM-yyyy"));
if (dtevents.Rows.Count != 0)
{
LinkButton lnkevent = new LinkButton();
if (dtevents.Rows.Count == 1)
{
if (dtevents.Rows[0]["eventtype"].ToString() == "Holiday")
{
lnkevent.Text = dtevents.Rows[0]["eventtype"].ToString();
lnkevent.CssClass = "tcholidaytext";
ctCell.CssClass = "tcholidaytext";
}
else if (dtevents.Rows[0]["eventtype"].ToString() == "Event")
{
lnkevent.Text = dtevents.Rows[0]["eventtype"].ToString();
lnkevent.CssClass = "tceventtext";
ctCell.CssClass = "tceventtext";
}
else
{
lnkevent.Text = dtevents.Rows[0]["eventtype"].ToString();
lnkevent.CssClass = "tcimpdaytext";
ctCell.CssClass = "tcimpdaytext";
}
}
else
{
ctCell.CssClass = "tcmixtext";
}
//lnkevent.Attributes.Add("onClick", "test();");
lnkevent.OnClick += new EventHandler(this,test);
ctCell.Controls.Add(lnkevent);
}
tr.Cells.Add(ctCell);
}
tblcalendar.Rows.Add(tr);
}
}
}
public void test(object sender,EventArgs e)
{
Response.Write("helloo");
}
Please help how I can solve this problem
Correct subscription should look like this:
lnkevent.Click += test;
OnClick is a method used internally inside the class to raise the event. On the contrary you should be subscribing to the event itself.
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
I am new to programming, working in asp.net.
I made a table with iteration, and here is my code:
int row2 = Convert.ToInt32(Request.QueryString["coll"]);
Session.Add("sumColl", row2);
TableRow tr2;
TableCell tc2;
TextBox tb2;
RequiredFieldValidator rfv;
for (int j = 0; j < row2; j++)
{
tr2 = new TableRow();
tc2 = new TableCell();
tc2.Controls.Add(new LiteralControl((j + 1).ToString()));
tr2.Cells.Add(tc2);
tc2 = new TableCell();
tb2 = new TextBox();
tb2.ID = "name" + (j + 1).ToString();
btn = new Button();
btn.ID = "search" + (j + 1).ToString();
btn.Text = "Search";
btn.OnClientClick = "javascript:openChild('popupA.aspx?dest=" + tb2.ID + "','win2')";
btn.CausesValidation = false;
rfv = new RequiredFieldValidator();
rfv.ID = "rfvR" + (j + 1).ToString();
rfv.ControlToValidate = tb2.ID;
rfv.Display = ValidatorDisplay.Dynamic;
rfv.ErrorMessage = "**";
tc2.Controls.Add(tb2);
tc2.Controls.Add(btn);
tc2.Controls.Add(rfv);
tr2.Cells.Add(tc2);
tblA.Rows.Add(tr2);
}
I want to get values from the database, where the input came from the textbox in the table above.
After I get the values I want to sum them.
The problem is I don't know how to get the input from iteration above.
I did something like below code.
int sumsColl = Convert.ToInt32(Session["sumColl"]);
double sum_coll = 0;
for (int j = 0; j < sumsColl; j++)
{
TextBox tb2 = new TextBox();
tb2 = this.Page.FindControl("name" + (j + 1).ToString()) as TextBox;
if (tb2 != null)
{
string sqlCol2 = "select p from somewhere where nameColl = '" + tb2.Text + "'";
DataTable dtcol2 = db.setDataTableSQL(sqlCol2);
for (int i = 0; i < j; i++)
{
Session.Add("P" + (j + 1).ToString(), dtcol2.Rows[0]["p"]);
}
sum_coll = sum_coll + Convert.ToDouble(Session["P"+(j + 1).ToString()]);
}
}
if (sum_coll < value)
{
Msg.Text = "Error . " + Msg.Text;
valid = false;
}
I know this is a mess, please help me and let me know if you need another clue.
I don't know what you are trying to do. Try DataGrid or DataGridView, ListView controls to display data. Your code seems very complicated. There are controls out there that specifically displays tabular data.