Binding a treeview Dynamically and getting selected nodes - c#

I am trying to dynamically (i.e. via code) bind a directory to a asp.net Treeview control and once the data is bound and displayed to the user i want to get a list of nodes the select.
I have got the binding and displaying of checkboxes to work fine , but when i query the Treeview1.CheckedNodes it always returns 0. If i do not bind dynamically but created the nodes by hand then it is able to get the selected nodes.
Thanks

My guess is ... since you're dynamically building the whole thing from scratch every page load you're losing the selection.
You need to somehow store info about which checkboxes are checked before the post back (in Viewstate or Session or whatever depending on your needs) and then re-apply those selection to your tree after rebuilding it (on page load or preload if you store it in Viewstate).

Related

Get all items from Repeater's datasource when using pagination

I've got a Repeater control, bound to a PagedDataSource, which datasource is a list of custom controls I've made. These custom controls contains a couple of text boxes.
I have a save button, and when it is clicked I want to save the data in all the custom controls to a database, no matter which page they are on - but currently I only got access to the custom controls displayed on the current page.
What I've tried to do is to, in the btnSave_Click event, create a new temporary datasource equal to the current one, except its not a PagedDataSource. That way my repeater contains all custom controls - BUT - the changes made in the textbox fields are no longer available. I then tried to add JavaScript onchange events on the textboxes in the custom control, so that a postback would be fired whenever text was changed, and the property in the user control codebehind would be updated. This didnt work either.
Any ideas?
save the changed values on each page index changing event (or prev /next buttons) into your persistance object (List)
http://www.dotnetfunda.com/articles/show/1611/how-to-select-multiple-records-from-multiple-pages-of-the-gridview-and
The reason your non-PagedDataSource is empty is because the changes in your text box exist in the client and not on the server - you'll need to synchronise the values from your controls with the empty slots in your repeater.
The Repeater does not have built-in Pagination (like the GridView or other complex controls) so it does not offer events such as the PageIndexChanging event. I assume therefore, that you have your own Page navigation implementation. You should therefore call the function you have presented within that implemented function.
Try Using a generic List and Skip and Take methods of that

How to add tri state checkboxes to treeview in asp.net

I need to have a treeview control in asp.net with tri state checkboxes using c#. Any pointers would be helpful.
In cases where you want users to be able to select multiple nodes, you can use the TreeView control to display a check box next to a node image. When the ShowCheckBoxes property is set to a TreeNodeType other than TreeNodeType.None, check boxes are displayed next to the specified nodes. When check boxes are displayed, you can use the TreeNodeCheckChanged event to run your custom routine whenever the state of a check box changes between posts to the server. The next time the page is posted to the server, the CheckedNodes property is populated with the selected nodes.
You can use either for the codes with can help you
CodeProject:1
CodeProject:2
CodeProject:3
StackOverFlow
IF anything let me know.

How does Viewstate work internally

