ajaxtoolkit fileupload event after upload - c#

I am using ajax toolkit fileupload for uploading images. I would like to add an event. Such as response.redirect after last image is uploadet. How will this be possible?
Tried with OnUploadCompleteAll but without any luck
Thanks
Code:
OnUploadComplete="File_Upload"
protected void File_Upload(object sender, AjaxFileUploadEventArgs e)
{
//SAVE UPLOADED FILES
}

Use UploadCompleteAll event.
That will fire after the all the files are uploaded.
Just type OnUploadCompleteAll and press tab twice. A event handler will be put up by visual studio.
Do whatever you want in that handler

When using UploadCompleteAll, the event args type should be AjaxFileUploadCompleteAllEventArgs not AjaxFileUploadEventArgs.
protected void Upload_End(object sender, AjaxFileUploadCompleteAllEventArgs e)
{
}

Add this to your AjaxUploadControl:
OnClientUploadComplete="ClientUploadComplete"
Then this part stays as is on server side:
AjaxFileUpload1.Attributes.Add("OnClientUploadComplete", "Return OnClientUploadComplete");
Last part is to add this to your client side scripts:
function ClientUploadComplete() {
var f = $(".filename").html();
window.location.replace("upload.aspx?filename=" + f);
}

Related

How can I upload a file into a form using a WebBrowser

I am trying to upload a file automatically using my webBrowser.
Let me explain : I have a form with an "input type='file' " element and i am trying to automatically add a file into it.
I already tried this and this and it still doesn't work.
Here is my code :
private void createSystem(string fileUpload)
{
webBrowser1.Navigate("https://test.com/OrderMngt/uploadclient.aspx");
webBrowser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webBrowser1_DocumentCompleted);
}
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
webBrowser1.Document.GetElementById("UploadFile").Focus();
SendKeys.Send(" C:\\Users\\me\\Downloads\\Upload\\file.csv{ENTER}");
return;
}
The web page is reached, the "Choose File Dialog" is opened but the value oh the path is always empty (except if I fill it manually of course -_- ).
Do you have any idea?
Thanks!
It's not possible due to security restrictions in the browser.

simulate click button after opening webpage

I have a requirement where I open a webpage using
WebRequest wr = WebRequest.Create(uri);
This webpage has a download button which needs to be clicked to download a zip file to my local directory. I do not know the URL for this download button, if i mouse over it it just shows the .aspx address.
Now how can I simulate this click so I can download the file?
using System;
using System.Windows.Forms;
namespace SampleBrowserautomate
{
public partial class Form1 : Form
{
// first of all, insert web browser control and button control into your form
string target = "https://www.facebook.com/login.php";
public Form1()
{
InitializeComponent();
}
private void btnGo_Click(object sender, EventArgs e)
{
webBrowser1.Navigate(target);
}
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
WebBrowser b = (WebBrowser)sender;
b.Document.GetElementById("email").InnerText = "helloworld#gmail.com";
b.Document.GetElementById("pass").InnerText = "HelloWorld";
b.Document.GetElementById("u_0_1").InvokeMember("click");
}
}
}
Another examples on msdn
http://msdn.microsoft.com/en-us/library/System.Windows.Forms.HtmlElement(v=vs.110).aspx
http://msdn.microsoft.com/en-us/library/system.windows.forms.htmlelement.setattribute(v=vs.110).aspx
What about UI automation? Is this what you're after?
http://msdn.microsoft.com/en-us/library/dd286726.aspx
EDIT: How about Web UI automation?
http://msdn.microsoft.com/en-us/library/hh404082.aspx
So I am working on this in between things, but check this out.
You can fire the client side event of that button using this javascript:
__doPostBack('DownloadButton','OnClick');
This will work with the 'javascript:' header in the chrome console. Now you could probably automate this using greasemonkey(firefox) or another add on for chrome.

Remove redundancies in code

