I have a list of checkboxes in gridview.
You may check one or you may check as many as you wish, up to 10.
Once you check a box or boxes in a paging app, you page to the next page, the boxes are unchecked.
How do I keep the checked boxes to remain checked until a user unchecks them?
The code I have below isn't working.
For Each r As GridViewRow In GridMaks.Rows
If CType(r.Cells(0).FindControl("makos"), CheckBox).Checked Then
Dim marko As String = baseHint & "&cup=" & r.Cells(4).Text
CType(r.Cells(0).FindControl("marko"), CheckBox).Checked = True '// added this hoping to keep checkbox checked but it doesn't work
End If
Next
Sorry for including 2 flavors c# and vb.net. I can convert c# to vb if it helps.
If you're binding data to the grid each time a page loads, the checkboxes won't persist because they're being reset. Try performing a check like the simple sample code below to only bind data when the request is not a postback.
if (IsPostBack) {
// It is a postback, don't bind data
} else {
// It is not a postback, bind data
}
Also, you may find this article regarding Persisting checkbox state while paging helpful for what you need. In the GridView's "PageChanging" event, the article says to store the row index of each row that has a checked checkbox in a List, then store that List within the ViewState. When the page changes, retrieve that List from the ViewState and repopulate the GridView checkboxes.
Related
I have a aspx page that has several drop down list. So when the users forget to input some values i have to show an error to user, but every DDL are initialized again, and the users information are lost, how can I avoid this ?
When the users forget to input values i return them to current url
url1=www.spadsystem.com
Response.Redirect(Url1);
I heard that we can avoid this problem by using something like absolute i am not sure.
Use SessionState object to store the SelectedIndex property of ComboBox (e.g. Cmb), then apply it in Page_Load() event.
Example 1: Store value in Session
int _idx = Cmb.SelectedIndex;
Session["idx"] = _idx.ToString();
Example 2: Read from Session and apply to ComboBox:
Cmb.SelectedIndex = (int)(Session["idx"]);
More details at: http://msdn.microsoft.com/en-us/library/vstudio/ms178581%28v=vs.100%29.aspx
Rgds,
Avoid redirecting to another page and back to show the error, and ensure you initialize the lists if IsPostBack is false. Then, the ASP.NET Viewstate will take care of it all for you and keep all the selected values.
First of all do not redirect the user when its not required.
Secondly use RequiredFieldValidator for your Fields. It will prevent the postback when your users forget to input any value.
And thirdly, if you are Binding dropdownlists programetically, do that in
If(!IsPostBack)
{
//Your ddl initialization here
}
This will ensure you get the selected values from DropDownLists
agree with use RequiredFieldValidator. and, controls should hold their values if view state is on for the control (check properties).
I have got a radio button list and based on selection of the radio button list , the drop-downs will populate. Important thing is here the radiobutton list is set to autopostback=true.
And also when i move to next page by button click, And when i come back. Drop down button not able to maintain state. It is losing values. It is important for me to maintain state until i reach the last page. How can i approach this problem. I have used sessions but was not successful. Could you tell me how to implement sessions.
hi #Newyork167 this will not work as he mentioned above that "based on selection of the radio button list , the drop-downs will populate." so you have to store the "RadioButtonList" Selected value and accordingly fillDropDownlist Values and set selected after returning back to the page.
Ok. As per my understanding you need to save a page state so that when you come back then you get the page in previous state where you left. So to do this you have two ways.
1. Before jumping to next page, store everything in session like "Radiobutton selected value", dropdown selected value and other settings if any.
2. Pass these values "Radiobutton selected value", "dropdown selected value" and other if any as the query string and when you coming back then read the same query string.
In either way when you will come back then you will have the previous data. In the page_load event, just check whether you have that data or not. If yes then populate your controls with previous data else populate your controls for first load.
Here is some link for your reference.
http://www.codeproject.com/Articles/5876/Passing-variables-between-pages-using-QueryString
Passing Session[] and Request[] to Methods in C#
If you want to use sessions, you could check the session variable with all of the values of the list
if(Session["selectedList"] != Null){
var check = Session["selectedList"].ToString();
foreach(ListItem item in yourList.Items){
if(item.Value.Equals(check))
// set it as selected
}
}
For storing, when you click the button
Session["selectedList"] = yourList.SelectedValue;
You could also use the indexes instead of the values. You can also create a session variable for each dropdown/radiobuttonlist and you just make a loop for each.
UPDATE
Thanks to Shekhar for pointing this out. You need to go through all of the lists that you want to store and save them with these loops, not just the dropdowns. Then, you need to restore the radio button values, rebind the dropdowns, and then set the selected item for each.
There are ASP controls such as radiobuttonlist and checkboxlist and you can databind them to a database query. It's great for creating dynamic lists with user interaction. What I'm trying to do is generate a list of textboxes in the same fashion. A list of textboxes that behave the same way.
The object is to have a checkboxlist that is generated via datasource/database. When the user is finished selecting items from this list, they click a button. That list hides (using jquery) and a new list is created based on their selections. However, the new list is now a list of their selections accompanied by an empty textbox. The user fills in the textboxes for each entry and submits again which commits it to a database.
SO:
checkbox - description
checkbox - description
checkbox - description
checkbox - description
Becomes:
Description - Textbox
Description - Textbox
The reason that I'm looking for a list-type control is so that I can ultimately loop through it for submission to the database using linq. Does that make sense? My real question is if there is a control like this yet. I gave the full description in case someone has any other ideas, short of creating a custom control.
There's nothing out of the box that does what you describe no. But you can still loop through controls. I would put your form controls inside of an asp:Panel or a div with runat="server" and use something like the following code to cycle through them as you described.
foreach(Control ctl in myPanel.Controls)
{
//check control type and handle
if (ctl is TextBox)
{
//handle the control and its value here
}
}
asp:ListBox has a property named SelectionMode, which can be set to SelectionMode="Multiple" and allows you to select items you want. This is not exactly what you need but it's a simple solution.
After the selection is made in checkboxlist, make a postback to server. Here create a datatable with two columns (description & text). For every selected item in the checkboxlist add a row to this table and bind it to a gridview control. Here 'text' column will be always empty. Configure the gridview to use template column with a textbox in the itemtemplate
I wish to pass values of the selected items into a database and however noticed that the selected item is not what is sent into the database you can see in the snap shot below.
during run time the following value is recorded.
Where did it all go wrong with the dropdown lists selected item?
Counting on your intelligence...thanks.
Put your call to load the dropdownlist in
if (!Page.IsPostBack)
{
//LoadDropdownListHere();
}
Try DBNull.Value instead of null.
Edit:
I think you need to specify the type using this overload: http://msdn.microsoft.com/en-us/library/cc491445.aspx
CSMDataSource.InsertParameters.Add("CaseID", DBType.Int32, CaseIDDropDownList.SelectedItem.Text);
I found the problem for the dropdownlist always selecting the first value. It appears the dropdown list is always rebinded anytime i click a button that requires a post pack as such the data tha was binded at page load re binds a gain before the selected item gets picked as such the default first value gets picked all the time. to solve my problem i first disabled enable auto postback on the dropdownlist and in the code behind that is my .cs file during the page load, where i first binded the data to the dropdown list, i used a condition like if(!ispostback) to bind my data. what this does is when the page loads first time the dropdownlist is binded and if i should select an item from the drop down list, the "auto post back" that i disabled earlier on keeps the selected item selected and when i click and button that requires a post to the server, the (!ispostback) prevents the dropdownlist to be binded again before selection. so in effect, the selection is done first and afterwards if the page should load anew, the drop down list is binded again.
i was in a bit of a rush while typing so please bare with my typo errors. do call in anytime for more clarification...peace
I have an ASP .NET GridView with Paging. One column in it has a CheckBox. In a certain scenario, I want to uncheck the checkboxes that are checked.
foreach (GridViewRow dr in gvMyGridView.Rows)
{
if (dr.RowType == DataControlRowType.DataRow)
{
if ((CheckBox)dr.FindControl("chkIsApplicable") != null)
{
((CheckBox)dr.FindControl("chkIsApplicable")).Checked = false;
}
}
}
But unfortunately because of Paging only the records that are currently shown in the Grid can be accessed in this way. I want it to apply to ALL the items in the GridView. This should happen client side and when the user commits will get saved to the database. Any way to handle this? :)
Maintaining the State of Checkbox while Paging in Gridview
The Logic
Save the checked rows primary keys to a list at PageIndexChanging event.
After setting the grid to new PageIndex and re-binding the grid, populate the new page with values in the list that is mapped to the rows in grid(if any)
So you will have a collection of checked rows in a list.
Delete the list to clear all.
This is one way of doing it.
PS: Its a two year old post, so you can surely optimise it with C# 4.0