Edit a WebView's HTML at runtime - c#

Is there a way to show the user a web page and let him edit it? It's enough if the user can put the cursor somewhere, then type a letter, and my code will be informed that the user has typed these characters in this location. My code will then do the rest.
Is that possible?

You need to invoke a javascript code to the loaded web page using the InvokeScriptAsync method. Then the javascript code may add a Content Editable element to the web page to do all the page related work and also handle events.
In order to interact with the injected javascript in the app (to receive events for example), you need to add a runtime object to the document. See AddWebAllowedObject.

Related

How to call a C# method from asp.net side?

This seems to be a common question but I could not find a relevant answer
I have a aspx page and in it,
I've made a list of "a href" tags that is generated from my C# code behind.
Now i want to send data based on which of the "a href" tag i have selected and pass it into my C# code which will then populate my popup with required information:
The process
"a href click" -> call c# method -> populate popup with data
I am stuck at how to pass data from the "a href click" to the c#.
I tried using .OnServerClick but my popup didn't even pop up.
For starters, i would like to try: When i Click a "a href" tag it would call my changeTitle() method from c# which will change the title of the pop up. If i can get this working, i should be able to manage the rest
c# method:
public void changeTitle()
{
this.modalTitle.InnerHtml = "Goodbye world";
}
Do tell me if you need more information please, I really hope to get this working
You should really look into learning more about how jQuery and ASP.NET can work together. I highly recommend starting at Dave Ward's site.
Here is a blog entry about using jQuery to call ASP.NET server methods (page methods are a great way to get quick hooks into server-side logic that can pass data back to the client-side):
Using jQuery To Directly Call ASP.NET AJAX Page Methods
I have used this technique in many projects over the years and I think once you start learning the power of jQuery you will want to use this approach over strictly server-side controls.
If ASP.NET WebForms is being used (remember to specify which "flavor" of ASP.NET), a LinkButton control may be a suitable approach1.
A LinkButton works like a a normal Button that looks like a hyperlink and causes a PostBack when clicked. (This is different from "normal" hyperlinks that change the browser location.)
The OnClick attrtibute specifies the server callback handler and the Command and CommandArguments can be used to associate specific data with the control that are available on the server during the LinkButton's click callback.
1 While my current preferred form of development is a thick client with a thin backend (e.g. not WebForms), switching to use "Page Methods" or "AJAX" requires rewriting the front-end/HTML to act on the response as appropriate. A LinkButton on the other hand, simply "works" with the normal ASP.NET WebForms lifecycle/infrastructure without any additional work.

ASP.Net Only 1 Form Restriction: Should I be using a webpage for everything?

