Export to Excel control derived from GridView - c#

I have a control that is derived from the GridView class. The BottomPagerRow is always visible. Overriding InitializePager, I added an UpdatePanel and a LinkButton (Click event points to function to convert data) inside the UpdatePanel.
var download = new LinkButton { Text = "Open in Excel", CssClass = "fakeButton", ID = "DownloadToExcel" };
download.Click += DownloadExcelFile;
var up = new UpdatePanel() { ID = "ExcelUpdatePanel" };
var trigger = new PostBackTrigger { ControlID = download.ID };
up.Triggers.Add(trigger);
up.ContentTemplateContainer.Controls.Add(download);
row.Cells[0].Controls.Add(up);
When I click the LinkButton in a page using the control I see the following client side Exception in the web browser,
Uncaught Sys.WebForms.PageRequestManagerParserErrorException:
Sys.WebForms.PageRequestManagerParserErrorException: The message
received from the server could not be parsed.
From my understanding, I think this is because I am using Response.Write in DownloadExcelFile(). However, I thought according to this link that adding the PostBackTrigger would avoid this. I have also replaced the PostBackTrigger generation in the InitializePager event with
ScriptManager.GetCurrent(Page).RegisterPostBackControl(download);
However, the result remains the same, some times it works and other times I see the exception. The exception is visible in a page similar to the following:
<%# Page Title="" Language="C#" MasterPageFile="~/Site.Master"
AutoEventWireup="true" CodeBehind="somePage.aspx.cs" Inherits="somePage" %>
<asp:Content ID="SomeContent" ContentPlaceHolderID="MainContent" runat="server">
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
Some other junk...
</ContentTemplate>
</asp:UpdatePanel>
<asp:UpdatePanel runat="server" ID="SomePanel" UpdateMode="Conditional">
<ContentTemplate>
<asp:CustomGridView ID="SomeCustomGridView" runat="server" AutoGenerateColumns="False"
DataSourceID="SomeSqlDataSource" AllowPaging="True" AllowSorting="True">
<Columns>
<asp:BoundField DataField="SomeField" />
</Columns>
</asp:CustomGridView>
<asp:SqlDataSource ID="SomeSqlDataSource" runat="server" ConnectionString="<%$ ConnectionStrings:SomeConnectionString %>"
SelectCommand="<%$ Resources: SomeResource, Query %>">
<SelectParameters>
<asp:QueryStringParameter Name="SomeId" QueryStringField="SomeId" />
</SelectParameters>
</asp:SqlDataSource>
</ContentTemplate>
</asp:UpdatePanel>
</asp:Content>
The master page that this child page uses includes a ToolkitScriptManager. I am using .NET Framework 4. If anyone has input on how to avoid this exception I would appreciate it. Not sure if I'm adding the trigger in the wrong part of the control's lifecycle, or just completely missed the point of the linked article. Thanks.

Related

Could not find UpdatePanel with ID on page postback

I have below structure in my asp.net application.
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<div id="overlay"></div>
<asp:Panel ID="pnlMain" runat="server" Visible="true">
<asp:UpdatePanel ID="upProfile" runat="server" UpdateMode="Always">
<ContentTemplate>
<div>
<table>
</table>
</div>
</ContentTemplate>
</asp:UpdatePanel>
<asp:UpdatePanel ID="upAttachment" runat="server" UpdateMode="Always">
<ContentTemplate>
</ContentTemplate>
</asp:UpdatePanel>
</asp:Panel>
<asp:Panel ID="pnlSuccessMessage" runat="server" >
<asp:Label ID="lblSuccessMessage" runat="server"></asp:Label>
</asp:Panel>
I have different textfields and dropdowns in the table of upProfile updatepanel and file upload control in upAttachment updatepanel. Now when I select one radio from the same update panel, I am getting below error and the upAttachment does not show up on page.
Uncaught Error: Sys.InvalidOperationException: Could not find UpdatePanel with ID 'MainContent_upAttachment'. If it is being updated dynamically then it must be inside another UpdatePanel.
I have searched a lot and tried below different things, but still it is not working
1. Set the upAttachment UpdateMode to Conditional.
- In this case, it does not throw error, but the file upload control which is inside that updatepanel, it does not show up
2. Place the upAttachment updatepanel inside another updatepanel, but that too is not working.
I don't know what to do in this case. Can anyone help me on this?

Could not access controls in codebehind page

