ASP.NET - How to remove dynamically added Gridviews from Asp:Panel - asp.net

I am populating an Asp:Panel with gridviews generated dynamically based on user selection. As the user changes the selection criteria and the date, the panel to show the new gridviews based on the search criteria. I am doing MyPanel.Controls.Clear(), but the gridviews are still showing the old result. Then i tried the following, but still of no use, the panle is always showing the first result.
foreach (Control c in MyPanel.Controls)
{
if (c is GridView)
{
MyPanel.Controls.Remove(c);
//Response.Write("**"+c.ID);
}
// else
// Response.Write("##" + c.ID);
}
Response.Write("cnt=" + MyPanel.Controls.Count ); // Always showing as 1 even when the count is greater than 1.
Any idea how i can clean the panel each time before i try to populate the panel with new results as gridviews?
ray..

Why you are generating panel each time make one or two panel as you required and just change their content or set visible hide true or false as you required creating panel each time is not a good approach.

Related

Persist checkbox in gridview while custom paging

I have a created a gridview and added a checkbox in item template. This grid has few columns along with DataKey (primary key). Due to performance gain, this grid will fetch the next set of recrods on each page change based on the page number click. So that is done.
Now when user selects a checkbox in page one and then go to page 2 and coming back to page one, then user will not see the checkbox checked as the user did earlier.
So is there a good way to persist the checkbox when user move page to page?
This checkbox be used as a flag to select the rows that can be deleted later by a button outside the grid.
Since you receive a new set each time a paging is selected, I suggest the following approach:
Create an array[] object via javascript that will add to list the datakey whenever a checkbox is selected and in turn remove it if the checkbox is deselected. Something like this:
var selectedDataKeys = [];
$('.checkboxclass').on('change', function() {
// Considering you assign the data key as id for the checkbox otherwise implement a way to retrieve the id.
var dataKey = $(this).prop('id');
// Determine if the dataKey is in the selected data keys array
var isContained = (selectedDataKeys.indexOf(dataKey) > -1 );
if($(this).is(':checked')) {
// If is contained is false - add to the array
if (!isContained)
selectedDataKeys.push(dataKey);
} else {
// If is contained is true - remove to the array
if (isContained){
selectedDataKeys = $.grep(selectedDataKeys, function(value) {
return value != dataKey;
});
}
}
});
From this point on the client user will have an active list of selected items, now its up to you to use that list to manipulate your grid display page. Either modify the display on document ready by comparing all the item on the grid display with the selectedDataKeys array or sending those keys and do the comparison server side.
Hope this helps.

Add new row when previous "new row" is saved in Kendo Grid

I have created a kendo.data.dataSource without transport (only data with model and fields) with success, and I am able to bind it to the KendoUI Grid on my page.
After loading, the grid is empty. I do calling the net line to add a empty data item in the grid so the user can enter data directly in the grid (inline mode).
$("#divid").data("kendoGrid").addRow();
This all works.
But after the user finished the input and hit the OK/Save button, I like to add a new empty row immediatly, below the previous added row. I try this during the grid function Save:
save: function(e) {
$("#divid").data("kendoGrid").addRow();
}
But the previous row with inserted data disappear and a new empty dataitem isn't added.
Also trying this same way during the datasource event 'change', but with the same behaviour.
My question is, what I doing wrong or what is the best way to add a new empty row to the Kendo Grid when user hits the OK/save button of a current inine editing row.
If you want to save multiple record at a time, you can go for batch editing. You can bind a keypress event to create new row as well. Try this:
$(document.body).keypress(function (e) {
if (e.keyCode == 13) {
var grid = $("#grid").data("kendoGrid");
grid.addRow();
}
});
13 is the value for enter key.

Why are my ASP.NET checkboxes always false?

I'm working on a ASP.NET web forms application. I have a four-column listview, bound to a datasource on pageload(), populated with contact names. One of the columns contains a checkbox. Users select a checkbox to indicate the corresponding contact should be processed in the next step.
The form also contains a button. When this button is clicked, the following code runs to process the selected contacts.
foreach (var x in lvPeople.Items)
{
chkSelected = (CheckBox)x.FindControl("IsLetterRecipient");
if (chkSelected.Checked)
{
// the person was selected by the user, do stuff here...
}
}
When I set a breakpoint on the line containing the IF statement, the breakpoint gets hit seven times (once for each row in the listview == seven checkboxes). However, the code inside the IF block never runs because .Checked is always False, regardless of the whether or not the checkbox is actually checked.
AutoPostBack, on the checkbox, is set to False. EnableViewState on the checkbox and listview is set to True.
What am I doing wrong? How do I get the .Checked status of the checkboxes?
Probably, when you bind the data on Page_Load you forgot to do:
if(!IsPostBack)
{
//bind the data to the list
}

Getting Checked rows from gridview in asp.net

I have a GridView in ASP.net where I have a CheckBox column. The user can toggle the CheckBox. Now, what I want is that when the user clicks on a button, all the records from the GridView where the CheckBox is checked should be displayed. And on another button, the opposite state should be displayed...
I am not getting the logic for the same.
Can anyone please help me with the logic..
You could iterate through the GridViewRows and check if the CheckBox is checked using something like the following
Edit from comments, fixed small bugs. Thanks guys. (3/20/2013):
foreach (GridViewRow row in yourGridViewID.Rows)
{
CheckBox check = (CheckBox)row.FindControl("CheckBoxName");
if (check.Checked)
{
//Take Row information from each column (Cell) and display it
}
else
{
//Display in seperate area
}
}
The index is going to be the column number starting from 0, going left to right of which column holds the CheckBox. You need to make sure the CheckBox has an ID name which is used at CheckBoxName. If you don't have an ID for that, you can also use
CheckBox check = (CheckBox)row.Cells[index].Controls[0];

Asp.net CheckBox within GridView looses its value

I have a GridView which has bound fields and a template field for checkbox. I wrote a code for deletion of records as per checking checkboxes. My problem is
HtmlInputCheckBox chk;
foreach(GridViewRow dr in dgvdetails.Rows)
{
chk = (HtmlInputCheckBox)dr.FindControl("ch");
chk.Checked = true;
if (chk.Checked)/// **here checkbox is not checked even if I'm check it**
{
pl.id = int.Parse(chk.Value);
bl.deletedgvdetails(pl);
}
}
There are two things that come to mind that could be at play here. I'm not sure when your code is running - with regard to the page lifecycle - but if it is being handled in a button click event you may want to make sure that your grid view has not been "rebound" during a page event such as Page_Load which runs prior to button click events firing. IF this is the case, all of your check boxes will have been reinitialized before your delete method runs.
Secondly, inside your foreach loop you may want to pay attention to the RowType of the current row. The first iteration through the loop may be focusing on the Header Row (if you have it enabled for the GridView) and therefore may not have the checkbox.
Checking that if(chk != null) {} is always a good idea if you use e.Row.FindControl() - just in case.

Resources