Tekerik radwindow open and close issue - c#

With the click event of a link button calling a server side method from Form1
Form1 aspx.cs page code:-
protected void lnkTakeAction_OnCommand(object sender, CommandEventArgs e)
{
if (e.CommandName == "duplicate")
{
//Some operations conditional
//If condition true
//call javascript method to call a javascript function to open a rad window
ScriptManager.RegisterStartupScript(this, typeof(System.Web.UI.Page), "ABC "ABCPopUp" + ID + "','Tag');", true);
}
}
Form1 aspx javascript code:-
function ABCPopUp(ID, Tag) {
var oWindow = window.radopen('XYZPage.aspx?ID=' + scoreMethodId + '&Tag=' + mode, 'rdWindowMapping');
oWindow.SetSize(600, 500);
oWindow.Center();
var titleBar = oWindow.GetTitlebar();
$telerik.$(titleBar).parent().hide();
return false;
}
Issue facing - The XYZPage is not opening.The same functionality works if I call the function ABCPopUp from javascript.But when I call from server side the form is not opening.Please give expert advice

Most likely, the script runs too early. You have two options:
use AJAX for the grid and do not include the RadWindow in the response
OR, use the Sys.Application.Load event to call your function: http://www.telerik.com/help/aspnet-ajax/window-troubleshooting-opening-from-server.html.

Related

Close webview2 application after webpage submit button click

I have created a simple windows application that uses a webview2 to shows a Website in a winform. I need to close this windows application after submit button click on the website.
Is it possible?
I am new to Windows Development.
The solution really depends to the content and how the page works. In most cases you should not close the app immediately after clicking on submit button and you need to wait until you receive the response of the submit and if the submit is successful, then you can close the window.
Here I share two examples:
Handle submit event and call a C# method from WebView2
Detect success of a submitted survey form based on URL
Please keep in mind, both examples are here for learning purpose to give you some idea on handling js events in C#. You need to tweak them to make them working on your specific case.
Example 1 - Handle submit event and call a C# method from WebView2
In the following example you can learn:
How to handle submit event of the form
How to call a C# method from JavaScript code and pass js object to .NET
Here is the code:
WebView2Host host;
private async void TestForm_Load(object sender, EventArgs e)
{
await webView21.EnsureCoreWebView2Async();
host = new WebView2Host(this);
webView21.NavigateToString(
"<html>" +
"<head><title>Test from</title></head>" +
"<body>" +
"<form id='form1' action='/' method='post'>" +
"<input type='text' name='someProperty' value ='something'/>" +
"<input type='submit' value='Click me'/>" +
"</form>" +
"</body>");
webView21.CoreWebView2.AddHostObjectToScript("host", host);
webView21.CoreWebView2.DOMContentLoaded += CoreWebView2_DOMContentLoaded;
}
private async void CoreWebView2_DOMContentLoaded(object sender,
CoreWebView2DOMContentLoadedEventArgs e)
{
await webView21.ExecuteScriptAsync(
"var form = document.getElementById('form1');" +
"form.addEventListener('submit', function(e){" +
"var data = JSON.stringify(Object.fromEntries(new FormData(form)));" +
"window.chrome.webview.hostObjects.sync.host.OnSubmit(data);" +
"});");
}
public class WebView2Host
{
TestForm hostForm;
public WebView2Host(TestForm hostForm)
{
this.hostForm = hostForm;
}
public void OnSubmit(string data)
{
//data is in json format
MessageBox.Show($"data: {data}", "C# OnSubmit!");
//You can close form like this: hostForm.Close();
}
}
Exmaple 2 - Detect success of a submitted survey form based on URL
You can rely on any of the navigation events and check the URL of the page, to see whether the submit has been successful. In this example I show how you can do it with a google survey form:
private async void TestForm_Load(object sender, EventArgs e)
{
await webView21.EnsureCoreWebView2Async();
webView21.Source = new Uri("https://forms.gle/pspLBJFZW5z8J1K37");
webView21.CoreWebView2.SourceChanged += CoreWebView2_SourceChanged;
}
private void CoreWebView2_SourceChanged(object? sender,
CoreWebView2SourceChangedEventArgs e)
{
if (webView21.Source.ToString().EndsWith("/formResponse"))
{
MessageBox.Show("Submitted successfully.");
}
}

Once the popup closed, main page should be refreshed to load changed data

private void AddOrderItems(object sender, EventArgs e)
{
string url = "Orderdetails.aspx";
ClientScript.RegisterStartupScript(this.GetType(), "OpenWin", "<script>openNewWin('" + url + "')</script>");
}
I am using above code to open a popup window.
I have a page order.aspx from which I am opening another aspx page (Orderdetails.aspx).
Orderdetails.aspx page opens as a popup.
once user close the window(Orderdetails.aspx) , Orders.aspx page should post back and load newly added items.
How to catch the close event of the popup window?
Put this script block in your child window:(Orderdetails.aspx)
<script>
window.onunload = refreshParent;
function refreshParent() {
window.opener.location.reload();
}
</script>

How to open page in new tab using the response. redirect at asp.net

I want to open a new tab or a new page by using Response.Redirect in a button click handler. I'm using a query string to pass some values. How can I open he page in a new tab?
protected void btnSave_Click(object sender, EventArgs e)
{
...//some code to insert records
Response.Redirect("NewQuote.aspx?val=" + this.txtQuotationNo.Text);//displaying gridview in other page to print what needed
}
Try this. This works sure for me...
protected void btnSave_Click(object sender, EventArgs e)
{
...//some code to insert records
Response.Write("<script>window.open ('NewQuote.aspx?val=" + txtQuotationNo.Text+"','_blank');</script>");
}
Simple solution is here.
Edit your html button element and add attribute OnClientClick="target ='_blank';".
<asp:Button ID="myButton" runat="server" CssClass="btn1"
OnClick="btnSave_Click" OnClientClick="target ='_blank';" />
Then in btnSave_Click
protected void btnSave_Click(object sender, EventArgs e) {
Response.Redirect(url);
}
You can change your design element as below : OnClientClick
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" OnClientClick ="document.forms[0].target = '_blank';" />
Javascript code :
Response.Write("<script>");
Response.Write("window.open('NewQuote.aspx' ,'_blank')");
Response.Write("</script>");
I encountered this same problem today.
Response.write didn't work for me, to solve it I used instead System.Web.UI.ScriptManager.RegisterClientScriptBlock.
This is how your btnSave click event should be:
protected void btnSave_Click(object sender, EventArgs e)
{
...//some code to insert records
System.Web.UI.ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "openModal", "window.open('NewQuote.aspx?val= "+ this.txtQuotationNo.Text+"' ,'_blank');", true);
}
NOTE: I'm using ScriptManager.RegisterClientScriptBlock because the button is inside an UpdatePanel, if that's not your case you should try:
protected void btnSave_Click(object sender, EventArgs e)
{
...//some code to insert records
Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "openModal", "window.open('NewQuote.aspx?val= " + this.txtQuotationNo.Text + "' ,'_blank');", true);
}
if you are using http
then use below code
Response.Write("<script>window.open ('URL','_blank');</script>");
this code cannot be used for https
for https do below
javascript in page
function shwwindow(myurl) {
window.open(myurl, '_blank');
}
in c# code behind
string URL = ResolveClientUrl("~") + "**internal page path**";
ScriptManager.RegisterStartupScript(this, this.GetType(), "show window",
"shwwindow('"+URL+"');", true);
this code cannot bybass the browser popup blocker. user must allow it to run.
for ie it opens in new window or new tab up to ie version
in firefox and chrome it opens new tab
enjoy it!!
A redirect is always in the same page as where you came from, you can't open a new window from a redirect call.
I would suggest to inject some javascript code in the client to open the new page on reload, or change to a control that can open to a new page, like a LinkButton with the right Target attribute.
You can use ScriptManager to do the needed:
private void Redirect_New_Tab(string url_To_Open)
{
string modified_URL = "window.open('" + url_To_Open + "', '_blank');";
ScriptManager.RegisterStartupScript(this, typeof(string), "OPEN_WINDOW", modified_URL , true);
}
Worked for me when I left the Double Quotes off of target ='_blank';
Response.write didn't work for me, so I used instead System.Web.UI.ScriptManager.RegisterClientScriptBlock.
System.Web.UI.ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "openModal", "window.open('NewQuote.aspx?val= "+ this.txtQuotationNo.Text+"' ,'_blank');", true);
I spent quite some time on this, and while it does work to set the target on the form to _blank it will mean that every other control on that page will attempt to open in a new window afterwards. And you can't manipulate the form back server side, since the response will never reach the orginal tab.
So I made it work with a combination of setting the target to _blank when the button is clicked, and then shortly after reset the target back.
I put in this Javascript function on the page:
<script type="text/javascript">
function openInNewTab() {
document.forms[0].target = "_blank";
setTimeout(function () {
document.forms[0].removeAttribute('target'); // After 500 ms revert back to normal mode so all other controls are not affected.
}, 500);
}
And on the control that needs to open in a new tab just add:
OnClientClick="openInNewTab();"

