Link to home
Start Free TrialLog in
Avatar of zintech
zintech

asked on

SharePoint button not executing code when clicked

I have a button in SharePoint 2007.  When I click it, I want inline C# I have written to execute.  I set the OnClientClick property set to the method name of the method I want to run which is ExportToExcel().  When I click the button, the page like refreshes but seemingly without my code ever being called.  I was just wondering if I had done something incorrectly.  The code for the button is:

<asp:Button runat="server" Text="Export to Excel" id="ExporttoExcelButton" onclientclick="C#:ExportToExcel"/>

And the code for the method is:

<script type="text/c#" runat="server">
  public void ExportToExcel()
  {
 Response.Clear();
Response.Buffer = true;
Response.AddHeader("content-disposition",
"attachment;filename=GridViewExport.xls");
Response.Charset = "";
Response.ContentType = "application/vnd.ms-excel";
System.IO.StringWriter sw = new System.IO.StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
GridView1.AllowPaging = false;
GridView1.DataBind();
GridView1.RenderControl(hw);
Response.Output.Write(sw.ToString());
Response.Flush();
Response.End();
   }
  </script>
Avatar of chapmanjw
chapmanjw
Flag of United States of America image

Where are you putting this inline code?

For the most part, you cannot use inline managed code because SharePoint renders the code after the .NET code is executed, so nothing actually executes this code. (You cannot use this in a Content Editor Web Part or in a SharePoint Designer).

The only place that you can really put in custom .NET code is from Visual Studio.
Avatar of zintech
zintech

ASKER

I am placing it inside of the <HEAD runat="server"> tag in SharePoint 2007.  I was under the impression that it is possible to do inline code, but you may be right.  In the event that I cannot use inline code, I would not know of a solution to try and accomplish the task I am trying to do, export a Gridview to Excel
Use jQuery.  Try something like http://www.kunalbabre.com/projects/table2CSV.php.

GridViews are HTML tables, you can use the client side to export the data to CSV, which Excel can then open.
You can run inline code in the form of <% %> by adding the following in the correct web config file.

<PageParserPaths>

  <PageParserPath VirtualPath="/pages/test.aspx" CompilationMode="Always" AllowServerSideScript="true" />

</PageParserPaths>

The above allows for code to run for one page. See below for more than one page:

 To specify all files in a path (Document Library) the path statement can include a wild card (*) in which case you can also add IncludeSubFolders="true" parameter to the PageParserPath to allow inline code for any page.

Avatar of zintech

ASKER

rebejones - I did add that to the web.config file already.  Whenever I clilck on the Button no matter what the code is I see in the lower left hand corner of the page a yellow exclamation mark meaning there is an error and then the whole page refreshes.  I don't see what I am doing wrong.  I can't even get a simple alert box in javascript to show up
Have you tried running the code outside of SharePoint to see if it works?
ASKER CERTIFIED SOLUTION
Avatar of rebejones
rebejones

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial