AjaxControlToolkit AsyncFileUpload - how to modify a label text value in UploadedComplete event - asp.net

I am trying to set a label text value after a file is uploaded to the server using a AsyncFileUpload component in AjaxControlToolkit. But it seams it is ineffective, although the file uploader is green after the upload, and the upload works.
protected void AsyncFileUpload1_UploadedComplete(object sender, AjaxControlToolkit.AsyncFileUploadEventArgs e)
{
string filename = System.IO.Path.GetFileName(AsyncFileUpload1.FileName);
AsyncFileUpload1.SaveAs(Server.MapPath("Uploads/") + filename);
sourceLabel.Text="saved to "+filename; //this has no effect. I assume this is because the event is Async, but how can I set the value of sourceLabel?
}

The AsyncFileUpload control uses hidden frame for file submitting so all updates of controls will be lost. Check this link and draw attention how the uploadResult label's text changed from AsyncFileUpload1_UploadedComplete method: Ajax Control Toolkit source code

It works for me: https://stackoverflow.com/a/12472235/2247978
.....................................................................................................
Add HiddenField control onto a form:
<asp:HiddenField runat="server" ID="UploadedPathHiddenField" />
Rewrite UploadComplete method as below:
protected void UploadComplete(object sender, AsyncFileUploadEventArgs e)
{
var fileName = GeneratePrefixFileName() + System.IO.Path.GetFileName(e.FileName);
var relativePath = "~/Image/" + fileName;
var filePath = Server.MapPath(relativePath);
AsyncFileUpload1.SaveAs(filePath);
ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "filePath", "top.$get(\"" + UploadedPathHiddenField.ClientID + "\").value = '" + ResolveClientUrl(relativePath) + "';", true);
}
After that you can get path of saved image in showConfirmation method by :
var src = $get("<%= UploadedPathHiddenField.ClientID %>").value;
.....................................................................................................
protected void BtnUpload_Click(object sender, EventArgs e)
{
UploadMessage.Text = UploadedPathHiddenField.Value;
}

Related

How to use validation controls in asp.net when user will enter URL and Path of Image?

My asp.net web page contain two textboxes one for URL and another for ImagePath
I want URL entered by user in following format
e.g http://articles.site.com in the URL textbox.If user enter URL other than http://articles.site.com this format it should not allowed.Message should display like Enter correct URL.
And for ImagePath entered by user ImagePath textbox like following manner.
e.g http://articles.site.com/ImageServer/Public/2016/July/EnglishBanner/effective-skunk-rinse-recipe.jpg
protected void Upload(object sender, EventArgs e)
{
if (FileUpload1.HasFile)
{
string fileName = Path.GetFileName(FileUpload1.PostedFile.FileName);
FileUpload1.PostedFile.SaveAs(Server.MapPath("~/Images/") + fileName);
Response.Redirect(Request.Url.AbsoluteUri);
}
}
In the Page Load event of the ASP.Net Web Page the files are fetched from the Images folder and then the fetched image files are bound to the ASP.Net GridView control
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
string[] filePaths = Directory.GetFiles(Server.MapPath("~/Images/"));
List<ListItem> files = new List<ListItem>();
foreach (string filePath in filePaths)
{
string fileName = Path.GetFileName(filePath);
files.Add(new ListItem(fileName, "~/Images/" + fileName));
}
GridView1.DataSource = files;
GridView1.DataBind();
}
}
you have use this function for validate url.
private bool IsUrlValid(string url)
{
string pattern = #"^(http|https|ftp|)\://|[a-zA-Z0-9\-\.]+\.[a-zA-Z](:[a-zA-Z0-9]*)?/?([a-zA-Z0-9\-\._\?\,\'/\\\+&%\$#\=~])*[^\.\,\)\(\s]$";
Regex reg = new Regex(pattern, RegexOptions.Compiled | RegexOptions.IgnoreCase);
return reg.IsMatch(url);
}

ASP.NET UserControl get property in inline code

