ASP.NET visible/invisible Rows in Table(C#)
ASP.NET visible/invisible Rows in Table(C#)
I have a System.Web.UI.WebControls.Table with five ROWs with five cells with server controls. In my "Page_load" => CreateChildControls() only Row1 is visible, other rows are invisible.
I want where I click on btnAdd_Click add in my table row by row and where I click on btnDell_Click delete last visible row in my table.... How to do this?
This is my UI in PageLoad:
This is my UI when click on BtnAdd:
This is my code for buttons but not workig :
void btnAdd_Click(object sender, EventArgs e) { if (tRow1.Visible) { tRow2.Visible = true; tRow3.Visible = false; tRow4.Visible = false; tRow5.Visible = false; } if (tRow1.Visible && tRow2.Visible && !tRow3.Visible) { tRow3.Visible = true; tRow4.Visible = false; tRow5.Visible = false; } } void btnDell_Click(object sender, EventArgs e) { }
Answer by user1515791 for ASP.NET visible/invisible Rows in Table(C#)
Although it is not actually deleting and adding on the backend (you always have a 5 row table).
DataRow[] rows = new DataRow[]{tRow1, tRow2.....};//im not sure how you refferenced them; void btnAdd_Click(object sender, EventArgs e) { foreach(DataRow dr in rows) { if(!dr.Visible) { dr.Visible = true; return; } } } void btnAdd_Click(object sender, EventArgs e) { foreach(DataRow dr in rows.Reverse()) { if(dr.Visible) { dr.Visible = false; return; } } }
Something like that?
Answer by Fafsi for ASP.NET visible/invisible Rows in Table(C#)
Much better approach is to have the row added dynamically. For your question, you can try simplifying your "if" conditions in your btnAdd_Click by using foreach statement instead. (assuming your Table id is "Table1").
foreach (TableRow row in Table1.Rows.Cast().Where(row => !row.Visible)) { row.Visible = true; return; }
For your delete you try below if will work, code below will search the last visible row and make it's property false to hide it.
if (Table1.Rows.Cast().Count(row => row.Visible) > 1) { Table1.Rows.Cast().Last(row => row.Visible).Visible = false; }
For your next question on How to clear Textbox, You can do so like what you are doing to access each cell textbox when retrieving each value and instead of getting the value you just need to clear the value.
Or simply iterate for each control in each cell and get all the text box per cell. once you have the control you wish you can then do what ever you want. To do this you can search each cell for controls with typeOf(TextBox).
first put the table row in a variable inside your if condition replacing your code to hide the row.:
var tableRow = Table1.Rows.Cast().Last(row => row.Visible);
then you can do another foreach loop in each cell to have access to the cell:
foreach (TableCell item in tableRow.Cells) { foreach (Control cntrl in item.Controls.Cast().Where(cntrl => cntrl.GetType() == typeof (TextBox))) { ((TextBox) cntrl).Text = string.Empty; } } tableRow.Visible = false; //Dont forget to hide the row before you exit
You can try above if works. If works you can simplify the foreach by combining the 2 foreach function into single foreach loop.. that will be your assignment. Learn how to use linq and Im pretty sure you'll gonna love it :-)
Answer by Gohyu for ASP.NET visible/invisible Rows in Table(C#)
void btnAdd_Click(object sender, EventArgs e) { if (tRow1.Visible && !tRow2.Visible) { tRow2.Visible = true; return; } else if (tRow1.Visible && tRow2.Visible && !tRow3.Visible) { tRow3.Visible = true; return; } else if (tRow1.Visible && tRow2.Visible && tRow3.Visible && !tRow4.Visible) { tRow4.Visible = true; return; } else if (tRow1.Visible && tRow2.Visible && tRow3.Visible && tRow4.Visible && !tRow5.Visible) { tRow5.Visible = true; return; } }
With this working :) How to delete last visible row with btnDell ? Use foreach or ?
Answer by Zaidi Riyadh for ASP.NET visible/invisible Rows in Table(C#)
This is shorter but i don't know if it will work for you.
void btnAdd_Click(object sender, EventArgs e) { i=1 while ( i<5 ) { if (tRow[i].Visible & !tRow[i+1].Visible) { tRow[i+1].Visible = true; Break; } else i++; }
}
Answer by Gohyu for ASP.NET visible/invisible Rows in Table(C#)
When I hide last a row in table I want to clear last row data... How to do this in IF statement?
if (Table1.Rows.Cast().Count(row => row.Visible) > 1) { Table1.Rows.Cast().Last(row => row.Visible).Visible = false; }
Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 72
0 comments:
Post a Comment