I declared Linkbutton control in Gridview but in code behind i could not access that. Below is my aspx page code.
<%# Page="" Language="C#" AutoEventWireup="true" MasterPageFile="~/MainMaster.Master"
CodeBehind="Page.aspx.cs" Inherits="IntakeLibrary.Page" %>
<%# Register="" Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="asp" %>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<asp:UpdatePanel ID="updatePanel" runat="server">
<contenttemplate>
<asp:GridView ID="grdView" runat="server">
<Columns>
<asp:TemplateField HeaderText="Text">
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server">LinkButton</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</contenttemplate>
</asp:UpdatePanel>
</asp:Content>
Below is my codebehind code.
protected void Page_Load(object sender, EventArgs e)
{
LinkButton1.Text = "Test";
}
Below is the error i am getting
The name 'LinkButton1' does not exist in the current context
You don't have a LinkButton on your page - you have a LinkButton in the ItemTemplate in one column of a GridView in the template of your UpdatePanel. You'll need to reach down past all those layers before you'll be able to reference the LinkButton itself.
You are using the wrong method, the correct way is to assign the text in the button control tag itself. This button is inside a gridview so it will be repeated. You cannot reference suppose 10 rendered buttons with one property right? You have to loop through and change their text on row data bound event.
You would probably have to use a DataGrid_RowDataBound event handler and get a handle to the control in the correct template type and then on the item passed into the event handler do a find control on the link button. I'll get a code example up shortly.

Accessing child control in ASP.NET

I am trying to find out if it's possible to have a control inside another control in ASP, like this:
<asp:FormView ID="FormView1" runat="server" Width="630px" Height="496px">
<ItemTemplate>
<asp:Literal ID="ID" runat="server">Idnumber: </asp:Literal><%#Eval("ID") %>
<asp:DataList ID="DataList1" runat="server">
<ItemTemplate>
<asp:HyperLink ID="ID" runat="server"> <%# Eval("FILE") %> </asp:HyperLink>
</ItemTemplate>
</asp:DataList>
</ItemTemplate>
</asp:FormView>
Can I access DataList1 control? I have been trying, but I can't figure it out, I should be able to access nested controls, but I cant get it to do it.
Controls inside Template tags cannot be directly accessed in code behind. Instead you should use FindControl method:
var dataList1 = (DataList)FormView1.FindControl("DataList1");
Note that this might not work in early stages of page life cycle (not until Page_Load I believe).

UpdatePanel cause error: PageRequestManagerServerErrorException

I have a page that works fine with multiple a grid and multiple buttons. The page works fine until I add an asp:UpdatePanel. Then I get the following message pushing any of my buttons:
Sys.WebForms.PageRequestManagerServerErrorException: Input string was not in a correct format.
There is no javascript on the page just straight html.
Here is the page:
<%# Page Title="" Language="C#" MasterPageFile="~/MasterPages/Site.Master" AutoEventWireup="true"
CodeBehind="TestUpdatePanel.aspx.cs" Inherits="ASCWeb.TestUpdatePanel" %>
<asp:Content ID="mHeadContent" ContentPlaceHolderID="HeadContent" runat="server">
</asp:Content>
<asp:Content ID="mBodyContent" ContentPlaceHolderID="MainContent" runat="server">
<asp:UpdatePanel ID="pnlUpdate" runat="server">
<ContentTemplate>
<asp:TextBox ID="txtUser" runat="server" />
<asp:ImageButton ID="btnAdd" runat="server" ImageUrl="~/Images/Add.png" />
</ContentTemplate>
</asp:UpdatePanel>
</asp:Content>
If I take the TextBox out, it works fine. Nothing is in the code behind.
What would cause this?
Thanks
As per experience, I only encounter that exception when calling Javascript from codebehind, like when using ScriptManager.RegisterClientScriptBlock() to call window.alert(), for instance. But for this issue, I think this resolves it: http://forums.asp.net/t/1823287.aspx/2/10.

Is it normal for Page_Load to trigger upon clicking a page of a GridView inside an UpdatePanel?

I have this code in my aspx page:
<form id="form2" runat="server">
<asp:ScriptManager ID="ItemsScriptManager" runat="server" EnablePartialRendering="true" />
<asp:Button runat="server" ID="SearchButton" OnClick="ItemsSearch" Text="Search" />
<asp:UpdatePanel runat="server" ID="ItemsUpdatePanel">
<ContentTemplate>
<asp:ObjectDataSource runat="server" ID="ItemsDS"
TypeName="TemplateGridViewODSPagingSorting.ItemDAO" SelectMethod="GetItems" />
<asp:GridView runat="server" ID="ItemsGridView" DataSourceID="ItemsDS"
AllowPaging="true" AllowSorting="true" PageSize="4">
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>
</form>
By pressing on another page of the GridView the Page_Load is triggered, is this normal behavior for a partial postback?
Partial rendering using UpdatePanel does not change or affect the whole Page life cycle in ASP.NET.
it's a small trick used to re-render only a certain region of the page in the browser (the UpdatePanel) but nothing else change, so yes, it's normal to see Page_Load and all other events to be triggered as usual; it has to be like that or it would not work :)
Yes, the during update panel update, the page_load will be called with every asynchronous postback to the server, to overcome this, you can use jquery ajax.

Categories