I have a user control, in the page load of the control I am doing this :
if (ViewState["Lib"] != null)
{
cfg.Lib = (string)ViewState["Lib"];
}
This Lib property can be modified with a textbox like this :
protected void Lib_e_Changed(object sender, EventArgs e)
{
cfg.Lib = Lib_e.Text;
ViewState["Lib"] = Lib_e.Text;
}
I have written the following javascript in my ascx file :
alert('<%= cfg.Lib %>');
It will always return the default value even if I have changed the text in my textbox. My textbox is in an update panel and I have set AutoPostBack to true. Is there something I am missing to update my value ?
It is happening because aspx page render
alert('<%= cfg.Lib %>');
before any assign you are performing on
cfg.Lib
to make it workable what you can do is .. register the script from server side like
protected void Lib_e_Changed(object sender, EventArgs e)
{
cfg.Lib = Lib_e.Text;
ViewState["Lib"] = Lib_e.Text;
ScriptManager.RegisterStartupScript(updatePanelId, updatePanelId.GetType(), "AnyKey", "alert('" + cfg.Lib + "')", true);
//ScriptManager.RegisterStartupScript(this, this.GetType(), "AnyKey", "alert('" + cfg.Lib + "')", true);
//Page.ClientScript.RegisterStartupScript(this.GetType(),"AnyKey","alert('"+cfg.Lib +"')",true);
}

Open link in a new window in ajax panel on server side

I have asked this question before, but got no answer that worked.
This is a buttonclick event that should initiate the download:
protected void btnDownload_Command1(object sender, CommandEventArgs e)
{
GridDataItem item = gvClients.Items[Convert.ToInt32(e.CommandArgument)];
GetUserData usr = new GetUserData(item["id"].Text, Security.level.Agent, servermap);
string file = usr.RetrieveContractPath();
SendFileDownload(file);
}
One of the solutions that was offered was opening a link in a new window and have the window on page load initiate the download there with this piece of code:
protected void btnDownload_Command1(object sender, CommandEventArgs e)
{
GridDataItem item = gvClients.Items[Convert.ToInt32(e.CommandArgument)];
GetUserData usr = new GetUserData(item["id"].Text, Security.level.Agent, servermap);
string file = usr.RetrieveContractPath();
// SendFileDownload(file); dont call it here , call it in the other window
string url = "PopupFileDownload.aspx?file="+file;
string s = "window.open('" + url + "', 'popup_window', 'width=300,height=100,left=100,top=100,resizable=yes');";
ClientScript.RegisterStartupScript(this.GetType(), "script", s, true);
}
This did not work. I tried doing something similar since I am using the Telerik Ajax Panel
ajaxPanel.ResponseScripts.Add("window.open('DownLoadPopup.aspx?file='" + file + "'', 'popup_window', 'width=300,height=100,left=100,top=100,resizable=yes');");
But this also did not work. the command was executed with no effect.
How can I send a file to the user without sacrificing the Ajax panel?
If your button's inside an ASP.NET Ajax UpdatePanel you could turn off the ajax for just the download button
ScriptManager.RegisterPostBackControl(btnDownload);
If you're using Telerik Ajax controls, you can use the following code to pop up a RadWindow.
Make sure you've got a RadScriptManager and a RadAjaxManager on your page before your RadAjaxPanel...
Then add a RadWindowManager inside your RadAjaxPanel like this...
<telerik:RadWindowManager runat="server" ID="rwm" Modal="true" Skin="Default" AutoSize="true" />
Then in your code, you can do this...
protected void btnDownload_Command1(object sender, CommandEventArgs e)
{
GridDataItem item = gvClients.Items[Convert.ToInt32(e.CommandArgument)];
GetUserData usr = new GetUserData(item["id"].Text, Security.level.Agent, servermap);
string file = usr.RetrieveContractPath();
rwm.Windows.Clear();
var rWin = new RadWindow();
rWin.ID = "Name of my window";
rWin.NavigateUrl = string.Format("~/DownLoadPopup.aspx?file={0}", file);
rWin.Width = Unit.Pixel(1000);
rWin.Height = Unit.Pixel(600);
rWin.VisibleOnPageLoad = true;
rwm.Windows.Add(rWin);
}
Adjust the path to your DownLoadPopup.aspx and the properties of the RadWindow as necessary.

