I am trying to write an aspx page containing a button whose click event should result in opening of a WPF form for further processing. I am trying to use a new process object to launch the WPF application.
I am using the following code in the code behind:
protected void Btn_Click(object sender, EventArgs e)
{
Process WPF = new Process();
WPF.StartInfo.FileName = "WpfApplication1.exe";
WPF.Start();
}
On execution, the button click executes without any exception, but the WPF window is not opened.
Can someone please help me.
Thanks.
The code you posted starts (or attempts to start) the WPF application on the server
If you want the client to run a WPF application you could use an XBAP application or a regular WPF application that is distributed through ClickOnce deployment so you can add a link to the application on your web page.
To launch WPF form from asp.net go to the View Code of code behind and Create an object for the WPF form and in the Page_Load event paste the following code
protected void Page_Load(object sender, EventArgs e)
{
Thread t = new Thread(() =>
{
mainWindow = new MainWindow();
mainWindow.Show();
System.Windows.Threading.Dispatcher.Run();
});
t.SetApartmentState(ApartmentState.STA);
t.IsBackground = true;
t.Start();
createDB();
}
Related
This is the code I'm using. The way I understood it, LinkButton1_Click is supposed to be called only when I click LinkButton1. Unfortunatelly, the method is being called when the page loads, so I'm kimd of lost. Is there anything wrong somewhere in the code?
protected void LinkButton1_Click(object sender, EventArgs e)
{
//Do something
}
override protected void OnInit(EventArgs e)
{
InitializeComponent();
base.OnInit(e);
}
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
this.LinkButton1.Click += new EventHandler(this.LinkButton1_Click);
}
Thanks for helping
If you are not calling "LinkButton1_Click" from code or using some javascript to finally trigger this method, then this should not be happening.
However I had similar problems that were caused by Visual Studio debug output.
Go to Visual Studio main menu and choose "Build" -> "Clean Solution".
You can also remove your "LinkButton1" from designer and force to rebuild solution. This should force debug files to be rebuild and phantom call will dissapear.
I have C# Web Application that has an aspx page hosting a user control (Review.ascx). Inside that user control there are 5 more user controls, one of which has a public event (Review_Summary.ascx). The problem is no matter what i do I cannot get the event wired up in the parent ascx control (Review.ascx).
Here is what I have in the child control (Review_Summary.ascx)
public event EventHandler forwardStatusChanged;
#region methods
protected void btnForward_Click(object sender, EventArgs e)
{
if (btnForward.Text == "Return")
{
if (forwardStatusChanged != null)
{
forwardStatusChanged(sender, e);
}
removeForward();
}
}
In the parent control (Review.ascx) I have this
public void initReview(string EmployeeNumber)
{
RevSummary.forwardStatusChanged += new EventHandler(RevSummary_forwardStatusChanged);
<more code here>
}
protected void RevSummary_forwardStatusChanged(object sender, EventArgs e)
{
lblReadOnly.Visible = false;
}
RevSummary is the ID of the child control in the parent control. InitReveiw is a method that is called by the aspx page in its Page_Load event.
I get no errors on compile or at runtime. But when I click the button the forwardStatusChanged event is null. The "removeForward()" method that is called after that executes properly. So that fact that the event is always null leads me to believe that the wire up in the parent control is not working. However, I am sure it is executing becasue all of the code after that executes.
How can I figure out why this event is not wiring up?
Where is initReview being called from? Are you sure it's being called because the only reason this happens is that the event handler wasn't truly setup. I've never found a reason other than this, the several times I did this myself.
HTH.
I am new to asp.net. I have an aspx page and i have to write some code in its PreInit event.
From where i find PreInit event on the page.
As we double click on button to get button click event(or selecting button and select event from property pane)
Plz reply me ASAP.
Man, why do you need the mouse?
If you need to write some code into PreInit just write the code:
protected virtual void OnPreInit(EventArgs e)
{
base.OnPreInit(e);
//your code
}
or in class constructor add a event handler for it:
...
PreInit += new EventHandler(SomeMethodName)
...
and define the event handler method
private void SomeMethodName(object sender, EventArgs e)
{
//your code
}
And by the way, check a .Net Framework book and a Visual Studio manual.
You need to do some reading:
http://msdn.microsoft.com/en-us/library/ms178472.aspx#lifecycle_events
Yeah some people would say "Are you crazy using winforms controls inside asp forms"... and I think they are right. But I would say.. "I'm not the only one!!, take a look"
http://www.eggheadcafe.com/tutorials/aspnet/b7cce396-e2b3-42d7-9571-cdc4eb38f3c1/build-a-selfcaching-asp.aspx
So...
Doing some kind of stuff like the previous link. I did the following:
using System;
using System.Threading;
using System.Windows.Forms;
namespace XXXX.aspx.Print
{
public partial class Drucker : System.Web.UI.Page
{
private ManualResetEvent mre = new ManualResetEvent(false);
protected void Page_Load(object sender, EventArgs e)
{
Threading();
}
private void Threading()
{
Thread t = new Thread(new ThreadStart(GoAhead));
t.SetApartmentState(ApartmentState.STA);
t.Start();
mre.WaitOne();
t.Abort();
}
private void GoAhead()
{
DateTime time = DateTime.Now;
WebBrowser webBrowser = new WebBrowser();
webBrowser.Navigate(Request.UrlReferrer.ToString());
webBrowser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webBrowser_DocumentCompleted);
while (true)
{
Thread.Sleep(0);
TimeSpan elapsedTime = DateTime.Now - time;
if (elapsedTime.Seconds >= 13)
{
mre.Set();
}
System.Windows.Forms.Application.DoEvents();
}
}
void webBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
WebBrowser webBrowser = (WebBrowser)sender;
if (e.Url.AbsolutePath != webBrowser.Url.AbsolutePath) return;
webBrowser.Print();
}
}
}
Now...
the DocumentCompleted event is not fired (neither ProgressChanged) and I've tried the next:
Add the library MsHtml.dll to my project and place the file into my lib folder... I did it... No changes.
Try to handle the state of the WebBrowser.ReadyState... I did it... No changes (Actually after receive the WebBrowserReadyState.Complete I tried to print the document with
webBrowser.Print(); but I receive a weird IE pop up telling me: "'dialogArguments.__IE_PrintType' is Null or a not an object'"... ok so doing some research I got: a microsoft topic about dcomcnfg and some com security settings
I did it... No changes.
...
By the way I´m working in 64bits, Win7...
So before I format the whole computer...
Any suggestions?
WebBrowser is never inplace-activated by its ActiveX container, which you don't have. Try put it on a form first.
By the way, you know you are in the unsupported territory when you use WinInet, the network layer of webbrowser control, in a Windows Service, right?
Well, just to keep it simple: I have a webform. On it a button called "Restart". I click on this button and IIS will restart itself.
Now, what would be the C# code that I would need to write behind the OnClick event of this web button? (If it's even possible?)
Then a second button is added. It's called "Reset" and should just reset the AppDomain for the current web application. What would be the code for this?
protected void Reload_Click(object sender, EventArgs e)
{
HttpRuntime.UnloadAppDomain();
}
protected void Restart_Click(object sender, EventArgs e)
{
using (var sc = new System.ServiceProcess.ServiceController("IISAdmin"))
{
sc.Stop();
sc.Start();
}
}
Process iisreset = new Process();
iisreset.StartInfo.FileName = "iisreset.exe";
iisreset.StartInfo.Arguments = "computer name";
iisreset.Start();
//iisreset.exe is located in the windows\system32 folder.
(http://www.velocityreviews.com/forums/t300488-how-can-i-restart-iis-or-server-from-aspx-page-or-web-service.html)
string processName = "aspnet_wp";
System.OperatingSystem os = System.Environment.OSVersion;
//Longhorn and Windows Server 2003 use w3wp.exe
if((os.Version.Major == 5 && os.Version.Minor > 1) || os.Version.Major ==6)
processName = "w3wp";
foreach(Process process in Process.GetProcessesByName(processName))
{
Response.Write("Killing ASP.NET worker process (Process ID:" +
process.Id + ")");
process.Kill();
}
Is there more than one web site hosted on the server where this code will run? If so, you may want to look at the System.DirectoryServices namespace, and restart the individual web site