I am developing my first Custom Server Control which will display a list of fields, with textboxes. When the page containing this custom server control is posted back, the values inside the textbox are lost as the repeater is either re-binded, or the viewstate isn't holding onto the values.
How can I get the repeater to hold the values of the textboxes between postbacks?
Cheers!
Most likely you are not re-creating your Custom Control at the right stage of the page lifecycle. Viewstate is restored right after the OnInit() event of the lifecycle. So if you are adding the control after this, the control is not there for the Viewstate to be restored.
Try moving the creation of your dynamic control to the OnInit() event of the page.
If you dynamically populate your repeater? then you might have a timing creation issue based on when View state is loaded.
Try putting all your dynamic control creation into the Page_Init event to preven the timing creation issue.
ie instead of using the Page_Load event use Page_Init event to bind data dinamically in your control.
protected void Page_Init(object sender, EventArgs e){ ..... }
regards, I hope I have been helpful
Related
Is there a way to pre-populate a MultiSelectList with selected items?
Example:
I have a single View that has the following ListBoxFor that will cause the page to update what it's displaying by allowing filtering of Model.Companies.
#Html.ListBoxFor(m => m.SelectedCompanies, new MultiSelectList(Model.Companies, "IdString", "CompanyName")
What I'd like to have happen is after the update, the MultiSelectList will have the items that were selected before the page updated and refreshed. Does that mean I need to return SelectedCompanies with what was selected, or is there another way?
I am using the javascript library Chosen for usability of the ListBox on the client side, but I don't think that this affects what I'm trying to do.
Sometimes, JS libaries can interfere with your desired results. I can't speak for Chosen JS library, but inspect the markup and see how it renders. As long as it still has the listbox on the client (it must have some input element defined somewhere; my guess it hides it and updates the values as they are selected), then yes it should integrate fine.
However, when the controller posts back, you have to repopulate the Model.SelectedCompanies property with whatever values came back from the POST operation to the controller. The property should still have the selected companies if you return a View from the POST operation. If you are using a RedirectToAction instead, you'd have to store the selections in TempData.
On a .cshtml page I have a single textbox inside a form. When I enter some text and press return it triggers a POST to a controller of the same name (different signature). The code processes the text and a model is created and passed back to the same page where in addition to the original textbox a grid is now populated (conditionally). Everything works, except the text in the textbox is retained.
This is a little odd, considering that most of the questions on here are about retaining the text after a POST and the indication has been that the text should not automatically be retained.
I can probably assign the textbox an empty string but I am simply wondering if I have this wrong?
Even if it was a simple textbox, html textbox is a input control that has two very important properties, i.e.
name
value
browser will send this name value pair to server
and the default behavior of server is to return the collection of all pairs back to browser when its done whatever it was supposed to do.
this collection is called post data. (or sometimes form data)
Yes, you could clear the textbox in code if everything was fine on postback
If something was wrong (exception or validation) you could do nothing ( as you do now) and text will be there again, what's makes sense in the UI
.
My List<BusinessObject> has some public properties that I want to bind to columns in a DataGrid. Unfortunately, the names of the public properties are not good and I may not even know what they are until runtime. For this reason, I set AutoGenerateColumns=True and interecept each DataGridAutoGeneratingColumnEvent so I can inspect what it is and either cancel it, hide it, or name the header something else.
It works great but I cannot figure out how to set the Mode=TwoWay so that my INotifyPropertyChanged events get fired once all the columns are generated and somebody edits a cell.
Bonus question:
On navigating up and down the rows of the grid, does the grid's datacontext automatically get set with that row's BusinessObject?
Thanks to this post, I learned that the binding happens on DataGridTextColumn. So the way to set the Mode at runtime is:
1 private void DataGrid1_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
2 {
3 DataGridTextColumn tc = (DataGridTextColumn)e.Column;
4 tc.Header = "Custom Header";
5 tc.Binding.Mode = BindingMode.TwoWay;
6 }
Now that I have TwoWay binding, I have to figure out how changes make it back to my BusinessObject.
If the binding is correct your business objects will automatically receive the required updates. To do you binding programmatically you might need a little more code, something like:
...
Binding binding = new Binding("Propertyname");
tc.binding.Mode = BindingMode.TwoWay;
...
HOw can i change content of ViewData on drop down list change using javascript in MVC View?
Short answer is: you can't.
Even if you could, nothing would happen Because you'd need to render your view with the new ViewData.
Javascript runs client side. That means, the user's computer.
ViewData is a server side collection. It is a data transfer object between your controller and view, which stays on your server.
You need to change the way you think. You can either make the change you want with javascript on client's comptuer when drop down changes, or you can post the value in drop down list and render the view with the new data, and send back to client.
If I'm not wrong you cannot change from client the server side object that is alive only during page rendering. You could change ViewData object of one page by putting there other data in controller.
I'm new to ASP .NET MVC and to web programming in general.
I'm wondering if there is a way to set ViewData variables when a radiobutton is selected -- but before the page is submitted.
Maybe I'm barking up the wrong tree but what I'm trying to do is create a form where new fields are added based on which radio button is selected. So what I want to do is when a radiobutton is clicked it sets a ViewData variable and based on that ViewData variable a different partial view loads the appropriate fields below the current field.
I imagine there must be someway of doing a onclick="some C# function that sets ViewData(args)"
Thanks
There are a couple of ways you could go about this.
1) You could have an Ajax form where through Javascript you post the form back and check to see if it's an Ajax Request, there by returning a partial view to a div that you specify.
2) Post the form as is and check server-side to see if the radio button was clicked, and thus redisplay the form with the new options visible.
If you take the first approach it would be easy enough to fall through to the second one for those without Javascript enabled.
There aren't really "onclick" events as I'm assuming you are used to from Webforms, you would basically have to roll your own Javascript to handle such things. Once you do a few, I think you'll find it's really not too bad, with the benefit that you'll have more control over what you're doing and through that gain a better understanding of the larger picture.
ViewData only exists, and only exists server-side, for the lifetime of the request. So, once the page is rendered the object no longer exists.
Some alternate approaches you can take:
1 - Use client-side Javascript to add a form and inputs as necessary. More info here:
ASP.NET MVC & JQuery Dynamic Form Content
2 - Pre-render the new form, but hide it via CSS, and unhide it when the appropriate radio button is clicked. More info here:
expand collapse html field Firefox
3 - Use AJAX to render the new form when the appropriate radio button is clicked. More info here:
http://www.asp.net/learn/mvc/tutorial-32-cs.aspx