How to retain field values on error in a dynamic gridview? - asp.net

I have a page which has a GridView on it, which is populated from a database. Some of the columns in the GridView have text boxes, as well as Checkboxes.
When the user saves the page, the page may error if they have not entered their data correctly. At this point, I need to re-display what they have entered already so they can simply make the correction instead of having to change everything from scratch again.
The part that I'm having trouble with is the fact that this GridView can have a variable number of rows, so in turn a variable number of fields. What would be a good way to retain those values?

e.Cancel = true; will do the trick
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
e.Cancel = true;
//Display Message here
}

Related

How to edit gridview row inside?

I want to edit row of gridview.For that, I have added showeditbutton = true.I have binded gridview from cs file.Does I need to wite 3 function for that?(For editing I have added 3 function in cs file.).I have taken help from internet.But some point did not understand.
--In aspx
<asp:GridView datakeyname="Id" Id ="Gridview1" onRowEditing="GridView1_RowEditing" RowCancelingEdit=" GridView1_RowCancelingEdit" onRowUpdating ="GridView1_RowUpdating" >
<column>
// hyperlink ,dataTextfield is id
// some checkboxfield.(start from column 6)
</column>
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
//code for Binding grid
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
// for fetching value of id and checkboxfield(column 6)
string Id= GridView1.DataKeys[e.RowIndex].Values["Id"].ToString());
bool ischeck = (Gridview1.Rows[e.RowIndex].Cells[5].Controls[0] as checkBox).Checked;
// code for updating grid
GridView1.EditIndex = -1;
//Now bind the gridview gain here
}
protected void GridView1_RowCancelingEdit(object sender, GridViewUpdateEventArgs e)
{
GridView1.EditIndex = -1;
//Now bind the gridview gain here
}
Does am I going in right direction?What is use of datakey.Does I used properly?Why GridView1.EditIndex = -1 in update and cancel event.Column 6 is checkboxfield.why .Controls[0] is used for accessing that checkboxfield.
If you are using an ObjectDataSource (or SqlDataSource or OleDbDataSource) to databind and use UpdateCommand, DeleteCommand, InsertCommand, then you do not need to explicitly write those three functions for the edit/update operation. You need those functions when you are writing the binding code in code-behind or if you want to do additional work before/after any operation.
RowEditing fires when you click "edit" on the GridView. Here you specify what row to open in editmode by writing GridView1.EditIndex = e.NewEditIndex. You can also write code here to do any work that is required before user is put into editmode. For example, you can check for business rules conditions, and cancel the operation if rules are not met.
RowUpdating fires when you click "save"/"update" on the GridView. This is fired before the actual database operation. If you have an UpdateCommand on the datasource, then you do not need to write database save routine, otherwise you write that here.
DataKeys identify the "key" that identifies the data that is bound. You specify DataKeys while databinding to the GridView. For example, primary key of a database table. This line: string Id= GridView1.DataKeys[e.RowIndex].Values["Id"].ToString()); Here you are picking up the value of the "Id" key (you can have more than one keys) of the current row.
GridView1.EditIndex = -1 in update or cancel specifies that the GridView should no longer be in editmode. If this value is >= 0, then the GridView is put into editmode for that row (index starting from 0). So we set it to -1, to indicate that it should not be in editmode.
Controls[0] is used to pick the first control in that cell (you may have more than one controls). Alternatively, you can also use FindControl.

radGrid control displays blank grid after date parameter is changed