How to refresh the page? (Click button close RadWindow)

I have 2 pages (Home and Сategory)
Load page Home on there's button. Click button run panel RadWindow (NavigateUrl: Сategory).
protected void ShowWindow()
{
string script = "function f(){$find(\"" + RadWindow_editor.ClientID + "\").show(); Sys.Application.remove_load(f);}Sys.Application.add_load(f);";
ScriptManager.RegisterStartupScript(Page, Page.GetType(), "key", script, true);
}
protected void RadButtonEdit_Click(object sender, EventArgs e)
{
ShowWindow();
}
Load RadWindow NavigateUrl - Сategory on there's button. Click button close RadWindow.
protected void RadButtonEdit_Click(object sender, EventArgs e)
{
ClientScript.RegisterStartupScript(Page.GetType(), "mykey", "Close();", true);
}
function GetRadWindow() {
var oWindow = null;
if (window.radWindow)
oWindow = window.radWindow;
else if (window.frameElement.radWindow)
oWindow = window.frameElement.radWindow;
return oWindow;
}
function Close() {
var oWindow = GetRadWindow();
oWindow.argument = null;
oWindow.close();
return false;
}
How to refresh the page Home? (Click button close RadWindow)
Thank you!
Here's how: http://www.telerik.com/community/forums/aspnet-ajax/window/how-to-refresh-the-page-click-button-close-radwindow.aspx#2894238.
If your page does not refresh, then the problem is in the page. Having a has in the URL can cause this problem. Make sure you change the URL so you have a fresh GET request.
There are client-side events like OnClientBeforeClose and OnClientClose.
http://demos.telerik.com/aspnet-ajax/window/examples/clientsideevents/defaultcs.aspx
and check this link, it gives you an idea.
How to close the radwindow on serverside and refresh the parent page
You should also look at using a RadAjaxManager in the parent page to send the window events to the parent as shown in the the following demo: Grid and Window. It focuses on refreshing a grid, not a page in your instance, but the concept of sending events to the parent page remains the same.

