Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

clicking image button causes clears page

Status
Not open for further replies.

ralphtrent

Programmer
Jun 2, 2003
958
US
Hello
I have a datagrid that I want to export to an excel file when the user clicks an image button. This works great, execept when I click the button, I need to rebind the datagrid. How do I avoid that. The datagrid is already bound and display, I just want to take that and save it to a file. I should say that this simple task works fine even though I need to rebind it, but I want to add an enhancement that will allow the users to click a checkbox on the datagrid header (one check box per column) to select the columns they want to export. I have teh checkbox there, but like i said when I click my image button, the datagrid is null'd as are the check box options.

Here is some code
Code:
protected void imgbtnExportToExcel_Click(object sender, ImageClickEventArgs e)
{
[tab]exportData(sender);
}


Code:
private void exportData(object sender)
{
[tab]if (sender.GetType().Name.EndsWith("ImageButton"))
[tab]{
[tab][tab]DataTable dt = new DataTable();
[tab][tab]TimeSpan ltsTimeToSpin, timeToDataBind, timeToCheckSLGs;
[tab][tab]if (Session["lastSerializedDataFile"] != null)
[tab][tab]{
[tab][tab][tab]dt.ReadXml(Convert.ToString(Session["lastSerializedDataFile"]));
[tab][tab][tab]displayResults(dt, "", out ltsTimeToSpin, out timeToDataBind, out timeToCheckSLGs);
[tab][tab][tab]dt = null;
[tab][tab][tab]if (((ImageButton)sender).ID.IndexOf("excel", StringComparison.CurrentCultureIgnoreCase) > -1)
[tab][tab][tab]{
[tab][tab][tab][tab]displayData.exportDataGridResults(dgResults, displayData.ExportType.Excel);
[tab][tab][tab]}
[tab][tab][tab]if (((ImageButton)sender).ID.IndexOf("word", StringComparison.CurrentCultureIgnoreCase) > -1)
[tab][tab][tab]{
[tab][tab][tab][tab]displayData.exportDataGridResults(dgResults, displayData.ExportType.Word);
[tab][tab][tab]}
[tab][tab]}
[tab]}
}
 
I might also add that the code I show is in a user control which is housed inside an aspx page that has a master file.
 
I would assume you need to bind the grid since each request is unique. viewstate is a hack around this, but i wouldn't depend on this. it just adds bloat to the rendered output.

I suggest using the button command event rather than the button click event. doing so will also you to create cleaner code. right now the "export" method is working against the concepts of OOP and encapsulation.

Jason Meckley
Programmer

faq855-7190
faq732-7259
 
Thanks JMeckleym, but what does your suggestion mean exactly, use the button command event.

Thanks,
Ralph
 
instead of the click event use the command event and set the command arg on the button. you can then use the command arg to determine which operation to execute.

Jason Meckley
Programmer

faq855-7190
faq732-7259
 
OK I implemented this code as
Code:
<asp:ImageButton id="btnImgExcel2" runat="server" ImageUrl="../images/gif/excel_logo.gif" CommandName="Export" CommandArgument="excel" OnCommand="exportData2" />

protected void exportData2(Object sender, CommandEventArgs e)
{
[tab]switch (e.CommandName)
[tab]{
[tab][tab]case "Export":
[tab][tab][tab]displayData.exportDataGridResults(dgResults,TriZetto.Support.GlobalSolutions.WebApplication.Exports.ExportTo.Excel);
[tab][tab][tab]break;
[tab][tab]}
}

but the datagrid's datasource still gets reset before I get into this method. Should I still exected that regardless of what I am doing?

Thanks again.
 
that's probably related to the page life cycle then.

Jason Meckley
Programmer

faq855-7190
faq732-7259
 
Any asp server button will cause a postback, therefore, your grid will have to be rebound each time the button is clicked. If you want to avoid the postback, the you will have to impliment a javascript or ajax solution.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top