Exporting Data from Telerik RadGrid to Ms-Word - c#

I'm trying to export a Telerik RadGrid to Word:
protected void DownloadWord_Click(object sender, EventArgs e)
{
aGrid.ExportSettings.Word.Format = GridWordExportFormat.Docx;
aGrid.ExportSettings.ExportOnlyData = true;
aGrid.ExportSettings.FileName = "test.docx";
aGrid.MasterTableView.ExportToWord();
Page.Response.ClearContent();
Page.Response.ClearHeaders();
}
When I click the button, the above code fires and I get a page refresh. Looking at the POST headers and response it is the same as for loading the page, so it seems to only refresh the page.
What's wrong?

The RadGrid writes the exported file into the response of the page. And since you are clearing the response and its headers, you get no file.
For your case, Only this is required:
protected void ImageButton3_Click(object sender, System.Web.UI.ImageClickEventArgs e)
{
aGrid.ExportSettings.Word.Format = Web.UI.GridWordExportFormat.Docx;
aGrid.ExportSettings.FileName = "test.docx";
aGrid.ExportSettings.ExportOnlyData = true;
aGrid.MasterTableView.ExportToWord();
}
I cant understand the use of:
Page.Response.ClearContent();
Page.Response.ClearHeaders();
Unless you have added it for some specific purpose.
See here for demo from telerik on using Export functionalities.

Related

Changing label text on button click that also executes other methods

I am trying to update a label on my windows form with statistics from another methods execution that scrapes a webpage and creates a zip file of the links and creates a sitemap page. Preferably this button would run the scraping operations and report the statistics properly. Currently the scraping process is working fine but the label I want to update with statistics data is not changing on the button click. Here is what my code looks like now:
protected void btn_click(object sender, EventArgs e)
{
//Run scrape work
scrape_work(sender, e);
//Run statistics work
statistics(sender, e);
}
protected void scrape_work(object sender, EventArgs e)
{
//Scraping work (works fine)
}
protected void statistics(object sender, EventArgs e)
{
int count = 0;
if (scriptBox.Text != null)
{
count += 1;
}
var extra = eventsBox.Text;
var extraArray = extra.Split('\n');
foreach (var item in extraArray)
{
count += 1;
}
//scrapeNumLbl is label I want to display text on
scrapeNumLbl.Text = count.ToString();
}
Would I have to use threading for this process or is there some other way I can get this process to work? I have already tried this solution but was having the same issue where the code runs but the label does not update. Any help would be greatly appreciated, this minor thing has been bugging me for hours now.
I eventually solved this by writing the path to the zip file to a label button on the form rather than sending it right to download on the client's browser on button click. The issue was that the request was ending after the zip file was sent for download. To ensure that both methods ran at the proper time I moved the call to the scrape_work method to inside of of statistics
In order for the path to be clickable and the file to download properly I had to make the "label" in the form a LinkButton in the .aspx page
<asp:LinkButton ID="lblDownload" runat="server" CssClass="xclass" OnClick="lblDownload_Click"></asp:LinkButton>
And made the lblDownload_Click run like the following:
Response.Clear();
Response.BufferOutput = false;
Response.ContentType = "application/zip";
Response.AddHeader("content-disposition", "attachment; filename=zipFiles.zip");
string folder = Server.MapPath("~/zip");
string endPath = folder + "/zipFiles.zip";
Response.TransmitFile(endPath);
Response.End();
Running it this way the page reloads with the new labels properly written and the zip file available to download as well.
ASSUMING THIS CODE IS RUNNING SYNCHRONOUSLY (you aren't threading the scraping in a call I don't see), Are you sure you are reaching the code that sets the label text? I simplified your code as below (removing the iteration over the array and just setting the label text to a stringified integer) and am not having any trouble changing the text of a label.
namespace SimpleTest
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
scrape_work(sender, e);
statistics(sender, e);
}
protected void scrape_work(object sender, EventArgs e)
{
//Scraping work (works fine)
}
protected void statistics(object sender, EventArgs e)
{
int count = 666;
scrapeNumLbl.Text = count.ToString();
}
}
}
Result:

How to Refresh GridView after closing down Dialog in ASP .NET