I'm still new at this so I will try to explain my problem the best I can. English is not my first language so I apologize if I use some terms incorrectly.
I have a 100 line code that is executed every time a button is pressed. My problem is, I have 20 buttons and they all contain the same code (they are only slightly different in means of grabbing info from different source). Is there any way to do this instead of copying the same code too many times.
Basically my code is this:
private void button1_Click(object sender, EventArgs e)
{
//file data source url
sourceUrl = ("www.myurl.com")
//Grab data
code
code
code
//Store data
code
code
code
//Write data
code
code
code
}
Every button has the same code except for the "sourceUrl" part. If I want to add more buttons I have to copy>paste the whole code and my application is starting to get HUGE. Is there any way to shrink the code by only having the code once, and then calling an action or method every time the button is pressed. So instead of having 100 line code multiple time, I'll have one line code for each button and one 100 line code on the top that will be the source for that one line code.
Thanks in advance
Use the Tag property of your buttons to store the source url string and then set, for every button, the same event handler
private void buttonCommonHandler_Click(object sender, EventArgs e)
{
Button b = sender as Button;
CommonMethod(b.Tag.ToString());
}
private void CommonMethod(string sourceUrl)
{
// Execute the common code here....
}
You could set the common handler and the Tag using the form designer window or you could do that dynamically mimicking the code prepared for you by the designer in the InitializeComponent call
button1.Click += buttonCommonHandler;
button1.Tag = "www.myurl.com";
button2.Click += buttonCommonHandler;
button2.Tag = "www.anotherurl.com";
That's what functions are for. Use this layout:
private void YourFunc(string sourceUrl)
{
//Grab data
code
//Store data
code
//Write data
code
}
Now your buttons' event handlers look like this:
private void button1_Click(object sender, EventArgs e)
{
YourFunc("www.myurl.com");
}
private void button2_Click(object sender, EventArgs e)
{
YourFunc("www.myurl2.com");
}
Sure there is a way. Just make the whole function a function that takes the url as a string parameter. And then call that function from your code behind.
private void button1_Click(object sender, EventArgs e)
{
//file data source url
ProcessData("www.myurl.com");
}
private void ProcessData(string sourceUrl)
{
//Grab data
code
code
code
//Store data
code
code
code
//Write data
code
code
code
}
Here we can use use CommandName property, by passing URL in every button's CommandName property and passing it as a parameter to your Common Method to fetch data , So you can create a single function and call it through you btn_Click event .
<asp:Button ID="button1" runat="server" Text="clickMe"
CommandName="put your URL here" OnCommand="button1_Click" />
<%--OnClick="button1_Click" />--%>
See here we can pass your 'URL' in CommandName property ,few things to keep in mind , Here we are using OnCommand event rather than OnClick event of button , so that we can use 'CommandName' property here .1. OnCommand MSDN
2. Commandname MSDN
// private void button1_Click(object sender, EventArgs e)
private void button1_Click(object sender, CommandEventArgs e)
{
string sourceUrl = Convert.tostring(e.CommandName)
// Call function to grab data pass URL as parameter.
GrabDate (sourceUrl ) ;
So now we can get the URL value from button CommandName property .
3 . EventArgs Class
4 .CommandEventArgsClass

How can I invoke javascript functions with c# with Gekofx Browser

I am using the Gekofx browser because my html files don't work with the default webbrowser control.
So far I was using ObjectForScripting to call javascript code from my C# project. But I was not able to call anything with the Gekofx browser.
I just want to send some data to my html file and display it with the Gekofx browser. Is it possible at all?
For contemplation here is my code:
GeckoWebBrowser myBrowser;
public Form1()
{
InitializeComponent();
String path = #"C:\tools\xulrunner\xulrunner-sdk\bin";
Console.WriteLine("Path: " + path);
Skybound.Gecko.Xpcom.Initialize(path);
myBrowser = new GeckoWebBrowser();
myBrowser.Parent = this;
myBrowser.Dock = DockStyle.Fill;
}
private void btn_go_Click(object sender, EventArgs e)
{
// like the normal browsers
myBrowser.Navigate(tbx_link.Text);
}
private void btn_test_Click(object sender, EventArgs e)
{
// getting the link to my own html file
String path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Webpage");
path += "\\Webpage.html";myBrowser.Navigate(path);
}
I hope you understand what I mean. Thank you!
You can always invoke javascript like this:
mybrowser.Navigate("javascript:YourJavascriptFunction('yourArgument1', 'youArgument2')");
Building on #jordy's answer above, call:
mybrowser.Navigate("javascript:YourJavascriptFunction('yourArgument1', 'youArgument2')");
prefferably in the Document complete event handler to allow for the page to first load.
void myBrowser_DocumentCompleted(object sender, Gecko.Events.GeckoDocumentCompletedEventArgs e)
{
myBrowser.Navigate("javascript:YourJavascriptFunction('yourArgument1', 'youArgument2')");
}

Creating a log file detect on click of a button

Is there any way to allow me to trace button click in which webpage.
For example
Default1.aspx
button1 click
Then in my log file.My log file will log that button1 click in default1 page .
Is possible to do this in the beginrequest event of the global.asax file?
Please guild me a solution .
Here is my code for global.asax
protected void Application_Start(object sender, EventArgs e)
{
// Set logfile name and application name variables
string l4net = Server.MapPath("~/log4Net.config");
log4net.Config.XmlConfigurator.ConfigureAndWatch(new System.IO.FileInfo(l4net));
}
protected void Application_BeginRequest(object sender, EventArgs e)
{
//What should i code here???
}
Any help is much appreciated
Yes, surely you can do it.
You can get the current page using
var page = HttpContext.Current.Request.Url;
and the button is present in the event target
var button = HttpContext.Current.Request.Params["__EVENTTARGET"];
This gets you the ClientID of your control, but you should be able to extract the control ID from it.

Categories

Resources