asp.net C# variable as Image1.ImageUrl property

I would like to Click "buttonImage1" and have that store "photos/Image1.jpg"
into a var called Photo. then have ButtonImage2.ImageUrl = (Photo).
I am using an UpdatePanel to refresh the area with the images so the only problem is actually setting the variable correctly and then using it correctly.
Reworded: how can I store a path to an image in a variable then set it correctly as a image's URL path?
protected void Button2_Click(object sender, ImageClickEventArgs e)
{
Evt = ("~/events/ev2.jpg");
}
protected void Image2_Click(object sender, ImageClickEventArgs e)
{
Image1.ImageUrl = (Evt);
}
use Server.MapPath to set image path
Image1.ImageUrl = Server.MapPath(Evt);

Dynamically Loaded ListButton's and PostBack Event

I am trying to get a dynamically loaded LinkButton to fire a postback event, which I then handle.
I have across Command, CommandName and CommandArgument - which looked real useful.. So I started messing with that, but currently cannot get to work as desired.
The links are rendered within a table that is created on the fly, here is the code to generate them:
// The links are stored in Session since I was told that the objects need to remain intact across requests.
LinkButton GetEditCardLink(string card)
{
if (!IsPostBack)
{
TW("Creating Link for Card '" + card + "' and Placing in Session.", true);
LinkButton link = CreateEditLink(card);
Business.Session.Set<LinkButton>("LinkedCards_EditLink_" + card, link);
}
return Business.Session.Get<LinkButton>("LinkedCards_EditLink_" + card);
}
// Here the link itself is created, note the ID and Command details are set.
LinkButton CreateEditLink(string forCard)
{
TW("Setting Up Link for Card: " + forCard, true);
LinkButton rtn = new LinkButton();
rtn.ID = "Edit_" + forCard;
rtn.Text = Resources.Header("EditDetails");
rtn.CommandName = "Edit";
rtn.CommandArgument = forCard;
rtn.Command += new CommandEventHandler(RedirectToEdit);
rtn.Attributes["style"] = "display: block; text-align:center;";
return rtn;
}
// ... And the delegate I want called on PostBack..
void RedirectToEdit(object sender, CommandEventArgs e)
{
TW("RedirectToEdit Called:\r\nName: " + e.CommandName + "\r\nArgument: " + e.CommandArgument);
}
Trace confirms that the LinkButtons are being loaded correctly, and are only being creating once, so in theory they should be fine, but when I click the link, a PostBack is performed, but the RedirectToEdit method is not called?
All help gratefully received! :)
NOTE
Oh, I thought I should mention TW is just a utility method for Trace.Write/Warn :)
When you create a control that needs event handling you have to do it early enough in the processing that the event handler gets hooked up. Override OnInit for the page where you are creating the table and move the table creation code there. As #ScarletGarden suggests, you also need to add the control whether it's a PostBack or not. I believe that doing it in Page_Load is too late for the event to be detected if you add the control there.
Reference
Here is my trial, and it worked :
protected void Page_Load(object sender, EventArgs e)
{
//if (!IsPostBack)
//{
placeHolderAtPage.Controls.Add(CreateEditLink("forCard1"));
//}
}
LinkButton CreateEditLink(string forCard)
{
LinkButton rtn = new LinkButton();
rtn.ID = "Edit_" + forCard;
rtn.Text = "EditDetails";
rtn.CommandName = "Edit";
rtn.CommandArgument = forCard;
rtn.Command += new CommandEventHandler(RedirectToEdit);
rtn.Attributes["style"] = "display: block; text-align:center;";
return rtn;
}
void RedirectToEdit(object sender, CommandEventArgs e)
{
Response.Write("RedirectToEdit Called:\r\nName: " + e.CommandName + "\r\nArgument: " + e.CommandArgument);
}
If you decomment the IsPostBack line, RedirectToEdit will be useless.
Only binding codes can be under IsPostBack control.

Resources