Within my site master I have an area where I would like to display status (info, success, error, warning) messages.
In my code behind I would like to make calls such as:
MessageSuccess("Some success message");
MessageSuccess("Another success message");
MessageWarning("Warning message");
and then have these messages all display when the page is next rendered.
I have tried a couple of approaches where I:
Save a structure in Session
Read the structure in Page_PreRender() and update some HTML controls
Clear the structure in Page_UnLoad()
However some of the time the messages show just fine, but some of the time by the time PreRender comes around Page_Unload() has been called and nothing displayed because the structure is empty.
Has anyone got a generic working solution that I can use with my WebForms project to "nicely" display status messages?
Once I was using something very close to example described in Displaying a Custom Error Message section of the Working with Partial-Page Rendering Events. It works nicely if you are dealing with asynchronous postbacks. The only downside is that it was designed for exception handling and not for success/warning/etc. notifications.
There is a property AsyncPostBackErrorMessage on ScriptManager which you can use to pass any text message back to the client. Client will retrieve the message in endRequest event handler:
Server code:
// inside Page code:
var scriptManager = ScriptManager.GetCurrent(this /* assuming that this is a Page instance */);
scriptManager.AsyncPostBackErrorMessage = "Hello world";
Client code:
Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler);
function EndRequestHandler(sender, args) {
if (args.get_error() != undefined) {
var errorMessage = args.get_error().message;
args.set_errorHandled(true);
// Do whatever you need to display the message
}
}
Another possible solution would be to add client function calls to the page using ScriptManager.RegisterStartupScript. I can imagine that you can have a NotificationHandler JavaScript object which would have a success, warning, error, etc. functions which you will call from the server side:
protected void Page_PreRender(object sender, EventArgs e)
{
ScriptManager.RegisterStartupScript(this, GetType(), "<Some unique key for script>", "NotificationHandler.success(\"Hello world\")", true);
}
Related
I have implement payment method with return URL and using webview open URL with my app. Recent, I'am issue want auto close webview with response success or fail process payment.
I encountered this case. Here is the solution I found.
After creating the WebElement, we bind the Navigating event to a certain method.
MyPage.Source = link;
MyPage.Navigating += Webview_Navigating;
MyPage WebElment name.
link is my link address
.
Then we perform our transactions here.
private void Webview_Navigating(object sender, WebNavigatingEventArgs e)
{
var url = e.Url;
if (url.Contains("status=1"))
{
int ordID = 0;
//save order
}
}
For me, if the changed link address checkout status=1, the checkout is successful.
You need to call Url property in WebNavigatingEventArgs to catch the changed url
If useful, choose. Good Luck
Sure! You can get response form WebView.
I recommend you use HybridWebView (see: https://github.com/devsmadeofsteel/Plugin.HybridWebView) as alternative.
Examples:
In Xamarin page, create a browser add register a named callback, and add browser to page childs
HybridWebViewControl Browser = new HybridWebViewControl();
//You can load browser content by string or URL, here is string which you can hard code or store in resource files
Browser.ContentType = Plugin.HybridWebView.Shared.Enumerations.WebViewContentType.Internet;
Browser.Source = YourUrl;
Browser.AddLocalCallback(YourCallBackFunctionName, CallBackFunction);
When run, HybridWebViewControl will insert a function with the name you specified in
YourCallBackFunctionName, and the single parameter is string type. So, In JavaScript of the page loaded by YourUrl, you can call back with the specified CallBackFunctionName:
function AnyFunction() {
//...
YourCallBackFunctionName(YourStringParameter);
}
When you call YourCallBackFunctionName(YourStringParameter) in JavaScript, you can get YourStringParameter in CallBackFunction using C#.
So, you could define different CallBackFunctions or pass different parameters with single CallBackFunction, to control the HybridWebView and the page or view which contain it, such as hide HybridWebView or close the page.
Have a try!
I'm working on creation of a winforms application which contains a WebBrowser user control.
The WebBrowser control navigates to a page in a web application which requests user authentication details. When the user name and password are input and the form submitted, the web application sends a request and, if authentication is successful, populates variables in an applicationValues object.
When the applicationValues object is instantiated, I want to read the object from the web application in the WebBrowser control back into my Winforms application and close the form containing the WebBrowser control.
Currently, this is working (the object gets created successfully in the web application) until the part where the applicationValues object should get passed back to the Winforms application; there I'm stuck.
Can this be done with this control? If not, are there any other approaches or workarounds I should be considering?
It can be done, but it will require some work on your part. First of all, the easiest way to pass data back and forth between the web browser control and whatever page is in it, is via window.external and invoke script. Of course you have to set the objectforscripting attribute in your user control.
Once you have done that you can freely send data 2 ways from javacript to .net via primitive types. Now for your object specifically, it may be worth your time to serialize it into a JSON string and then send it via window.external, and then deserialize into a counterpart object on the .NET side. You could use System.Web.Script.Serialization's JavaScriptSerializer Or of course you could use JSON.net to deserialize as well.
Here is the basic idea taken from the MSDN
private void Form1_Load(object sender, EventArgs e)
{
webBrowser1.ObjectForScripting = this;
webBrowser1.DocumentText =
"<html><head><script>" +
"function test(message) { alert(message); }" +
"</script></head><body><button " +
"onclick=\"window.external.Test('called from script code')\">" +
"call client code from script code</button>" +
"</body></html>";
}
public void Test(String message)
{
MessageBox.Show(message, "client code");
}
private void button1_Click(object sender, EventArgs e)
{
webBrowser1.Document.InvokeScript("test",
new String[] { "called from client code" });
}
I am working on a project that includes a parent page, This parent page includes two iframe tags that is connected to two other child asp pages. I need to protect a time instance, and it should be like this: In the first child page it should be locked, and when the other child asp page reads from it should be released. Can anyone give me an advice please?
[WebMethod]
public static void SetCreatioTimeAndID(DateTime CreationTime, string ID)
{
ResourceProtector.Sem.WaitOne();
SessionHelper.CreationTimeFromEvents = CreationTime;
SessionHelper.EventID = ID;
}
and here is the jquery function that calls on the method:
function EventGrid_ItemSelected(sender, e) {
var item = e.get_item();
var creationTime = item.Data[0][1];
var id = item.Data[10];
PageMethods.SetCreatioTimeAndID(creationTime, id, OnSuccess);
}
function OnSuccess(response, userContext, methodName) {
}
Since the gridview(ComponentArt grid) im using doesnt have a server side event when a row is selecteddoesnt, i have to ouse a client side event, to retrieve the row is selected. then i call the server side function to store the Date of the row. If you like this is the whole scenario:
when a user choses a row on the gridview i have to call a server function from a jquery function in the page. The problem is this server function has to be static. therfore i store it in a static DateTime object, so when the user presses a button, the information regarding that DateTime object is loaded. the problem is if another client connects to server and chooses a row, im afriad the DateTime objecdt is
this link answered my question. This way i can call non-static methods on the server from javascript.
Here is the link: Call non-static method in server side(aspx.cs) from client side use javascript (aspx)
I have a C# method from a web form that I want to call asynchronously with with either a Page Method, Web Method or possibly jQuery to avoid postback to the server. The method calls other methods and rebuilds a treeview and performs validation and sets some other values as well. Below is an example of the code.
What would be the simpleset way to do this?
Any code samples provided would be greatly appreciated if possible. No update Panels..
protected void btnSubmit_Click(object sender, ImageClickEventArgs e)
{
if (ValidateSelection() == true)
{
int ProductID = Convert.ToInt32(grdGetProducts.SelectedValue.ToString());
if (Convert.ToInt32(ddBetTypeID.SelectedItem.Value) != 2)
{
SubmitProduct(Convert.ToInt32(ddProductTypeID.SelectedItem.Value), currentWeek, Convert.ToInt32(ddProductID.SelectedItem.Value), Convert.ToInt32(ddValue.SelectedItem.Value), Convert.ToInt32(ddCost.SelectedItem.Value), ProductID);
}
if (Convert.ToInt32(ddProductTypeID.SelectedItem.Value) == 2)
{
int price;
price= 1;
//if 1 open side then part 2
if (txtProductID.Text != "0" && txt2ProductID.Text == "0")
{
price= 2;
}
if (ProductID > 2)
{
BuildTree(currentTime, Convert.ToInt32(ddProductID.SelectedItem.Value), currentProduct);
}
}
}
Without an update panel you are certainly going to need a lot of client side javascript.
I do things like this with dynamic tables all the time and the first thing I did was isolate my data and my display. You should start by doing the same.
Figure out what data is being validated and how you can gather it on the client to be sent to a web method. Then you need to write client scripts to do this gathering and posting. After you post with the client script, in the callback of the ajax call, you would then need to do the rendering on the page. Without an update panel this is all on you.
The issue you have without using an update panel is that you are going to have to recreate the treeview structure or manipulate it on the client side. This means replacing all the elements and their event handlers. This can be a very daunting task for the more complicated ASP controls.
What the server would need to send back for rendering would be all of the data the tree needs. It is on you to figure out what that is though.
I have dynamically rendered gridviews before because it is nice to use them to create complex styles. Then it is just a matter of creating rows with the script on the client, based on the data I get from the server; I imagine you can do something similar to this with a treeview.
I have learned a lot about jQuery, ajax and ASP.NET interactions by reading encosia.com. That individual is a wonderful resource.
Things to avoid using jQuery AJAX and ASP.NET
Mistakes: manual JSON serialization
Using jQuery to directly call asp.net page methods
Update Panels are Dangerous
As title state after my solution on aspx.cs I want to switch to another web page to remove cookies and avoid resending the same email.
Do the PRG pattern
After you done with your transaction, Redirect to another page (showing "It is done successfully" or some other message). Now Refresh can not beat you
To Redirect, You can use Response.Redirect method
Response.Redirect("welcome.aspx")
You could add a Session which prevents the email being sent again (if this is a once per session email (ie. Newsletter sign up etc)).
Then check whether this Session exists before sending the email:
if (Session["emailsent"]==null){
//Send email code here
Session["emailsent"] = true;
}
you have to test
in your page_load event
if(!IsPostBack)
{
//send mail...
}
else
{
//do nothing
}