I am fairly new to ASP.NET and I am trying to add the number i created in the backend to be inserted into
the label text field in the front-end but Im not being able to access the Label (lblAmountNumber) inside the Repeater i guess it is not allowing me to access the ID=lblAmountNumber
and the name of my repeater is RpMember
I have my simple code down below
FRONT END
<asp:Repeater ID="RpMember" runat="server">
<HeaderTemplate>
<table id="example1" class="table table-bordered table-hover">
<thead>
<tr>
<th style="min-width: 100px">
<asp:Label ID="Label2" runat="server" Text="MemberInfo"></asp:Label></th>
<th style="min-width: 100px">
<asp:Label ID="Label3" runat="server" Text="Amount Number"></asp:Label></th>
<th style="min-width: 100px">
<asp:Label ID="Label5" runat="server" Text="Loan Balance"></asp:Label></th>
<th style="min-width: 100px">
<asp:Label ID="Label1" runat="server" Text="Savings Balance"></asp:Label></th>
<th style="min-width: 100px" class="ButtonView">
<asp:Label ID="Label6" runat="server"></asp:Label></th>
</tr>
</thead>
<tbody>
</HeaderTemplate>
<ItemTemplate>
<tr style="text-align: center">
<td style="width: 100px; text-align: left; text-transform: capitalize"><%#Eval("Name") %>
<br />
<%#Eval("Phone") %>
<br />
<%#Eval("Address") %>
</td>
<td style="width: 100px"><asp:Label ID="lblAmountNumber" runat="server"></asp:Label></th></td>
<td style="width: 100px"><%#Eval("LoanBalance") %></td>
<td style="width: 100px"><%#Eval("SavingsBalance") %></td>
<td style="text-align: center; width: 100px" class="ButtonView">
<asp:LinkButton ID="ReportLinkBtn" runat="server" OnClick="ReportLinkBtn_Click" CommandArgument='<%# Eval("MemberId")%>' CssClass="btn btn-success" Text="Report Details" />
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</tbody>
</table>
</FooterTemplate>
</asp:Repeater>
BACKEND
DataTable dt2 = objdalTransactionEntry.GetLoanTakerStartAmountByLoanTakerId(Sessions.Name.UserId, MemberId);
foreach (RepeaterItem item in RpMember.Items)
{
Label lab = item.FindControl("lblHishabNumber") as Label;
string yearForHishabNumber = Convert.ToDateTime(dt2.Rows[0]["EntryDate"].ToString()).Year.ToString();
string monthForHishabNumber = Convert.ToDateTime(dt2.Rows[0]["EntryDate"].ToString()).ToString("MM");
string yearForInvoiceNumber = Convert.ToDateTime(dt2.Rows[0]["EntryDate"].ToString()).Year.ToString();
string monthForInvoiceNumber = Convert.ToDateTime(dt2.Rows[0]["EntryDate"].ToString()).ToString("MM");
string dayForInvoiceNumber = Convert.ToDateTime(dt2.Rows[0]["EntryDate"].ToString()).ToString("dd");
lab.Text = "Amount Number " + monthForHishabNumber + yearForHishabNumber.Substring(yearForHishabNumber.Length - 2) + dt2.Rows[0]["UserWiseID"].ToString();
}
Related
I'm trying to control the output from a repeater.
I do have a nice working repeater but would like to further control the display of cirtain values.
So this is my repeater:
<asp:Repeater ID="Repeater1" runat="server" DataSourceID="SqlDataSource_MyList" EnableViewState="False">
<HeaderTemplate>
<table border="0" style="margin:0px; border-collapse: collapse; border-spacing: 0px; padding:0px;" class="table table-striped table-hover">
<thead>
<tr style="height:35px; font-weight: bold;">
<th style="width:20%" class="toprowcolor">StartNr</th>
<th style="width:40%" class="toprowcolor">Name</th>
<th style="width:30%" class="toprowcolor">StartDate</th>
<th style="width:10%" class="toprowcolor">Group</th>
</tr>
</thead>
<tbody>
</HeaderTemplate>
<ItemTemplate>
<tr style="height:27px;" >
<td style="width:20%" class="listtext_s"><asp:Label ID="startnrLabel" runat="server" Text='<%# Eval("startnr") %>' /></td>
<td style="width:40%" class="listtext_s"><asp:Label ID="nameLabel" runat="server" Text='<%# Eval("name") %>' /></td>
<td style="width:30%" class="listtext_s"><asp:Label ID="startdateLabel" runat="server" Text='<%# Eval("startdate", "{0:g}") %>' /></td>
<td style="width:10%" class="listtext_s"><asp:Label ID="groupLabel" runat="server" Text='<%# Eval("group") %>' /></td>
</tr>
</ItemTemplate>
<FooterTemplate>
</tbody>
</table>
</FooterTemplate>
</asp:Repeater>
I would like to do something like this; (Trying to explain my goal in below code with a mix of spegetticode (Classic ASP) with some translation to ASP.NET C#) I hope you can read it!?
<asp:Repeater ID="Repeater1" runat="server" DataSourceID="SqlDataSource_MyList" EnableViewState="False">
<HeaderTemplate>
<table border="0" style="margin:0px; border-collapse: collapse; border-spacing: 0px; padding:0px;" class="table table-striped table-hover">
<thead>
<tr style="height:35px; font-weight: bold;">
<th style="width:20%" class="toprowcolor">StartNr</th>
<th style="width:40%" class="toprowcolor">Name</th>
<th style="width:30%" class="toprowcolor">StartDate</th>
<th style="width:10%" class="toprowcolor">Group</th>
</tr>
</thead>
<tbody>
</HeaderTemplate>
<ItemTemplate>
<% If Eval("name") == "aron"
{
If Eval("group") == "1"
{ namecellcolor = "#454545" }
ElseIf Eval("group") == "2"
{ namecellcolor = "#555555" }
ElseIf Eval("group") == "3"
{ namecellcolor = "#666666" }
Else
{ namecellcolor = "#FFFFFF" }
}
ElseIf Eval("name") == "bill"
{ namecellcolor = "#000000" }
Else
{ namecellcolor = "#111111" }
%>
<% If Eval("startdate", "{0:g}") <= DateTime.Now.Date; AND Eval("group") == "1" OR Eval("startdate", "{0:g}") <= DateTime.Now.Date; AND Eval("group") == "2"
{ groupcellcolor = "#010101" }
elseif Eval("startdate", "{0:g}") <= (DateTime.Now.Date; + 2) AND Eval("group") == "1" OR Eval("startdate", "{0:g}") <= (DateTime.Now.Date; + 2) AND Eval("group") == "2"
{ groupcellcolor = "#333333" }
else
{ groupcellcolor = "#000000" }
%>
<tr style="height:27px;" >
<td style="width:20%; background-color: <%# namecellcolor %>" class="listtext_s"><asp:Label ID="startnrLabel" runat="server" Text='<%# Eval("startnr") %>' /></td>
<td style="width:40%" class="listtext_s"><asp:Label ID="nameLabel" runat="server" Text='<%# Eval("name") %>' /></td>
<td style="width:30%" class="listtext_s"><asp:Label ID="startdateLabel" runat="server" Text='<%# Eval("startdate", "{0:g}") %>' /></td>
<td style="width:10%; background-color: <%# groupcellcolor %>" class="listtext_s"><asp:Label ID="groupLabel" runat="server" Text='<%# Eval("group") %>' /></td>
</tr>
</ItemTemplate>
<FooterTemplate>
</tbody>
</table>
</FooterTemplate>
</asp:Repeater>
Your answer:
Wow dude, crawl back under that stonedge block of yours and dont bother to come back!
Interesting set of typo's you got there champ!
I will try to actually explain a working solutions to your problem, and here goes;
So what will it be?
I really appreciate any help i can get.
Interesting set of typo's you got there champ!
But I see what you are trying to achieve. You basically need to choose a background color based on the values in the databound object.
You can in theory do all this in the aspx file but as you can see, it's very messy and you quickly lose track of what is what.
A better option is to do this in the backend code. Either just call a method in the Page to determine and return the color, or better, use the ItemDataBound event to manipulate the html after it is rendered by the repeater.
Here is how to do the name cell.
Add an ID to the <td> and make it runat="server" so the code-behind can access it.
Create an ItemDataBound event handler for the repeater.
In the event handler get your data object from e.Item.DataItem and use it to work out the background color. This should be simpler because now you are in code and just have an object (in your case a DataRow as you are using SqlDataSource) instead of all the Eval()s.
Find the <td> control and set it's BgColor.
Something like this should do it (untested but should be close enough):
<asp:Repeater ID="Repeater1" runat="server" DataSourceID="SqlDataSource_MyList" EnableViewState="False"
OnItemDataBound="Repeater1_ItemDataBound">
<HeaderTemplate>
<table border="0" style="margin:0px; border-collapse: collapse; border-spacing: 0px; padding:0px;" class="table table-striped table-hover">
<thead>
<tr style="height:35px; font-weight: bold;">
<th style="width:20%" class="toprowcolor">StartNr</th>
<th style="width:40%" class="toprowcolor">Name</th>
<th style="width:30%" class="toprowcolor">StartDate</th>
<th style="width:10%" class="toprowcolor">Group</th>
</tr>
</thead>
<tbody>
</HeaderTemplate>
<ItemTemplate>
<tr style="height:27px;" >
<td runat="server" ID="nameCell" style="width:20%" class="listtext_s"><asp:Label ID="startnrLabel" runat="server" Text='<%# Eval("startnr") %>' /></td>
<td style="width:40%" class="listtext_s"><asp:Label ID="nameLabel" runat="server" Text='<%# Eval("name") %>' /></td>
<td style="width:30%" class="listtext_s"><asp:Label ID="startdateLabel" runat="server" Text='<%# Eval("startdate", "{0:g}") %>' /></td>
<td style="width:10%" class="listtext_s"><asp:Label ID="groupLabel" runat="server" Text='<%# Eval("group") %>' /></td>
</tr>
</ItemTemplate>
<FooterTemplate>
</tbody>
</table>
</FooterTemplate>
</asp:Repeater>
public void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
// get the data item bound to this row of the repeater
var dataItem = (DataRow)e.Item.DataItem;
// do the logic on the raw object instance
string namecellcolor;
// read the values from the DataRow (may need to cast to a different type, depending om your SQL query).
string itemName = (string)dataItem["name"];
int itemGroup = (int)dataItem["group"];
if(itemName == "aron")
{
if(itemGroup == 1)
namecellcolor = "#454545";
else if(itemGroup == 2)
namecellcolor = "#555555";
else if(itemGroup == 3)
namecellcolor = "#666666";
else
namecellcolor = "#FFFFFF";
}
else if(itemName == "bill")
namecellcolor = "#000000";
else
namecellcolor = "#111111";
// get the table cell and set its BgColor.
var nameCell = (System.Web.UI.HtmlControls.HtmlTableCell)e.Item.FindControl("nameCell");
nameCell.BgColor = namecellcolor;
}
I am trying to use linkbutton inside of foreach in asp.net
I have below html.
Asp.net html:
<table border="1" style="grid-cell: inherit; border-spacing: inherit;">
<thead>
<tr>
<th>İlan ID
</th>
<th>İlan Yolu
</th>
<th>Eklenme Tarihi
</th>
<th>İlk Güncelleme Tarihi
</th>
<th>Güncelleme Aralığı
</th>
<th>Son Güncelleme Tarihi
</th>
<th>Aktifmi
</th>
<th>Detay Göster
</th>
</tr>
</thead>
<%foreach (var item in list)
{%>
<tr>
<td style="text-align: center">
<span><%= item.Id%> </span>
</td>
<td>
<span><%=item.DosyaAdi %></span>
</td>
<td style="text-align: center">
<span><%=item.EklemeTarihi %></span>
</td>
<td>
<span><%=item.IlkGuncellemeTarihi %></span>
</td>
<td style="text-align: center">
<span><%=item.GuncellemeAraligi %></span>
</td>
<td style="text-align: center">
<span><%=item.SonGuncelleme %></span>
</td>
<td style="text-align: center">
<input type="checkbox" class="chk" id="<%=item.Id %>" <%= item.Aktif ==true ? "checked='checked'" : "" %> />
</td>
<td style="text-align: center">
<asp:LinkButton ID="lbdetay" runat="server" OnClick="lbdetay_Click" CommandArgument="<%=item.Id%>" CommandName="Detay">Detay</asp:LinkButton>
</td>
</tr>
<% } %>
</table>
Question:
In part of linkbutton as below
<asp:LinkButton ID="lbdetay" runat="server" OnClick="lbdetay_Click" CommandArgument="<%=item.Id%>" CommandName="Detay">Detay</asp:LinkButton>
If i use CommandArgument="<%=item.Id%>" it is not working (it displays syntax error here)
Where i miss inside of code for command argument in linkbutton side ?
Any help will be appreciated.
Thanks
<%= %> is equivalent to Response.Write, it outputs directly to the html markup. So it cannot write to server-side controls at all, since it does not know about them. In other words, what you are trying to do is impossible.
You should probably consider refactoring this foreach to Repeater, which gives control over controls as well as html:
<asp:Repeater runat="server" ID="Repeater1">
<ItemTemplate>
<td style="text-align: center">
<span><%# Eval("Id") %></span>
</td>
... same for other tds ...
<td style="text-align: center">
<asp:LinkButton ID="lbdetay" runat="server" OnClick="lbdetay_Click" CommandArgument='<%# Eval("Id") %>' CommandName="Detay">Detay</asp:LinkButton>
</td>
</ItemTemplate>
</asp:Repeater>
And don't forget to data bind it:
Repeater1.DataSource = list;
Repeater1.DataBind();
I have 3 text fields on my page and one calendar control. After I fill in the 3 text boxes, then click a date on the calendar control, the bottom textfield clears out, losing its value. The other 2 text boxes have their values available and do not get cleared. The textboxes all have the same properties, but I can't figure out how to keep the value in the third textbox.
<table class="auto-style2" align="center">
<tr>
<td class="auto-style4" style="text-align: right">Project Name:</td>
<td style="text-align: left">
<asp:TextBox ID="TxtProjectName" runat="server" height="32px" width="211px" MaxLength="50" OnTextChanged="TxtProjectName_TextChanged" onKeyUp="Count(this,50)" onChange="Count(this,50)" TextMode="MultiLine"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="TxtProjectName" ErrorMessage="Field is required"></asp:RequiredFieldValidator>
</td>
</tr>
</table>
<table class="auto-style2" align="center">
<tr>
<td class="auto-style4" style="text-align: right">Project Description:</td>
<td style="text-align: left">
<asp:TextBox ID="TxtDescription" runat="server" height="45px" width="211px" TextMode="MultiLine" OnTextChanged="TxtDescription_TextChanged"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="TxtDescription" ErrorMessage="Field is required"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td class="auto-style4" style="text-align: right">Contact Name:</td>
<td style="text-align: left">
<asp:TextBox ID="TxtContactName" runat="server" Width="203px" OnTextChanged="TxtContactName_TextChanged" Height="16px"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ControlToValidate="TxtContactName" ErrorMessage="Field is required"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td class="auto-style5">Business Area:</td>
<td class="auto-style3">
<asp:DropDownList ID="DDLBusinessArea" runat="server" style="text-align: left">
</asp:DropDownList>
</td>
</tr>
<tr>
<td class="auto-style4" style="text-align: right">Priority:</td>
<td style="text-align: left">
<asp:DropDownList ID="DDLPriority" runat="server">
</asp:DropDownList>
</td>
</tr>
<tr>
<td class="auto-style4" style="text-align: right">Desired Completion Date:</td>
<td style="text-align: left">
<asp:Calendar ID="Calendar1" runat="server">
<SelectedDayStyle Font-Bold="True" />
<TodayDayStyle ForeColor="#3333FF" />
</asp:Calendar>
</td>
</tr>
Its the TxtContactName that clears.
I have the following snippet getting control ids in a listview at runtime. However ctrl is null when debugged in spite of the correct id being provided.
foreach (ListViewItem item in lvData.Items)
{
string uid = item.ClientID;
Control ctrl = lvData.FindControl(uid+"_lbUnattend");
ctrl.Visible = false;
}
my template:
<asp:ListView runat="server" ID="lvData" DataSourceID="odsEvents"
OnDataBound="lvData_DataBound"
onselectedindexchanged="lvData_SelectedIndexChanged">
<LayoutTemplate>
<table class="diary" cellspacing="0" width="600px">
<tr>
<th align="center">
Date:
</th>
<th align="center">
Time:
</th>
<th align="center">
Event:
</th>
<th align="center">
Location:
</th>
<th align="center">
</th>
</tr>
<tr runat="server" id="itemPlaceHolder">
</tr>
</table>
</LayoutTemplate>
<ItemTemplate>
<tr>
<td width="100px" align="center">
<%#FRT.Utils.DateTimeHelper.FormatToShortDate(Eval("StartDate"))%>
<%#FRT.Utils.DateTimeHelper.FormatToShortDate(Eval("VisitDateStart"))%>
</td>
<td width="100px" align="center">
<%#FRT.Utils.DateTimeHelper.FormatToShortTime(Eval("StartDate"))%>
<%#FRT.Utils.DateTimeHelper.FormatToShortTime(Eval("VisitDateStart"))%>
</td>
<td width="100px" align="center">
<%#Eval("Title")%>
</td>
<td width="200px" align="center">
<%# Eval("Address") %>
</td>
<td width="100px" align="center">
<asp:LinkButton ID="lbLogin" runat="server" Text="I want to attend this event" CssClass="button" />
<asp:LinkButton ID="lbAttend" runat="server" Text="I want to attend this event" CssClass="button" />
<asp:LinkButton ID="lbUnattend" runat="server" CommandName="unattend" Text="I want to unregister from this event" CssClass="button" />
See details 
</td>
</tr>
</ItemTemplate>
</asp:ListView>
where linkbuttons whith ids generated from lbUnattend are the ones I want.
Anyone pointout where the problem is?
I think what you're looking for is
foreach (ListViewItem item in lvData.Items)
{
Control ctrl = item.FindControl("lbUnattend");
ctrl.Visible = false;
}
Exporting from c#.net
I am getting a problem I have a form that when I export to excel as a result in excel Any ideas why is this happening I am including the ASP code below.
<%# Page Language="C#" MasterPageFile="~/masterpages/Admin.master" AutoEventWireup="true" CodeFile="members-search-adv.aspx.cs" Inherits="masteradmin_members_search_adv" Title="LISA - Life Insurance Settlement" %>
<%# Register TagPrefix="UC" TagName="Paging" Src="~/controls/Paging.ascx" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<script type="text/javascript" src="sorttable.js"></script>
Go to Quick Search
<h1>Contact Database - Advanced Search</h1>
<asp:Label ID="lblSearchCriteria" runat="server" Text="" ForeColor="blue"></asp:Label>
<table width="100%" cellspacing="0" cellpadding="0">
<tr>
<td valign="top" width="50%">
<table border="0" cellpadding="3" cellspacing="1" style="height: 214px; width: 101%;">
<tr>
<td class="form1" align="right"><strong>OpenSearch:</strong></td>
<td class="form2">
<asp:TextBox ID="txtSearch" runat="server" CssClass="fields" Width="245px" ></asp:TextBox><b>*</b> <br><b>*</b><small>Search By Company Name, First Name, Last Name, Tags, Comments</small></td>
</tr>
<tr>
<td class="form1" align="right"><strong>Industry Segment:</strong></td>
<td class="form2"><asp:DropDownList ID="ddlIndSegment" runat="server" CssClass="fields" /></td>
</tr>
<tr>
<td class="form1" align="right"><strong>Member Type:</strong></td>
<td class="form2"><asp:DropDownList ID="ddlMemberType" runat="server" CssClass="fields" /></td>
</tr>
<tr>
<td class="form1" align="right"><strong>Member Rep only:</strong></td>
<td class="form2"><asp:CheckBox ID="cbxMemberRep" runat="server" /></td>
</tr>
<tr>
<td class="form1" align="right"><strong>Board Only:</strong></td>
<td class="form2"><asp:CheckBox ID="cbxBoardOnly" runat="server" /></td>
</tr>
<tr>
<td colspan="2" align="right">
<asp:ImageButton ID="btnSearch" runat="server" ImageUrl="/RadControls/images/bu_search.jpg" />
</td>
</tr>
</table>
</td>
<td> </td>
<td valign="top" width="50%">
<%--split the table here--%>
<table border="0" cellpadding="3" cellspacing="1"
style="width: 98%; margin-left: 0px;">
<tr>
<td class="form1" align="right"><strong>Suspended Only:</strong></td>
<td class="form2"><asp:CheckBox ID="cbxSuspended" runat="server" /></td>
</tr>
<tr>
<td class="form1" align="right"><strong>Active Only:</strong></td>
<td class="form2"><asp:CheckBox ID="cbxActiveOnly" runat="server" /></td>
</tr>
<tr>
<td class="form1" align="right"><strong>Allow Other Members To See My Info Only:</strong></td>
<td class="form2"><asp:CheckBox ID="cbxAllowOtherMembers" runat="server" /></td>
</tr>
<tr>
<td class="form1" align="right"><strong>State:</strong></td>
<td class="form2"><asp:DropDownList ID="ddlState" runat="server" CssClass="fields" /></td>
</tr>
<tr>
<td class="form1" align="right"><strong>By Special Level:</strong></td>
<td class="form2"><asp:DropDownList ID="ddlSpecialLevel" runat="server" CssClass="fields" /></td>
</tr>
<tr>
<td class="form1" align="right"><strong>Sort by:</strong></td>
<td class="form2">
<asp:DropDownList ID="ddlSortBy" runat="server" CssClass="fields">
<asp:ListItem Value="CompanyName">Company</asp:ListItem>
<asp:ListItem Value="FirstName">First Name</asp:ListItem>
<asp:ListItem Value="LastName">Last Name</asp:ListItem>
<asp:ListItem Value="MemberCategoryId">Membership Type</asp:ListItem>
<asp:ListItem Value="IndustrySegmentId">Industry Segment</asp:ListItem>
</asp:DropDownList>
</td>
</tr>
</table>
</td>
</tr>
</table>
<table border="0" cellpadding="3" cellspacing="1" width="100%">
<tr>
<td><asp:Literal ID="litTotalCount" runat="server" /></td><td align=left class="form3">
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
<asp:LinkButton ID="LinkButton1" runat="server" onclick="LinkButton1_Click">Export To Excel</asp:LinkButton>
<small>* Click on the column to SORT</small></td></tr>
</table>
<asp:Repeater ID="rptList" runat="server">
<HeaderTemplate>
<table class="sortable" border="0" cellpadding="3" cellspacing="1" width="100%">
<tr>
<td align="left" valign="top" class="form1"><b>Company</b></td>
<td align="left" valign="top" class="form1"><b>First Name</b></td>
<td align="left" valign="top" class="form1"><b>Last Name</b></td>
<td align="left" valign="top" class="form1"><b>Email</b></td>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td class="form2" align="left" valign="top"><asp:HyperLink ID="hlCompany" runat="server" CssClass="link"></asp:HyperLink></td>
<td class="form2" align="left" valign="top"><asp:HyperLink ID="hlFirstName" runat="server" CssClass="link"></asp:HyperLink></td>
<td class="form2" align="left" valign="top"><asp:HyperLink ID="hlLastName" runat="server" CssClass="link"></asp:HyperLink></td>
<td class="form2" align="left" valign="top"><asp:HyperLink ID="hlEmail" runat="server" CssClass="link"></asp:HyperLink> </td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
<asp:GridView ID="GridView2" runat="server">
</asp:GridView>
<asp:PlaceHolder ID="PlaceHolder1" runat="server"><br />
<UC:Paging Id="ctlPaging" runat="server" />
</asp:PlaceHolder>
<%-- <div align="center">
<br /><UC:Paging Id="ctlPaging" runat="server" />
</div>--%>
</asp:Content>
Sorry but this is a better explanation of the issue
Exporting from c#.net I am getting a problem I have a form that when I export to excel I ge the following <div></div> I have data in the datagrid but not in excel I am just getting <div></div>
And this is the function that I am using
void ExportToExcel3()
{
Response.Clear();
Response.AddHeader("content-disposition", "attachment;filename=FileName.xls");
Response.Charset = "";
Response.ContentType = "application/vnd.xls";
System.IO.StringWriter stringWrite = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
GridView2.RenderControl(htmlWrite);
Response.Write(stringWrite.ToString());
Response.End();
}
public override void VerifyRenderingInServerForm(Control control)
{
}
I am populating the datagrid prior to the export. This is the line of code. It's funny I create a new webform just with this section of code and the export worked.
Do you think there's a problem with the datarepeater that I have in the page that is causing the conflict.
MemberList list = MemberDB.GetMembers(sql, m_page, m_RecordPerPage, out count, _state);
this.GridView2.DataSource = list;
this.GridView2.DataBind();
That's not the sequence at all- it's just not how asp.net and the web work.
You don't fill the grid during the load phase: you retrieve the data and set it as the datasource for the table, but the grid (actually a repeater) isn't filled yet. That doesn't happen until the databinding phase. After the databinding phase (during the render phase) the page with the databound repeater is finally sent by the server to the browser. At this point all your server-side stuff is disposed.
Now the browser can finally show the page with the big "Export to Excel" button which the user can click. Again: the server is already destroying all the data you worked so hard to build. When the user clicks your button, a brand new request is created and a brand new page will be sent back to the browser. You have to load all your data into the grid again.
I found the solution to my problem. Thank you all for contributing. On the Load_Page Event I had the binding inside of an if !postback.