I am forced to use Web Forms in my project, and sadly, Web Forms only allow - If I may - "Strict" Websites to be created.
Whenever you need a Button you need to put it in a form, and then you need another button which has nothing to do with the previous button, and you can't have 2 forms,
And the idea of putting a DIV that fires a server side (C#) method is kind of difficult, okay it may be easy but all I have found are "tricks", not an "official" clean way.
So I have this idea of making a webpage for each action in my websites.
For Example:
Let's say I wanna click on the ARROW that raises the rating of this question, I would put something like this.
HTML
Rate Up
And Some CSS Codes to make it look like a beautiful button...
Okay now this will take me to a page called Rating.aspx with 2 parameters, the first parameter is the ID of the question that I would like to raise its rating, and the second parameter is either UP (+rating) or DOWN(-rating).
On the Page Load method of Rating.aspx, I would update the database, then redirect to the question page.
This will work perfectly, BUT, is it a good approach? is it professional? (put in mind that there will be many actions to preform like that...)
With ASP.NET you better use server controls. Better way of implementing that is using or , that actually renders your anchor tag. But you can attach OnClick event handler to this control (link button) so after clicking there would be automatic POST to server. The same page cycle for the current page will take place (this is called PostBack) and your attached event handler will fire, where you can actually make changes to the database. So you don't even need to create any other pages for tasks like this. Every server control has specific set of events like OnClick for buttons or OnSelectedIndexChanged for dropdown lists. You can even create your own controls or derive from existing ones and create your own events.
Take a look on following links for more information:
Button Click
Event Handling in ASP.NET
ASP.NET Page Life Cycle

Non-MVC alternatives to ajax for passing data to usercontrols

I'm in a huge bind.
Long story short, I am working on a big project and am learning ASP.NET, C#, everything as I go.
The elementals of my project are comprised of user controls. My line of thinking was that I could create many user controls, each performing a function for a "component" of the project I'm building. Up until now I have been using clientside scripting to postback ajax calls to the code-behind on each of my user controls. AJAX worked well because it allowed to me pass data(that I need from the client) to my user controls and then I could return something in order to do an action.
I have been using a method for generating querystrings to create a callback "action" in order to determine what method needs to handle what data when the postback is sent to the code-behind side.
My problem now is that I need to start using many user controls one page -- and so now whenever ANY control does a postback ALL of the controls go through a page load. I thought my callback solution would take of this, but it isn't. Particularly when I drop a custom registered control into another user control.
I have done multitudes of research and having seen various ways to get around this, the best of them being [WebMethod] and controllers. However the project I am working on is NON-MVC.
I am also attempting to use UpdatePanel controls to minimize postback to the entire page but have had little success.
What can I use as alternatives? I feel like I'm running out of options or am missing something very basic here.
TL;DR -- I need a non-MVC method to pass data to user controls that can distinguish between multiple controls. Cannot use pagemethods(or page). Manual ajax calls are not working out. Cannot afford to do a full postback
Take a look at:
updatepanel vs page methods
Based on this:
My problem now is that I need to start using many user controls one page -- and so now whenever ANY control does a postback ALL of the controls go through a page load.
This might sound simple but have you tried to use if(!this.IsPostBack) in your load events?
Well not, the only way to avoid this situation, is using PageMethods or create a Script Service to handle AJAX requests (Web services with the ScriptService attribute or WCF REST services)
Note that even if you use the evil UpdatePanel, absolutely all the page life cycle will execute, which means that the whole page viewstate has to be sent in each post, the only advantage of using UpdatePanel controls is that you gain partial rendering, that's it, the performance on the server side doesn't change at all.
So you could use PageMethods or Script Services. But there's a catch, if you start using them you will notice an incredible performance change, your application will be more responsive (RIA applications), but the catch is that you won't be able to use the benefits of the ASP.Net server controls such as GridView, Repeater, etc. In other words you would need to change most of your view controls (this is the approach followed when working with MVC applications)
You can create static methods on your aspx page and mark it with [WebMethod]. Then you can call the method using jQuery ajax from the user user control markup. Take a look at this blog

SharePoint 2007 Custom Webpart, saving data to the properties

How can I go about creating a custom Web Part that has a custom UI for editing. When the page is in Edit mode I have a special button show up "Edit" and when the user clicks on it it posts back to the page. I catch this and I want to display a custom UI so they can edit the web part properties. I need to be able to have rich text fields, and text fields that are editable and will post back so I can save them to the properties.
I'm struggling mostly with creating a RichTextField, or TextBox that I can grab the data in post back. I know how to do it if I outputted raw HTML to the page, then use HTTP.Context, but what's the C# way to do it?
Thanks!
You can check in your web part if page is in Edit mode or Display mode and render the controls accordingly.
How to tell if page is in edit mode on a non-publishing site
On postback, you can fetch the values and assign it to your web part properties. Since you are handing it custom, you will need to set dirty flag:
http://msdn.microsoft.com/en-us/library/ms157656.aspx
It sounds like you're putting a configuration UI into the web part itself?
The standard way to provide a configuration UI is via the toolpart - the tall thin window that appears down the right hand side of a page when you edit a web part.
These posts should give you a good introduction :-
How To: Custom Web Part Properties (ToolPart)
Creating a Web Part with a Custom Tool Part
(SharePoint 2003 but still relevant)

ASP.NET Postback error

I am using C# to open a popup window and allow the user to select some value from that and send the value back to the server. However, when I close the pop-up window, I get the following error:
Invalid postback or callback argument. Event validation is enabled using <pagesenableEventValidation="true"/> in configuration or <%# Page EnableEventValidation="true" %> in a page. For security purposes, this feature verifies that arguments to postback or callback events originate from the server control that originally rendered them. If the data is valid and expected, use the ClientScriptManager.RegisterForEventValidation method in order to register the postback or callback data for validation. "
Does anyone know what might cause this?
I've seen this error when posting values back to the form that contain HTML markup or other restricted characters. By default, .NET blocks this for security reasons.
Check the values you are posting back to your page when closing the form to see if you are sending any markup in one of the input values.
If you absolutely have to send this type of data, disable the EnableEventValidation property on your ASPX page, and make sure to apply validation to prevent injection.
The most common reason this error comes up (for me) is when you're creating controls yourself imperatively (in the code-behind) rather than declaratively in the .aspx file. And specifically when during the initial page rendering you create one set of controls, and during postback you're page creation logic creates a (possibly slightly) different set of controls. All the controls on the page must be created and added to the same position in the tree each time the page is rendered, whether on postback or not. Otherwise this error can result.
There may be other causes for this error as well, but this is the only scenario where I've seen it.
Turn off event validation. It's totally unnecessary as long as you check the form values yourself and make sure you don't accept invalid values.
This event validation mechanism
reduces the risk of unauthorized
postback requests and callbacks. With
this model, a control registers its
events during rendering and then
validates the events during the
post-back or callback handling. All
event-driven controls in ASP.NET use
this feature by default.
You can turn off EnableEventValidation in the PAGE or the web.config- but this is considered a security risk.
you might try:
unless necessary make sure bindings are done in - !Page.IsPostBack
If your popup (or anything else) is injecting anything into the form which does the postback, then that may be the source of the error.
I have had this error when using hand wired jquery ajax calls to update cascading select lists (much faster than aspnet ajax), the new values that were put into the secondary select (which was runat=server) caused event validation to fail. You can either go through registering it all with the script manager (which I find a little painful), or disable event validation, but that does mean working a little harder and validating it all yourself.

Categories