RowUpdating en gridview

[ASP.NET Web Forms] Adding fields to insert data with the footer row of a GridView

2019.01.30 09:28 lcyduh [ASP.NET Web Forms] Adding fields to insert data with the footer row of a GridView

Tried posting in /aspnet but apparently I'm not allowed to post there...
I've got a form where I'm adding a company record to my database.
Relevant to this company is a list of contact people that I want to add at the same time I add the company.
I wanted to do this by using a gridview to manipulate a list of the people that I'm storing in Session memory, then additions to the list would be done via text fields in the gridview footer.
I'm working from this example on Docs about inserting a new record from the gridview footer but I'm seeing strange behavior, particularly that the record isn't being added and that as soon as I hit the Insert LinkButton, the footer fields disappear.
Can anyone identify what I'm doing wrong because I'm basically lost at this point.
Here's my code:
       No data to show.
And here's the logic:
Since I'm adding the Insert button to the footer with a Command property, I'm hooking into RowCommand to do the work but this being the expected result, isn't actually what's happening as the breakpoint on the ItemsGrid_RowCommand event handler isn't even being hit.
I suspect I'm therefore just seeing postbacks and since I'm only binding the grid (and creating the footer controls) if the load is not a postback, that explains why the controls are disappearing.
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { BindGrid(); } } public void BindGrid() { if (Session["CompanyUsers"] == null) { var list = new List { new User { Cellphone = "0825551111", Email = "[email protected]", Fax = "", Name = " John Doe", Telephone = "0115551111" }, new User { Cellphone = "1234567890", Email = "[email protected]", Fax = "0116352462", Name = "Herp Derp", Telephone = "0123689348" } }; Session["CompanyUsers"] = list; } ItemsGrid.DataSource = Session["CompanyUsers"] as List; ItemsGrid.DataBind(); } protected void ItemsGrid_RowEditing(object sender, GridViewEditEventArgs e) { var ri = e.NewEditIndex; var txtName = ItemsGrid.FooterRow.FindControl("txtName") as TextBox; txtName.Text = ItemsGrid.Rows[ri].Cells[1].Text; var txtTelephone = ItemsGrid.FooterRow.FindControl("txtTelephone") as TextBox; txtTelephone.Text = ItemsGrid.Rows[ri].Cells[2].Text; var txtCellphone = ItemsGrid.FooterRow.FindControl("txtCellphone") as TextBox; txtCellphone.Text = ItemsGrid.Rows[ri].Cells[3].Text; var txtEmail = ItemsGrid.FooterRow.FindControl("txtEmail") as TextBox; txtEmail.Text = ItemsGrid.Rows[ri].Cells[4].Text; var txtFax = ItemsGrid.FooterRow.FindControl("txtFax") as TextBox; txtFax.Text = ItemsGrid.Rows[ri].Cells[5].Text; } protected void ItemsGrid_RowUpdating(object sender, GridViewUpdateEventArgs e) { for (int i = 0; i <= ItemsGrid.Rows.Count - 1; i++) { var email = ItemsGrid.Rows[i].Cells[3].Text; var newName = ((TextBox)ItemsGrid.Rows[i].FindControl("NameText")).Text; var newTelephone = ((TextBox)ItemsGrid.Rows[i].FindControl("TelephoneText")).Text; var newCell = ((TextBox)ItemsGrid.Rows[i].FindControl("CellphoneText")).Text; var newEmail = ((TextBox)ItemsGrid.Rows[i].FindControl("EmailText")).Text; var newFax = ((TextBox)ItemsGrid.Rows[i].FindControl("FaxText")).Text; } } protected void ItemsGrid_DataBound(object sender, EventArgs e) { var btnInsert = new LinkButton(); btnInsert.ID = "btnInsert"; btnInsert.CommandName = "Insert"; btnInsert.Text = "Add new"; ItemsGrid.FooterRow.Cells[0].Controls.Add(btnInsert); var txtName = new TextBox { ID = "txtName" }; txtName.Attributes.Add("placeholder", "Name"); ItemsGrid.FooterRow.Cells[1].Controls.Add(txtName); var txtTelephone = new TextBox { ID = "txtTelephone" }; txtTelephone.Attributes.Add("placeholder", "Telephone"); ItemsGrid.FooterRow.Cells[2].Controls.Add(txtTelephone); var txtCellphone = new TextBox { ID = "txtCellphone" }; txtCellphone.Attributes.Add("placeholder", "Cellphone"); ItemsGrid.FooterRow.Cells[3].Controls.Add(txtCellphone); var txtEmail = new TextBox { ID = "txtEmail" }; txtEmail.Attributes.Add("placeholder", "Email"); ItemsGrid.FooterRow.Cells[4].Controls.Add(txtEmail); var txtFax = new TextBox { ID = "txtFax" }; txtFax.Attributes.Add("placeholder", "Fax"); ItemsGrid.FooterRow.Cells[5].Controls.Add(txtFax); } protected void ItemsGrid_RowCommand(object sender, GridViewCommandEventArgs e) { if (e.CommandName == "Insert") { var txtName = ((TextBox)ItemsGrid.FooterRow.FindControl("txtName")).Text; var txtTel = ((TextBox)ItemsGrid.FooterRow.FindControl("txtTelelphone")).Text; var txtCell = ((TextBox)ItemsGrid.FooterRow.FindControl("txtCellphone")).Text; var txtEmail = ((TextBox)ItemsGrid.FooterRow.FindControl("txtEmail")).Text; var txtFax = ((TextBox)ItemsGrid.FooterRow.FindControl("txtFax")).Text; var list = Session["CompanyUsers"] as List; list.Add(new User { Cellphone = txtCell, Email = txtEmail, Fax = txtFax, Name = txtName, Telephone = txtTel }); Session["CompanyUsers"] = list; BindGrid(); } } 

submitted by lcyduh to csharp [link] [comments]