Scratch this!
I have googled my ass off looking for this.
1. Lets say that i have a webform that has a few textboxes, with ID's textbox1, textbox2, textbox3. They all have viewstate enabled.
I put some values in these textboxes and push a empty postback button and all the values are still there after postback. How can i access them in the viewstate after postback ?
I would think that they were saved under the ID name of the textboxes but i dont get it to work like so.
String s = ViewState["textbox1"].ToString();
I'm trying to get this to work because I want to be able to save the viewstate into the session so i can retrieve the info after i visit another webform.
2. Isn't it right that i can only use the viewstate on the same page that it was made on ?
I could not use the viewstate on default.aspx in editor.aspx ?
3. And one more thing, isnt it right that the viewstate saves how a treeview nodes are expended ? I would like save the state on the treeview between two webforms that use the same masterpage.
EDIT:
Ok, this wasn't clear enough, thats a given.
Basicly i'm trying to understand the viewstate and what i can do with it.
I dont usually use viewstate to store values.
What i'm trying to do, or figure out if its possible with viewstate.
I have a masterpage and on the masterpage is a treeview. I have two pages that i use with the masterpage, Default.aspx and editor.aspx.
I do my navigations and everything in the Default.aspx. When i have expanded the nodes in the treeview and selected one of the treenode, the navigateurl on that treenode send me to editor.aspx?navID=3. The editor.aspx uses the same masterpage and i want that page to show the SAME state on the treeview as the Default.aspx did before i clicked on the node.
Take a look at this article to learn more about viewstate. I found it helpful
Truly understanding viewstate
The reason your code does not work is because ASP.NET uses a different name (I think it prefixes the control name with the form name and the master page name , if there is one). But even if you could pull it using that method, you shouldn't. You should manually add a property yourself to the viewstate. So if your trying to preserve the text in a text box, use the following code:
ViewState["TextBoxText"] = textbox1.ToString();
And to retreive this later, use:
String s = (String)ViewState["TextBoxText"];
To answer your questions:
You are right. The viewstates are sacred to each individual page and cannot be accessed
Treeview will automatically save the expanded nodes. Just make sure you are doing your initialzation to the treeview inside a if (!Page.IsPostBack) block.
The Viewstate collection in System.Web.UI.Control only allows you to access the viewstate bag for that control, not child controls. So basically you can't do what you want to do through ViewState.
You can get the values that a control posted through the Request.Form parameters. For example, if you have a control call textbox1 you could get its posted value through
Request.Form["textbox1"]
Depending on the control you may have to do some processing on the value you get out of there. For a treeview you can get the posted value of its expanded state using
Request.Form[TreeView1.ClientID + "_ExpandState"]
The value is a string with either an e (expanded) or an n (not expanded) for each node. So if the value was "eennene", nodes 1 2 5 and 7 would be expanded while the others would not be

"Cannot load ViewState" after dynamic control changed

In my ASP.NET page I have to dynamically choose and load a custom control, depending on the selected value in a dropdownlist.However I encountered the following problem:
When the parameters of the dynamically loaded control are changed, and then the selection in the dropdownlist is changed( thus forcing me to load a different dynamic control the next time the page reloads ), I end up with a "Cannot load ViewState" exception.I assume that this happens because the ViewState is trying to restore the parameters of the old control and it doesn't find it.
So , is there any way to stop the viewstate from attempting to restore the state of the non-existig control?
You should load the controls the exact same way initially and then alter then after LoadViewState or disable the viewstate on the dynamic controls you know will not be in sync with the page.
It sounds like the state of the drop down / added control is not being restored before you are restoring the view state. If you have the drop down defaulted to show control X, and the user changes it to show control Y, the page must add control Y to the control collection before view state is restored.
Had the same issue where a variable length list of controls was added, rearranged and/or modified by the user and is changable during each postback.
The answer is surprisingly simple.
When you create the dynamic control set "EnableViewState = False" before you add it to the pages control collection. Then no viewstate information is stored and regardless of how many dynamic controls are added or removed or re-ordered the viewstate for everything else will work correctly.
If your adding these dynamically you are normally setting all the properties anyways so it didn't actually create any work in my case which is very similar.
I've the same issue with grid control. I was binding dataviews dynamically and according to DarrenMB's solution I've just write EnableViewState = false; and problem solved.
Infragistics.Web.UI.DataSourceControls.DataView dvMesaj = new Infragistics.Web.UI.DataSourceControls.DataView();
whdsShowMessages.DataRelations.Clear();
whdsShowMessages.DataViews.Clear();
whgridShowMessages.Rows.Clear();
EnableViewState = false; //here is the solution..
whdsShowMessages.DataViews.Add(dvKisi);
whdsShowMessages.DataViews.Add(dvMesaj);

Paged Custom ComboBox

I would like to have some sample code on how to do a paginated ComboBox.
My data consist on 1300+ items. When the user clicks the combo box arrow, the combobox will display display 25 (page size configurable) items at the time with arrows up/down (depending on page location) so that the user can request the previous/next page.
The data is coming from a generic list.(List)
Thus, the idea is to display only a subset of the data at the time.The user can scroll and select from the list as per normal combobox. At the top and bottom of the list should be a new button to request the previous or next page of navigator values.
Note: All data is read only. For legacy issues I can only use Winforms (.net 2.0) and C# but VB.net code will do as well
you might want to consider using a treeview as a dropdown control for your combobox, smth like is done here:
you can get source code for this control here: ComboBox control with a TreeView I guess it should give an idea on how to proceed with your task
regards

Categories