المساعد الشخصي الرقمي

مشاهدة النسخة كاملة : Dynamically Add Rows to html table from code behind [modified]



C# Programming
03-18-2010, 05:02 AM
Hi Guys!
I am in a bit of a Dilemma here and need some help. I have a table that has a row and three columns which are showed to the user after the page loads. There is a button which calls a code behind function to add a new row. The first time the user clicks, the row is added properly, no problem. The second time however, the row is not added but the previous row that was added is updated with the new id/name etc. Also the second time, the drop down menu gives an error which is quiet ridiculous, something to do with accessing innerHtml of the drop down menu when I am not even asking the compiler to assign anything of that sort. Please have a look and let me know where the problem is which my brain cells are failing to recognise.
Note: I have tried all the different methods to add the row for example table.Rows.Insert(index,htmlRow) etc.
Thanks is Advance.

Below id my code for reference:

=============================
Front End [HTML - Table only]
=============================



Loan Type
Amount Owing
Monthly Repayments






Please Select
Home Loan
Investment Loan
Credit Card
Personal Loan
Store Cards
Tax Debt
Other














======================
Code Behind [.cs File]
======================
protected void iBtnAddRow_Click(object sender, ImageClickEventArgs e)
{
int iIndex = Convert.ToInt32(Session["iKeyIndex"]);
iIndex = iIndex+1;

//Instantiate and create new items
HtmlTableRow rTableRow = new HtmlTableRow();
HtmlTableCell cTableCellLoanTypeDropDown = new HtmlTableCell();
HtmlTableCell cTableCellBalance = new HtmlTableCell();
HtmlTableCell cTableCellMonthlyRepayments = new HtmlTableCell();
HtmlSelect hSelect = new HtmlSelect();
TextBox txtBalance = new TextBox();
TextBox txtRepayments = new TextBox();

try
{
///******************** First we create the drop down menu *******************************///
hSelect.Attributes.Add("name", "loanType" + iIndex);
hSelect.Attributes.Add("id", "loanType" + iIndex);
hSelect.Attributes.Add("class", "txtNormalDrp");
hSelect.Attributes.Add("runat", "server");
//Add the selection items to hSelect
ListItem lst = new ListItem("Please Select", "");
lst.Attributes.Add("Selected", "Selected");
hSelect.Items.Add(lst);
ListItem lst1 = new ListItem("Home Loan", "Home Loan");
hSelect.Items.Add(lst1);
ListItem lst2 = new ListItem("Investment Loan", "Investment Loan");
hSelect.Items.Add(lst2);
ListItem lst3 = new ListItem("Credit Card", "Credit Card");
hSelect.Items.Add(lst3);
ListItem lst4 = new ListItem("Personal Loan", "Personal Loan");
hSelect.Items.Add(lst4);
ListItem lst5 = new ListItem("Store Cards", "Store Cards");
hSelect.Items.Add(lst5);
ListItem lst6 = new ListItem("Tax Debt", "Tax Debt");
hSelect.Items.Add(lst6);
ListItem lst7 = new ListItem("Other", "Other");
hSelect.Items.Add(lst7);
//Assign the drop down to the first cell
//cTableCellLoanTypeDropDown.Controls.Add(hSelect);

///**************************************************************************************///
///*************** Second we create the Text box for Balance*****************************///

txtBalance.Attributes.Add("class", "txtBalrepayments");
txtBalance.Attributes.Add("name", "balance" + iIndex);
txtBalance.Attributes.Add("id", "balance" + iIndex);
txtBalance.Attributes.Add("onfocusout", "fcnAddTotals();");
txtBalance.Attributes.Add("onkeypress", "return numbersonly(event, false)");
//Assign the textbox to the second column
cTableCellBalance.Controls.Add(txtBalance);

///**************************************************************************************///
///*************** Second we create the Text box for Monthly Repayments******************///

txtRepayments.Attributes.Add("class", "txtBalrepayments");
txtRepayments.Attributes.Add("name", "mRepayments" + iIndex);
txtRepayments.Attributes.Add("id", "mRepayments" + iIndex);
txtRepayments.Attributes.Add("onfocusout", "fcnAddTotals();");
txtRepayments.Attributes.Add("onkeypress", "return numbersonly(event, false)");
//Assign the textbox to the third column
cTableCellMonthlyRepayments.Controls.Add(txtRepayments);

///**************************************************************************************///
///************************************ FINALLY *****************************************///
//Add the Row to the table
//tblLoanTypes.Rows.Insert((tblLoanTypes.Rows.Count+1), rTableRow);
tblLoanTypes.Rows.Add(rTableRow);

//Add the cells and rows to the table: tblLoanTypes
rTableRow.Cells.Add(cTableCellLoanTypeDropDown);
rTableRow.Cells.Add(cTableCellBalance);
rTableRow.Cells.Add(cTableCellMonthlyRepayments);

//Increase the index and save it to the session variable
Session["iKeyIndex"] = iIndex;
}
catch (Exception sError)
{
//Functions._sendEmail(Error generating Rows", System.Configuration.ConfigurationManager.AppSettings["sWebAdminEmail"].ToString(),
// "An error has occured generating Dynamic Rows on Error: " + sError.Message);
}
finally
{
//Flush all the declarations
hSelect.Dispose();
txtBalance.Dispose();
txtRepayments.Dispose();
cTableCellLoanTypeDropDown.Dispose();
cTableCellBalance.Dispose();
cTableCellMonthlyRepayments.Dispose();
rTableRow.Dispose();
}
}



P.S: Sorry but my previous post caused some issue to the forum. I appologise for any inconvenience caused.
Thanksmodified on Wednesday, March 17, 2010 3:16 PM