ASP:Table adding rows programmatically - c#

I am trying to add rows programmatically to a server side table in asp.net. I have a button, and within the click event handler I have:
TableRow row = new TableRow();
TableCell cell = new TableCell();
cell.Controls.Add(new TextBox());
row.Cells.Add(cell);
myServersideTable.Rows.AddAt(myServersideTable.Rows.Count - 1, row);
All good, the code works the first time the button is clicked and a new row is added.
However, all subsequent button clicks don't add a new row. No error is thrown.
Why this might be happening?

You are adding a row dynamically, so when you postback you will have to again recreate the controls.
In this case you are using a Table and the row is added on first button click, so when you click again you will have to recreate the previous row again and one more for the current click

You can't do this because of the stateless nature of Http protocol. For more information please read ASP.NET page life cycle from the MSDN.

Related

dataGridView hos go simulate a cell click

I have a dataGridView and I want to simulate that a cell that was clicked.
I want to do this using a button outside the table (a "Send button").
How can I access the row and column that was added?
I thought doing it with:
dataGridView_MyTable.Rows[rowIndex].Cells[index].Selected = true;
However, this didn't work.
I manage to slove it.
This is what i wrote:
Int32 GetNextRow = dataGridView_OverrideTable.CurrentCell.RowIndex;
dataGridView_OverrideTable_CellContentClick(dataGridView_OverrideTable, new DataGridViewCellEventArgs(SEND_COLUMN_INDEX_IN_GRID, GetNextRow));

ASP.NET GridView BackColor update on column sort

I have a GridView tied to a table in a SQL Database so that it loads onto a page after login to my website.
When the page with the GridView is first loaded. I step through the rows on the table and change the BackColor for each row depending on it's "Status" column (i.e. if it's "Incomplete" make it red or green if it's "Complete"). When you sort the table by another column then the BackColor goes away.
I attempt to run the same function to step through the rows and change all the BackColors on the GridView1_Sorted event but the table remains without any color changes. Same goes for using the GridView1_Load event. However adding a button in and tieing that button's click to the same formatting code lets me apply the BackColors after sorting.
This tells me I'm probably misunderstanding something in how the Sorted event works. Can anyone tell me how to get the GridView set up properly so that I can sort by some column and still re-apply my BackColor formatting?
Try something like this
If e.Row.RowType = DataControlRowType.DataRow Then
Dim stStatus As String = e.Row.Cells(1).Text ' INDEX OF YOUR STATUS COLUMN
For Each cell As TableCell In e.Row.Cells
If stStatus = "Incomplete" Then
cell.BackColor = Color.Red
Else If stStatus = "Complete" Then
cell.BackColor = Color.Green
End If
Next
End If
Put this code in the RowDataBound event of your GridView

c# DatagridView set NewRow State to Added

I have a DatagridView with the Option AllowUserToAddRows = True.
So my user can see the last blank line and if he inserts something there a new Row is generated.
I have some calculations to do when the user changes cell values for this i use the EndEdit Event. And I also use a Custom Contextmenu to delete rows from this Datagridview.
So now my Problem, if I Add a new Row (and Important) if I do not Change the Selected Row after creation, and then delete a Row through the Contextmenu both Rows, the newly created and the one i want to delete dissapear.
I see if i create a new Row this Row is Initial in Detached State and gets Added after i Change the selected Row.
So my Question, is there a way to create a Row and set the State to Added programmatically?
There is a setAdded Method in the DataRow but I couldnĀ“t call it when the Row is detached.
I have found the MSDN Article which says I had to Add the Row to a RowCollection then the Rowstate is Added, but in my Case (and Mind) the Row is allready in a RowCollection course i Add it to the datagridview....
I hope you understand my creepy english, if you need Sourcecode to understand my meaning just ask.
Best Regads
You could listen for the RowsAdded event and change one of the values on the new row in a handler (and change it back so the user does not notice). This should do the same as manually changing some values on your row.
If no values are changed on the new row, it's not added by default, otherwise each edit you would get a new blank row added to your collection.

How to get selected Row data on edit link click in gridview

I have a gridview with two extra buttons as Edit and Add on each row and I have a click event on each Edit and Add Linkbuttons through which I am opening a ModelViewExtender Dialog. I want when I am clicking on these linkbuttons on each row of gridview, all row data should accessed means Row Data of particular column (cells)from the row of clicked Edit link.
You can get better understanding thorough the below image of GridView, as:
Please suggest me any solution regarding the same.
Thanks in advance.
You have tow ways, either you use the AutoGenereateEdit property to true which will generate these hyperlinks. then on the row editing event you can get all these values easily by using the "e.NewEditIndex" which is the rowIndex of the GridViewRow your editing.
The second way is that you have put these hyperlinks "edit" and "add" as templates, in order to access them you need to parse the the sender object to it's control on the hyperlink click event and then get it's parent which will return the GridViewRow that the control on, which will allow you to get all the values you want from that row like this:
//Debug it and just make sure that tow parents return the GridViewRow
GridViewRow row = (GridViewRow)(((HyperLink)(sender)).Parent.Parent);

Retrieve data from dynamically created controls

I am dynamically creating several dropdown list on the selection changed event of a main (static) dropdown list. These are created in The TableCell of default Table. When submit button is clicked I need to load a new page with selected values of dropdown as parameters. Basically I need to get the dropdown results in the second page.
This is how dropboxes are created:
while (reader.Read())
{
pcID = int.Parse(reader["fk_pcID"].ToString());
pcDesc = GetpcDescription(pcID);
List<Product> prodList = GetProductsBypcID(pcID);
DropDownList ddList = new DropDownList();
ddList.ID = "ddlPC" + pcID;
foreach(Product prod in prodList)
{
ddList.Items.Add(new ListItem(prod.ProductName, prod.ProductID.ToString()));
}
TableCell cell1 = new TableCell();
cell1.Text = pcDesc;
TableCell cell2 = new TableCell();
cell2.Controls.Add(ddList);
TableRow row = new TableRow();
row.Cells.Add(cell1);
row.Cells.Add(cell2);
table.Rows.Add(row);
}
EDIT: The above code runs at the selection changed event of a dropdown which is set to runat server. But dynamically created dropdowns are not set to run at server
Are these drop-down boxes runat="server"? Meaning: did you created them programmaticly via a .net post-back event through the code behind? If so, just get the selected value the same as you would with any other control, and then pass it through a query string or a cookie.
Are these standard html drop-down lists? Good luck reading them from a code-behind. That gets you all tangled up in the viewstate, and while technically possible, is realistically not feasible.
If the second option is the case, it might be a much better option to have your button target a JavaScript function that gets the selected values and then passes them via querystring. Then on the other page you can read the querystrings from the code-behind, or on the client-side.
This sounds like a good candidate for the DynamicControlsPlaceholder. It will preserve your dynamic controls automatically, without requiring any additional code on the page. If you need to create the controls OnSelectedIndexChanged, I think this might be the simplest solution.
It's a free component, and you can download it here.
http://www.denisbauer.com/ASPNETControls/DynamicControlsPlaceholder.aspx

Categories