I searched SO, google & Telerik forums, but could not find a solution.
I have an existing app (written by a previous developer) that is calling a stored procedure that populates a RadGrid control. It populates fine the first time around.
However, when I change the date parameter, click "search" button, I get a blank RadGrid control. When I click search the second time, the grid is populated. When I walk through the code, I get an error message
Column 'ID' does not belong to table Table.
How can I resolve the issue of having to click search twice to display data ?
My code behind is:
protected void btnSubmit_OnClick(object sender, EventArgs e)
{
try
{
ViewState["newset"] = null;
CreateDatasource();
this.RadGrid1.DataBind();
this.RadGrid1.CurrentPageIndex = 0;
ViewState["newset"] = "new";
string idex = this.hdnindex.Value;
if (idex != string.Empty)
this.RadGrid1.MasterTableView.Items[int.Parse(idex)].Selected = true;
}
catch (Exception ex) {
this.lblMessage.Text = ex.Message;
}
}
protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e)
{
try
{
if (e.Item is GridDataItem)
{
GridDataItem item = (GridDataItem)e.Item;
HyperLink hLink = (HyperLink)item["ViewHyperLink"].Controls[0];
if (hLink != null)
hLink.Attributes.Add("onclick", "selectMe('" + item.ItemIndex + "');");
}
}
catch (Exception ex)
{
this.lblMessage.Text = ex.Message;
}
}
In the code behind above, when this.RadGrid1.DataBind() is called, the code steps into RadGrid1_ItemCreated loops through the if statement a few times, them goes into the if statement, comes out of the function, and then the catch statement of btnSubmit is called, which displays the error message "Column ID does not belong to table Table".
Any ideas on how to resolve this?
Maybe you should try Grid - Simple Data Binding. Also you this thread (RadGrid NeedDataSource Page load) on telerik forum talks in detail about Simple Data binding. I don't know if you are trying to use NeedDataSource, but I have had similar issues before and the Simple Data binding worked perfectly.
That sure sounds like to me you need to call RadGrid1.Rebind() somewhere. Try calling that for your last line in your try block inside btnSubmit_OnClick.
Also, what does this search button do? Does it filter the results based on a date?

How to store cell value of an Asp.Net GridView row on click in a C# variable

I am having a GridView in my page , I want that when a particular row is clicked I want the cell value of that particular row.Currently I get that that value by using something like this ,
protected void SearchRecordGrid_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes.Add("onclick", "location='ModifyCustomerByCategory.aspx?ApplicationNo=" + e.Row.Cells[0].Text + "'");
}
}
Currently I am passing that value as a parameter to the page , can any body tell how can I store that value in a variable or , atleast on click I assign it to a hidden field.
Thanks for any suggestions.
Try this link.
It runs through everything you will need, and on the gridview selectedindexchanged event you can reference the value with GridView.SelectedRow.Cells[0].Text or the control containing the value Gridview.SelectedRow.FindControl("[Control Name]")
In row data bound simply save that value and use it
int appno = CInt(e.Row.Cells[0].Text)
and if you want to use this value to next page
int appno = CInt(Request.Querystring("ApplicationNo"))

Call add variable to GridView in UpdatePanel cause recreate page every time

I have that code in my page :
public partial class Utworz : System.Web.UI.Page
{
private List<Codes> Codes;
protected void Page_Load(object sender, EventArgs e)
{
this.GridView1.DataSource = Codes;
Codes = new List<Codes>();
}
protected void btn_AddKodManually_Click(object sender, EventArgs e)
{
Codes.Add(new Codes(code: this.TextBox1.Text, codeType: this.DropDownList1.SelectedValue));
this.GridView1.DataBind();
}
}
My page contains UpdatePanel in which I have form. I type data in this form and add to GridView.
and problem is that every time when I call Click method my Page was create new, and my variable Code also. Why this variable not save his state ?
Problem #1: The Codes variable is getting set to a new object every time the page loads. It will never have more than one code.
Problem #2: There is nothing here to hold the value of the Codes list from page view to page view. You need to store it somehow and retrieve it every time you want to rebind. For example, create a property called Codes and store the value in the viewstate. After adding a new code, rebind the grid.
I understand that you might expect the grid to hold it's state, but you're rebinding with a new object every time. Someone else might chime in here, but you may be able to restore the Codes object by calling:
Codes = this.GridView1.Datasource

ASP.NET Formview Item Inserting Event

I'm using the FormView control in ASP.NET for a simple form to insert into a MS SQL DB. I have an event for onItemInserting to set some values behind (such as time stamp, etc) and was curious how to check some user entered values in the onItemInserting event and cancel the item from being inserted. The reason I want to do it in the code behind is to query the database and use the values to validate the user entered data.
Pseudo Code is as follows:
protected void Form_addRoom_ItemInserting(object sender, FormViewInsertEventArgs e)
{
... Query DB for some values ...
if(enteredMaxPeople > queryMaxPeople)
{
**Cancel** DB Insert
statusLabel.text = "Value entered not valid";
}
}
In the end the question comes down to how do I cancel a FormView from inserting in the code behind?
Thank you!
Sean
e.Cancel = true;

Resources