jQuery Submit Heisenbug

I have a heisenbug - it works when I put a Firebug breakpoint on it, but it doesn't work without the breakpoint.
The LoginSubmit function is called from two places in my code. In one instance, the btnLoginSubmit_Click event is fired successfully, but in the other instance, the event doesn't fire unless I put a breakpoint on the $submitButton.click(); line.
function LoginSubmit() {
$("#diagnostics").text("");
var $submitButton = $("#<%=btnLoginSubmit.ClientID %>");
$submitButton.click(); // <-- This is the line of code that is not executing.
$("#diagnostics").text("LoginSubmit failed to submit postback. ");
}
<asp:Button ID="btnLoginSubmit" runat="server" Text="Submit" CssClass="hiddenButton"
OnClick="btnLoginSubmit_Click" OnClientClick="return true;" Visible="true" />
Code behind page:
protected void btnLoginSubmit_Click(object sender, EventArgs e)
{
....
}
Consider registering a PostBackEventHandler instead of firing the onclick of the button
public partial class ScheduleEdit : System.Web.UI.Page,
System.Web.UI.IPostBackEventHandler
{
protected void Page_Init(object sender, EventArgs e)
{
var postBack = Page.ClientScript.GetPostBackEventReference(this, null);
var script = "function postback() { " + postBack + "; }";
Page.ClientScript.RegisterStartupScript(GetType(), this.UniqueID, script, true);
}
// this is called via the client side 'postback' script
public void RaisePostBackEvent(string eventArgument)
{
DoStuff();
}
}
// client side script:
$("#DP").datepicker({
onSelect: function (dateText, inst) { postback(); },
});
The issue involved a problem in jQuery involving dialogue boxes. When I closed the dialogue box before issuing the submit, it worked.

Categories