Silverlight - Dynamically creating controls without any postbacks - c#

Can I dynamically create controls in Silverlight without a postback to the server (even an asynchronous one). Does silverlight drag-n-drop requires postback?
I'm asking this because I've an asp.net application where I dynamically create/delete lots of controls. So after the postback I'm getting error with view state stating that the control tree doesn't match the view state tree.
Can I avoid such problems in Silverlight?

everything done in a silverlight control/application happen on the client. web service calls if any happen asynchronously. thats on of the advantages of using silverlight

Yes you can add controls dynamically to pages, without a round-trip to the server.
Drag-drop is also executed client-side.
Think of Silverlight as more like a desktop app, that talks to the server only to get/save data.
Adding controls dynamically in Silverlight is as simple as newing the appropriate control class and inserting it in the render tree (e.g. by adding it to a parent control).

Here is an example: http://asd.murven.com/blog/post/2009/10/16/Silverlight-Adding-controls-dynamically.aspx.
However, I would not suggest switching to Silverlight just to kill this bug. Only if you have a real need for a client-like application instead of a real web application. ASP.NET is suitable for dynamically creating controls as well. Please remember to initialize the control on the server during each postback. If this doesn't help, I would suggest you to submit a description of your problem with some code to help us solve it with you.
Br. Morten

The vast majority of what goes on in Silverlight involves no postback. In fact, I'd say Silverlight represents a completely different mindset. Whenever there IS a postback from Silverlight, it will almost always be asynchronous, and there is no "view state" that the server needs to worry about. In my opinion, it makes ASP.NET look like a joke when it comes to writing web applications.

Related

Why is my ASP.NET Web forms rendering slow?

I have some complex ASP.NET Web Forms website. Tracing showed this:
So basically, all my database calls and other complex, time-consuming things often take less time then ASP.NET Web Form render method. How to make it faster?
Rendering often takes around 200-300ms or even more. That is already above Google's reccomendation for time for first byte
Is it because it is not small? But it is around 40kb (size of rendered website).
I if was database query that is slow I would know what to do it? But here I don't know how to make faster, apparanetly, internal ASP.NET Web forms method?
First, if you have codes in the Page_Load event, u might have to look into the codes, not remove it, but look for another way to write them.
Second, check all controls on the form, remove idle controls if any, or look for alternate controls to replace some of the ones you used

Dynamic Web Controls when using Pure Razor

I want to make a dynamic control on a web page that does an action and generates new HTML code without needing to refresh the page (what would traditionally be handled by JavaScript, I presume). However, my project is currently comfortably and neatly nestled in as a pure ASP.NET Web Pages project strictly using Razor, no MVC at all.
I've hunted everywhere and haven't gotten a clear answer; what answers I have found always concern deep MVC or Web Forms components. As I understand, it should be fairly easy to make use of bits and pieces of those, but I'm still fairly inexperienced with ASP.NET.
What I want to make in short: A button that
Is an image
When clicked, increments a record value (already stored in the Razor code, simple to push to the database), and replaces itself with another image button
This button has a different image, will decrement the record value and replace itself with the original button (they are effectively the inverse of each other)
This is the most complex control I need at the moment for my project, and learning about this will hopefully provide a basis from which to engineer different controls.
The underlying technology of the server is irrelevant, whether it's PHP, ASP.NET, Ruby, et cetera - if you want a web page to "do something", you need to send a request to the server. If you want it to do something without the user directly submitting a form or clicking on a link and changing their location, you have to send the request to your server with javascript. Javascript is how you get the client to do things. The razor templating system operates on the server, not on the client.
Specifically, you should look in to Javascript AJAX requests; I'd particularly recommend learning about jQuery, because it simplifies a lot of aspects of Javascript.

Deploy a WPF user control in a Silverlight application

I have developed a user control in WPF which draws some graphs.
Now i have to show this user control in a Silverlight application (to show on ASP.net webpage).
Is this possible to use a WPF user control in a Silverlight application?
I have searched on Google, but have not found a satisfactory answer.
No, it is not possible to show a WPF control in Silverlight. They use two different runtimes so are not directly substitutable with each other.
You have a few options though:
use XBAP to show WPF within the browser
rewrite your control so that you can compile a version for Silverlight or WPF (this is (was) quite a common way to do it)
Edit:
in response to your comment you seem to have some misunderstandings, I think you haven't understood the links I gave you. You may also have misunderstood what Silverlight is - just in case you have let me mention that Silverlight runs as a plugin within the web page, it isn't directly part of the HTML structure.
For the XBAP approach the WPF control/page is hosted inside a web page - just like a Silverlight control is. However you don't have direct access to the local filesystem or network filesystem (or databases running on the network) - Silverlight is the same, to access a database you really need to go via a WCF service.
With the second approach you have two versions (one for WPF, one for Silverlight) of your control and you use compile time targeting to dictate which control is built. You then use the appropriate control in the appropriate project.

How to have more than one silverlight application in an ASP MVC project?

I have an ASP MVC application which uses some silverlight applications in the View. By now, I hava a map, that, when a State is clicked, redirects to the related page. Now, I need a silverlight application in which state, with the state map and the location of the harbors on this state (you chose a state in the first map, than you get redirected to the chosen state, where you can select a harbor that will be highlighted in a table - not sure how I'll achieve that yet). Which type of class should I use to this new silverlight map of each state? Is it easy to do what I want? (I created an user controller class, but expression blend couldn't open it, don't know why).
It sounds like all your Silverlight controls are so similar in function that a single Silverlight application would do.
You can pickup the host-page url from within Silverlight and therefore use url parameters to determine what to display.
You also have options of communicating via Javascript to exposed methods on your Silverlight application and also the reverse (executing Javascript from the Silverlight app).
Not sure why you had problems with Blend without seeing the offending user control.
Hope this helps.

How can I load .ascx user control from console application or windows service?

We use ascx user controls as templates for documents (e.g. invoices).
Now I need to load,render to HTML, then transform to PDF these controls from windows service.
So, what's the correct way/workaround? TemplateControl.LoadControl(path) doesn't work.
To be perfectly honest, I don't know how to do this. However, if I was stuck with this problem, I think I'd download the ASP.NET MVC source, and see what they do to render a partial view (a partial view in MVC is essentially an .ascx control). The Render method on System.Web.Mvc.WebFormView might be a good starting place.
You're probably going about it the wrong way, to be honest. You should probably have your logic embedded in a library, and then have the user control interface with that library. Then, you could use that same logic to populate values on a form, or similar.
You're probably going to run into a lot of trouble trying to get the control to load, and deal with things that don't exist outside of ASP.Net, like session variables, and viewstate.
Generally, what you're asking for is a report, which is handled by something like Crystal Reports, or SQL Server Reporting Services.

Categories

Resources