I have a main page with a Gridview that shows data from a Database. In the gridview there is a button, when this button is clicked a Dialog will appear allowing u to edit the selected row. The dialog is created as an aspx class. When i edit the data and close down the dialog i want to refresh my GridView on the Main page so the edited data is represented. How can i do this?
my code for editing the data and closing down the dialog is this:
protected void editButton_Click(object sender, EventArgs e)
{
string alias = Request.QueryString["alias"];
string custid = Request.QueryString["custid"];
controller.EditDeliveries(custid, alias, tdaysField.Text, thoursField.Text, ttypeField.Text, pdaysField.Text, phoursField.Text, ptypeField.Text);
ClientScript.RegisterClientScriptBlock(GetType(), "onUpload", "<script type='text/javascript'> window.close();</script>");
}
Can anyone help ?
And if u need to see more code please tell me.
Just set your datasource again and rebind it in the postback
gvMyGrid.DataSource = myData; //Fresh from the database
gvMyGrid.DataBind(); //Bam, fresh data
Edit: Oh and if it's another Control that is the source of the postback, you can use a Bubble Event to trigger the refresh.
Second Edit: To have the Dialog Page tell the Main Page to update that grid:
RaiseBubbleEvent(this, new CommandEventArgs("DataUpdated", "This could be null, or I could be a message to let the user know things are updated"));
Then on your main Page
protected override bool OnBubbleEvent(object sender, EventArgs e)
{
if (e is CommandEventArgs)
{
var args = (CommandEventArgs)e;
//Could use args.CommandArgument here
switch(args.CommandName)
{
case "DataUpdated":
gvMyGrid.DataSource = myData;
gvMyGrid.DataBind();
return true; //Handled the event LIKE A BOSS
}
}
return false; //I didn't handle this event
}

Getting the HTML content of the current page with databound controls

I'm trying to create a control that converts the page it's on into a PDF.
protected void ConvertPageToPDF_click(object sender, EventArgs e)
{
string pageHtml;
byte[] pdfBytes;
string url = HttpContext.Current.Request.Url.AbsoluteUri;
// get the HTML for the entire page into pageHtml
HtmlTextWriter hw = new HtmlTextWriter(new StringWriter());
this.Page.RenderControl(hw);
pageHtml = hw.InnerWriter.ToString();
// send pageHtml to a library for conversion
// send the PDF to the user
}
This partially works. On my pages I have several repeaters; their content does not show up in pageHtml. Any thoughts on why that is? How should I fix this?
It turns out that I had to render the control after the data had been bound to it. My click method just adds an event handler:
protected void ConvertRepeaterToPDF_click(object sender, EventArgs e)
{
// handle the PreRender complete method
Page.PreRenderComplete += new EventHandler(Page_OnPreRenderComplete);
}
Then in Page_OnPreRenderComplete I can use RenderControl as above.

Make a .net WebForm dynamic without autopostback

Here's my issue, I'm using visual studios 2010 and I'm trying to create a form to fill then send it. This form requires a fileupload and some informations. The problem is that when I want to show/hide some pannel or textbox I can't do it dynamicly and I have to use autopost back which unselect the file selected in the fileupload control :
protected void CB_image_CheckedChanged(object sender, EventArgs e)
{
if (CB_image.Checked)
{
PanelImage.Visible = true;
}
else
{
PanelImage.Visible = false;
}
}
This is an exemple of code i'd like to pull out without using autopostback property on my CB_image (CB = checkbox). Any help would be appreciated, thanks
You can't do that. Something has to trigger a postback so the code on the server-side can check whether the checkbox is checked or not, and act according to it.
You can use a javascript timer that initiates a postback and work that way, I wouldn't do that though.
btw,
protected void CB_image_CheckedChanged(object sender, EventArgs e)
{
PanelImage.Visible = CB_image.Checked;
}
looks much cleaner!

How to getting data from Excel or PDF or Word in Asp.net 2010 using RADGrid Control?

i am getting error when i upload RADGrid Control on server it run properly but when i export data into PDF or Word or Excel, not display data only display html tags like this
<span class=highlight></span>M<span class=highlight></span>M<span class=highlight></span>/<span class=highlight></span>P<span class=highlight></span>I<span class=highlight></span>/<span class=highlight></span>G<span class=highlight></span>U<span class=highlight></span>D<span class=highlight></span>M<span class=highlight></span>/<span class=highlight></span>8<span class=highlight></span>
I'm using code like this:
protected void ExportToPdf_Click(object sender, ImageClickEventArgs e)
{
RadGrid1.MasterTableView.ExportToPdf();
}
protected void ExportToExcel_Click(object sender, ImageClickEventArgs e)
{
RadGrid1.MasterTableView.ExportToExcel();
}
protected void ExportToWord_Click(object sender, ImageClickEventArgs e)
{
RadGrid1.MasterTableView.ExportToWord();
}
i attached one sample image where you can check your exported pdf's html.
please check and let me know if any concern.

Categories