I'm Using a GridView in my Application.
My GridView has a checkbox column and when the checkbox is clicked, the page is redirected to an other form.
When I return to previous from, nothing comes and GridView data is lost.
I used session and it's working fine, but I want that checkbox which was clicked to also maintain there state.
Is there better method to do this?
Upon redirection to the next page, Iterate your Gridview to update your Datatable with a checked checkbox column and then set that datatable to a session variable. I am assuming that you are already storing your source Datatable to a session.
Sorry, but no other magic work around !!!
Related
I have a page (using MS Ajax). depending on other options, I may need to create a radio Button List inside a Panel.
This is done as per below.
RadioButtonList rbl = new RadioButtonList();
rbl.SelectedIndexChanged += new EventHandler(answer_Click);
rbl.AutoPostBack = true;
foreach (KeyValuePair<string, int> d in _answers)
{
ListItem li = new ListItem(d.Key.ToString(), d.Key.ToString());
li.Attributes.Add("class", "radio");
rbl.Items.Add(li);
}
p.Controls.Add(rbl);
this works fine, unless after the postback I need another RadioButton List. The list is drawn correctly, with all the correct options, but now when I click on an option the first time, it fills in and then resets. It takes a second click to get it to set and trigger the SelectedIndexChanged Event.
Im destroying and recreating the rbl once answer_Click is triggered (I know this as the next question that is created has different answers and options).
So, any ideas as to why it is I need to click twice on the second List?
Matt!
Looks like you have a ViewState issue. Controls are created after the LoadViewState trigger, so they doesn't get their values from the browser to load. You must save this value somewhere, using JS, ViewState, QueryString, Session, DB to persiste the state, and then load the selected value into each one.
Hopes this help you! Take a look here for more info about ASP.NET Page Life Cycle.
I have a gridview included "Edit CommandFields Update and Cancel" at the last column of my gridview. Also, I have a search button above my Gridview and everything works properly together. When I am searching for a particular Column the "sqlDataSource" of my Gridview is changing respectively on the query and displays the results.
However, the Edit/Update/Cancel command field buttons refresh the whole Gridview and I am loosing the selected editable row derived from the search results.
How can I prevent grdiview Databing when I click the Edit Command Field ?
Any advice would be appreciated,
You are probably missing the famous is Page.IsPostback check.In vb.net you will have to do some thing like this
If Not Page.IsPostBack
->your binding code goes here
End if
C#
if (!Page.IsPostBack)
{
->your binding code goes here
}
This way your results are not lost when the page reloads.
I want to do something really simple, I just can't seem to find the EnableClientAddRow property, so I can set it to true. I have a standard GridView control on a web form. I want a button to appear on the web form. When the user clicks the button, an empty row is added to the GridView UI, so the user can enter data in the appropriate fields. The row will of course, have a "Save" button of some type in one of the columns.
I know this functionality must be in the GridView somewhere, I just can't find it. I did find some odd hacks that try to manually implement this. I'm not really interested in footer manipulations or binding tricks, just the standard add row method.
EDIT:
It appears the GridView does not support adding a row as a first-order operation. This appears to be a serious design flaw.
I typically add a new record to the underlying data source as a part of the "add record" button click action. I then re-bind the view in order to show the blank row.
The new record is typically a DataRow if the GridView is bound to a DataTable, or an object if the GridView is bound to a collection of a particular type. Not sure if that is what you consider a binding trick from your question, but it works well and is quite easy to implement.
Edit - more detail to describe the process:
Add the row to the data source, set the EditItemIndex to the newly added row in order for the row to enter edit mode, then bind the data source to the GridView. Your EditItemTemplate would contain a Cancel and a Save button. Cancel would re-bind the GridView to the underlying data source without the empty row and set EditItemIndex to -1, thereby removing the row from the GridView.
How to easily insert row in GridView with SqlDataSource
If you add a new row to the datasource, even if the row has empty values, and you databind the datasource to the Gridview, it should show up as an editable row just like any of the other rows.
I have a gridview full of telephone numbers. To populate the gridview I bind the gridview's datasource to a List<> of telephone numbers. I do this when the page is first loaded, but not on postbacks.
I want the user to be able to delete some of the telephone numbers, and then, if they want, click a Save button, and this will update the database, otherwise their changes will be ignored. So I have a button in the grid, and an event is fired, and I can call DeleteRow(row index) and remove the row from inside this event. For some reason this doesn't work.
All the gridview examples I find on the Internet execute the delete straight away by calling an sql function, and then bind again. And some examples bind the grid every time the page ios loaded, which seems inefficient.
My questions is:
The delete button causes a postback to the server. On postback the list of telephone numbers no longer exists. And the gridview's datasource is null. The grid is no longer bound. But there must be data somewhere, because the data in the grid is still visiable. Where is this data, and can I delete a row of it, so that a row in the gridview is deleted?
The viewstate saves the contents of the datagrid, so the answer is "The Viewstate"
Understanding the viewstate is essential to understanding how ASP.NET works, so rather than posting just enough info to answer your question, I'm going to recommend you read the entire article I linked to.
You can use jquery , you need to save the Datakey value of each deleted row in a hidden field
and hide the selected row , and when user clicks save , u can delete the rows based on hidden field values # code behind.
Well i have a gridview where i have defined the columns on my own and turned autogenerating off but now i have the problem that i cant access GridView.SelectedRow.DataItem.
As it turns out to be null now, when it had a value when auto generation was turned on..
Edit:
What i need is a way to save the ID of the row while not showing the ID to the user so if there is any way to do this?
I'm guessing DataItem is only properly filled when you are using DataBinding.
Are you using DataBinding?
Ok from this url:
The GridView (and actually, all our
data controls) does not save data
items across postbacks. This reduces
ViewState (if the objects are even
serializable) and enables garbage
collection to happen and clean up your
objects. So, when you click the
button to post back, the GridView has
not called DataBind and therefore your
data item isn't there. This is what
you've discovered.
Guessing you're reading the value from a postback, might just be the problem.
Try using SelectedValue, if you've setup the (primary) key for the items.
I've always used that and it worked.
msdn about SelectedValue
You can create a new hidden template column that will have a label with the ID . and in the cs file you use .FindControl on the rows.
You also have DataKeys property on the gridview, witch I think also does what you want