I've searched for almost 2 days on the internet to find solution but nothing works yet.
I have 2 user controls on a page. First contains AsyncFileUpload:
<cc1:AsyncFileUpload runat="server" ID="fuExcelUploader" Width="400px"
UploadingBackColor="#CCFFFF" ThrobberID="myThrobber"
CompleteBackColor="#CEF6CE" />
and the second has a gridview with such templatefield with download button(excel file)
<asp:TemplateField HeaderText="Report with errors" ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:LinkButton id="lbError" CommandName="ErrorClick" runat="server" CommandArgument='<%# Eval("Report.Id") %>' ValidationGroup="other2357"><asp:Image ID="imgReport" runat="server"
ImageUrl="~/App_Themes/Default/Images/icons/page_excel.png" ImageAlign="Middle" Visible='<%# Convert.ToInt32(Eval("Report.Id")) > 0 %>' /></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
In the RowCommand if e.CommandName = ErrorClicked I have such piece of code for downloading file
Response.Clear();
Response.Buffer = true;
Response.AddHeader(
"Content-Disposition", string.Format("attachment; filename={0}", "Error_report_" + this.ErrorClicked + ".xlsx"));
HttpContext.Current.Response.ContentType = "application/vnd.openxmlformats";
// Response.Cache.SetCacheability(HttpCacheability.Private);
Response.BinaryWrite(value); //value is byte[];
Response.End();
It works perfect I can upload files with asyncfileupload then download reports by clicking icons on gridview etc. , but there is one problem.
Whenever I click on download icon on gridview, the download file dialog pops up, I can save/open/cancel but whatever I do, after I try to upload new file with asyncfileupload the same RowCommand event is fired with same 'ErrorClick' CommandName and CommandArgument, so I am getting that window with file to download again (and page is locked). It might be because neither linkbutton nor asyncfileupload refresh whole page (is it the same postback?).
Do you have any idea why the rowcommand is fired during uploading with asyncfileupload control or how to solve that problem. I don't use udpatepanels in this case.
The OnRowComand event is fired when you click the Linkbutton and after processing the upload. An error occurs by registering twice the download code.
The script of the "href" in the LinkButton somehow influences this result. I used a ImageButton to trigger the download, and the LinkButton to execute the event click of ImageButton.
ASPX
the ImageButton -> Style="display: none;"
ASPX.CS - RowDataBound
LinkButton.Attributes.Add("OnClick", "$(document.getElementById('" + ImageButton.ClientID + "')).click(); return false;");
Sorry, my English is horrible. I use google Translate.
Related
I am using this code on button click from which I can download a file with a specific name.
But I want, when the user has uploaded a file, in his details like any Id proof details file.
Now to verify the user admin want to see the his Id proof details.
So he will download the file which user had uploaded and that is saved in database.
So the file can be in any type or extension.
private void Button1_click(object sender, System.EventArgs e)
{
string filename="C:\myuploads\invoice.pdf";
Response.ContentType = "Application/pdf";
Response.AppendHeader("Content-Disposition", "attachment;" + filename +);
Response.TransmitFile(Server.MapPath(filename));
Response.End();
}
I think this is what you're after.
private void DownloadFile(string file)
{
var fi = new FileInfo(file);
Response.Clear();
Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Disposition", "attachment; filename="+ fi.Name);
Response.WriteFile(file);
Response.End();
}
So you just call it like this:
string myfile = #"c:\path\To\Files\myFile.pdf"; //this wouldn't be a static string in your code
DownloadFile(myfile);
Thanks for answering my questions. My LinkButton to downloading the file from database on particular user Id worked Successfully.
Actually My Download Link was in update panel so It needs the "Trigger" for this.. And My Link was in GridView So I had Passed the Link Button Id in Trigger.
Whenever we use GridView in Update Panel and There is a LinkButton to Download the File from database on particular user Id. We Should Pass **GridView Id not the LinkButton Id in Template Field.**
<asp:UpdatePanel ID="upd" runat="server">
<ContentTemplate>
<asp:GridView ID="grd_UserList" runat="server" CssClass="table"
DataKeyNames="Uid" AutoGenerateColumns="false" AllowPaging="false">
<asp:TemplateField HeaderText="Task Name">
<ItemTemplate>
<asp:LinkButton Id="LinkDownload" runat="Server" CommandArgument='<%# Eval("Attachment") %>' >
</ItemTemplate>
</asp:TemplateField>
</asp:GridView>
</ContentTemplate>
<Triggers>
<asp:PostBackTrigger ControlID="grd_UserList" />
</Triggers>
</asp:UpdatePanel>
When the user uploads the file, you should be able to use the ContentType property of the posted file. This is assuming you used a file upload control like:
<asp:FileUpload ID="uploadFile" runat="server" />
When the file is uploaded, get the content type using
string fileType = uploadFile.PostedFile.ContentType
Save that value in your DB, and use it as the value of Reponse.ContentType in your existing code, when you download it later.
I create a link button and put an image on it.
Here is my code:
<asp:LinkButton ID="LinkButton1" runat="server" Text="">
<asp:Image ID="Image1" ImageUrl="" runat="server" />
</asp:LinkButton>
And here is my subsequent C# code:
if(!Page.IsPostBack)
{
LinkButton1.OnClientClick = "ClientClick()";
Image1.ImageUrl = "~/Images/embed.png";
}
I provide the ImageUrl from the c# code behind for the Image and add an OnClientClick event from c# code for the Link Button. When the page first load then the button showed properly.
My browser rendered HTML before post back
<a onclick="ClientClick();" id="MainContent_LinkButton1"
href="javascript:__doPostBack('ctl00$MainContent$LinkButton1','')">
<img id="MainContent_Image1" src="Images/embed.png">
</a>
When I press any button in this page and PostBack occurs, then the button disappears but I don't do anything in my code behind for that button for PostBack.
My browser rendered HTML after post back
<a onclick="ClientClick();" id="MainContent_LinkButton1"
href="javascript:__doPostBack('ctl00$MainContent$LinkButton1','')">
</a>
If I do not add OnClientClick or Text for the Link Button then the image does not disappear.Or if I set ViewState false for link button then image does not disappear. So why this button disappear when page is PostBack?
With EnableViewState="true" this code works correct even with post back and the image is shown
<asp:LinkButton ID="LinkButton1" runat="server" Text="">
<asp:Image ID="Image1" ImageUrl="" runat="server" />
</asp:LinkButton>
if(!Page.IsPostBack)
{
LinkButton1.OnClientClick = "ClientClick()";
Image1.ImageUrl = "~/Images/embed.png";
}
With EnableViewState="false" You must remove the IsPostBack because is not saved anywhere and you need to add it again
// remove that check if EnableViewState is false
//if(!Page.IsPostBack)
{
LinkButton1.OnClientClick = "ClientClick()";
Image1.ImageUrl = "~/Images/embed.png";
}
This occurs because Postback clears all the control of your asp.net page.
To retain controls from previous load, you need to make sure that whatever code is in Page_Load() is re-executed on "Button_click" which causes postback.
void button_click()
{
// do stuff
page_load(Dummy Object);
}
Why My Event as link button event or item command not fired until i post back page with another control in page.
for example in this code:
<div id="pagination">
<span class="all" runat="server" id="CurrentPage">Total Pages</span>
<asp:Repeater ID="RPTPaging" runat="server" Visible="false" OnItemDataBound="RPTPaging_ItemDataBound" OnItemCommand="RPTPaging_ItemCommand">
<ItemTemplate>
<asp:LinkButton ID="BtnPage" CssClass="inactive" CommandName="Page" CommandArgument="<%# Container.DataItem %>" runat="server" Text="<%# Container.DataItem %>"></asp:LinkButton>
</ItemTemplate>
</asp:Repeater>
</div>
related to this question
Why Repeater ItemCommand Doesn't fire, even if i don't rebind by post back?
i found when i click on link button in this repeater nothing happened,but after i click on empty input button on page, after post back a page changed corectlly.
Dear Expert After 2 day finally i figure out why my page doesn't post back.
after i tried to post back my button manually with java script and i failed. i tried to trace the script and i found this Error with firebug:
TypeError: theForm.submit is not a function
theForm.submit();
when i google it i found i have a button in my MasterPage with id="Submit" and as
"submit is not a function" means that you named your submit button or some other element submit. Rename the button to btnSubmit and your call will magically work.
When you name the button submit, you override the submit() function on the form.
So This Problem Resolved. Thanks to all.
I'm having an issue where the button within a radgrid is not firing until second time.
I have a usercontrol with a radgrid on it and button within one of its column.
The usercontrol is placed on a page.
When clicking the button on radgrid nothing happens 1st time, however it works 2nd time.
This is some of the radgrid column data
<telerik:GridTemplateColumn DataField="Quantity" HeaderText="Quantity" UniqueName="QuantityCol">
<ItemTemplate>
<asp:TextBox ID="Quantity" runat="server" Columns="4" Text='<%# DataBinder.Eval(Container.DataItem, "Quantity") %>' width="40px" />
<asp:LinkButton id='btnUpdateRow' runat="server" CausesValidation="false" Text='<span>Update</span>' CommandName="ButtonUpdateRow" CommandArgument='<%# DataBinder.Eval(Container, "RowIndex") %>'/>
</ItemTemplate>
</telerik:GridTemplateColumn>
The radgrid uses NeedDataSource to obtain its data.
Page Load does not do anything.
During debug within itemcreated/itemdatabound, the linkbutton clientid it shows
TestBasket_RadGrid1_ctl01_ctl04_btnUpdateRow
However, when rendered to the browser it shows as
<a id="TestBasket_RadGrid1_ctl01_ctl09_btnUpdateRow" href="javascript:__doPostBack('TestBasket$RadGrid1$ctl01$ctl09$btnUpdateRow','')">
Clicking the button - itemcommand is NOT fired.
On returning from postback the brower shows
<a id="TestBasket_RadGrid1_ctl01_ctl04_btnUpdateRow" href="javascript:__doPostBack('TestBasket$RadGrid1$ctl01$ctl04$btnUpdateRow','')">
Clicking the button - itemcommand is fired.
Anyone explain why clientid gets changed.
I have tried placing a placeholder and creating the control with an id in itemcreated - still same issue.
Thanks in advance for any help.
Looks like the issue happened because of page inheritance.
I made a simple aspx page not using any of our architecture and it seemed to work fine.
I then bought the code back in and it stopped working.
I’ve managed to fix it by changing the page inheritance of the pages that use Telerik controls.
I have an ImageButton within a GridView in .aspx on clicking this ImageButton i have to call a function.
This is how i tried and the function was not being called.
Code inside.aspx page:
<GridView ......>
<asp:HyperLink ID="HyperLink2" runat="server"
NavigateUrl='<%# DataBinder.Eval(Container.DataItem,"VehID","mngVeh.aspx?delid={0}") %>'>
<asp:ImageButton runat="server" ID="DeleteUrlImageButton"
width='24' height='24'
ImageUrl="~/images/delete.jpeg"
OnClick="DeleteUrlImageButton_Click"
OnClientClick="return confirm('Are you sure you want to delete?');" />
<!--<img src="images/delete.jpeg" alt='edit' border="0" width='24' height='24'/> -->
</asp:HyperLink>
</GridView>
code in .aspx.cs page:
public void DeleteUrlImageButton_Click(object sender, EventArgs e)
{
//code to perform the necessary action.
}
Because you are wrapping your ImageButton inside of a Hyperlink, the browser is probably going to the hyperlink's URL instead of posting back to hit the OnClick function. You should have the DeleteUrlImageButton_Click function call Server.Transfer or Response.Redirect to the appropriate URL and get rid of the Hyperlink.
Sure it won't be fired because it is nested in a Hyperlink. So the imagebutton serves as the text for the hperlink and hyperlink does not cause postback. ImageButton can cause the desired action only if it stands out of the Hyperlink. Try this:
<asp:GridView ....
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:ImageButton runat="server" ID="DeleteUrlImageButton"
width='24' height='24'
ImageUrl="~/images/delete.jpeg"
OnClick="DeleteUrlImageButton_Click"
OnClientClick="return confirm('Are you sure you want to delete?');"
PostBackUrl='<%# DataBinder.Eval(Container.DataItem,"VehID","mngVeh.aspx?delid={0}")
%>'/>
</ItemTemplate>
</asp:TemplateField>
</asp:GridView>
ImageButton can do the job no need for Hyperlink just use the postbackurl and it will redirect you to the page. You can omit the HyperLink.
Button controls (like LinkButton,ImageButton and Button) are designed to cause postback by default.
Edit: Make sure event name and arguments are correct. This is the event I used to test it. y the way don't forget to place the ImageButton in a TemplateField, refer the code above
protected void DeleteUrlImageButton_Click(object sender, ImageClickEventArgs e)
{
TextBox5.Text = "Fired ";
//Response.Redirect( ((ImageButton)sender).PostBackUrl);//uncomment this if the button does not automatically redirects. This line should be the last one
}