#asp.net #asp.net-mvc #asp.net-core
#asp.net #asp.net-mvc #asp.net-core
Вопрос:
На самом деле я являюсь gridview в том смысле, что я использовал один столбец TemplateField для отображения данных в браузере при экспорте этой таблицы в EXCEl, PDF и CSV, данные для этого столбца не отображаются, как исправить эту проблему. Спасибо
ВОТ код .asp
<asp:GridView ID="gvmacroManagement" runat="server" Font-Size="Small" Height="40px" Width="100%" AutoGenerateColumns="false"
BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" OnPageIndexChanging="gvmacroManagement_PageIndexChanging"
BorderWidth="1px" CellPadding="3" AllowPaging="true" PageSize="10" CssClass="mGrid">
<Columns>
<asp:BoundField DataField="Id" HeaderText="ID" ItemStyle-Width="50" Visible="false" />
<asp:BoundField DataField="FirstName" HeaderText="Physician Name" ItemStyle-Width="50" />
<asp:BoundField DataField="Hospital" HeaderText="Hospital Name" ItemStyle-Width="50" />
<asp:BoundField DataField="MacroTitle" HeaderText="Macro Title" ItemStyle-Width="50" />
<%--<asp:BoundField DataField="MacroDescription" HeaderText="Macro Description" ItemStyle-Width="50" />--%>
<asp:TemplateField ItemStyle-Width="50" HeaderText="Macro Description">
<ItemTemplate>
<asp:Label ID="MacroDescription" runat="server"
Text='<%# Limit(Eval("MacroDescription"),30) %>'
ToolTip='<%# Eval("MacroDescription") %>'
>
</asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="PId" HeaderText="PId" ItemStyle-Width="50" Visible="false" />
<asp:BoundField DataField="CreatedBy" HeaderText="CreatedBy" ItemStyle-Width="50" />
<%--<asp:BoundField DataField="PId" HeaderText="PId" ItemStyle-Width="50" Visible="false" />--%>
<asp:BoundField DataField="HId" HeaderText="HId" ItemStyle-Width="50" Visible="false" />
<asp:TemplateField HeaderText="Action" ItemStyle-Width="90">
<ItemTemplate>
<asp:ImageButton ID="imgMacroEdit" ImageUrl="images/edit.png" runat="server" Width="15" Height="15"
CommandArgument='<%# Eval("Id") "," Eval("FirstName") "," Eval("Hospital") "," Eval("MacroTitle")
"," Eval("macrodescription") "," Eval("PId") "," Eval("HId")%>'
OnClick="imgMacroEdit_Click" OnClientClick="return confirm('Are you sure you want to edit this Macro?');" ToolTip="Edit" />
<asp:ImageButton ID="imgMacroDelete" ImageUrl="images/Delete.png" runat="server" Width="15" Height="15"
OnClick="imgMacroDelete_Click" OnClientClick="return confirm('Are you sure you want to delete this Macro?');"
CommandArgument='<%# Eval("Id")%>' ToolTip="Delete" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="White" ForeColor="#000066" />
<HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" />
<RowStyle ForeColor="#000066" />
<SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#F1F1F1" />
<SortedAscendingHeaderStyle BackColor="#007DBB" />
<SortedDescendingCellStyle BackColor="#CAC9C9" />
<SortedDescendingHeaderStyle BackColor="#00547E" />
</asp:GridView>
ВОТ код файла .CS
protected string Limit(object desc, int maxLength)
{
var MacroDescription = (string)desc;
if (string.IsNullOrEmpty(MacroDescription)) { return MacroDescription; }
return MacroDescription.Length <= maxLength ?
MacroDescription : MacroDescription.Substring(0, maxLength) " ... ";
}
Комментарии:
1. Взгляните на mikesdotnetting.com/article/278 /… .
Ответ №1:
GridView
с TemplateField
может быть легко экспортирован и также отображается должным образом. Проблема возникает, когда GridView со столбцом TemplateField содержит элементы управления, такие как HyperLink
, TextBox
, Button
LinkButton
RadioButton
или CheckBox
элементы управления. Когда возникает такой случай, нам нужно преобразовать, удалить эти элементы управления и заменить их элементами управления Label или Literal.
Вот функция GetCellText.
private static string GetCellText(DataControlFieldCell cell)
{
if (cell.Controls.Count == 0) {
return CsvFormatted(cell.Text);
} else {
foreach (object current_loopVariable in cell.Controls) {
current = current_loopVariable;
if (current is Label) {
return CsvFormatted((current as Label).Text);
} else if (current is TextBox) {
return CsvFormatted((current as TextBox).Text);
} else if (current is LinkButton) {
return CsvFormatted((current as LinkButton).Text);
} else if (current is ImageButton) {
return CsvFormatted((current as ImageButton).AlternateText);
} else if (current is HyperLink) {
return CsvFormatted((current as HyperLink).Text);
} else if (current is DropDownList) {
return CsvFormatted((current as DropDownList).SelectedItem.Text);
} else if (current is CheckBox) {
return CsvFormatted((current as CheckBox).Checked ? "True" : "False");
}
}
}
return "";
}