DetailsView.TemplateField.FooterTemplate 渲染问题:使其在另一种语言中显示






2.75/5 (7投票s)
这段代码解决了 DetailsView.TemplateField.FooterTemplate 的渲染问题。
引言
在长时间的搜索之后,我发现了一种通过修改 DotNet 为 DetailsView
控件生成的 HTML 代码,从头开始生成 DetailsView.FieldTemplate.FooterTemplate
的方法。我将在接下来的步骤中解释它。
步骤 1
<asp:DetailsView ID="DetailsView1" runat="server" AllowPaging="True"
AutoGenerateRows="False"
CellPadding="4" DataSourceID="ObjectDataSource1"
ForeColor="#333333" GridLines="None"Height="50px"
OnItemInserting="DetailsView1_ItemInserting"
OnItemUpdated="DetailsView1_ItemUpdated"
Width="50%">
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<CommandRowStyle BackColor="#E2DED6" Font-Bold="True" />
<EditRowStyle BackColor="#999999" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<Fields>
<asp:BoundField DataField="Guid" HeaderText="$ID$"
InsertVisible="False" ReadOnly="True" />
<asp:TemplateField HeaderText="$Ÿéèí§$" InsertVisible="False">
</asp:TemplateField>
<asp:BoundField DataField="Name" HeaderText="$Name$" />
<asp:TemplateField HeaderText="$ŸéŸ«ê$"></asp:TemplateField>
<asp:BoundField DataField="Address" HeaderText="$Address$" />
<asp:TemplateField HeaderText="$ŸéãëíŸë$"></asp:TemplateField>
<asp:BoundField DataField="Email" HeaderText="$Email$" />
<asp:TemplateField HeaderText="$ŸéŸêïé ŸéŸé袩íëï$"></asp:TemplateField>
<asp:BoundField DataField="Tel" HeaderText="$Telephone$" />
<asp:TemplateField HeaderText="$Ÿé¢éïåíë$"></asp:TemplateField>
<asp:CommandField ShowEditButton="True" ShowInsertButton="True" />
</Fields>
<FieldHeaderStyle BackColor="#E9ECF1" Font-Bold="True" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
</asp:DetailsView>
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server"
DataObjectTypeName="Employee"
InsertMethod="Add" SelectMethod="getAllEmplyees"
TypeName="Employee" UpdateMethod="Update">
</asp:ObjectDataSource>
上面的代码说明,在放置 detailsview
的字段时,我将它们放在两个 $
之间,就像有一个名为 (Name)
的 Field(Template Field,Bound Filed,.......)
一样。我将其 headerText
属性设置为 $Name$
,并为 Footer 添加了一个新的 Template 列,并像之前的示例一样将其放在两个 $
之间,但使用我想要使用的另一种语言。
第二步
public static void ApplyFooters(ref string str, DetailsView dv)
{
for (int i = 0; i < dv.Fields.Count - 1; i += 2)
{
FooterTemplate.SetFooter(ref str, dv.Fields[i].HeaderText,
dv.Fields[i + 1].HeaderText, dv.ClientID);
}
FooterTemplate.AdjustGrid(ref str, dv.ClientID);
}
此函数将 HTML 代码作为 ref
字符串,并将 detailsview
控件作为参数。该函数循环遍历 detailsView
控件的字段,并使用 SetFooter
参数生成 Footer。
private static void SetFooter(ref string str, string Header,
string Footer, string GridName)
{
int StartGridIndex = str.IndexOf(GridName);
if (StartGridIndex == -1) return;
int EndGridIndex = str.IndexOf("</table>", StartGridIndex);
int HeaderIndex = str.IndexOf(Header);
if (HeaderIndex == -1) return;
int StartIndex = str.IndexOf("</tr>", HeaderIndex);
if (StartIndex == -1 || (StartIndex < StartGridIndex ||
StartIndex > EndGridIndex)) return;
int EndIndex = str.IndexOf(">", StartIndex + 5);
string oHeader = Header.Replace("$", "");
string oFooter = Footer.Replace("$", "");
str = str.Remove(StartIndex, (EndIndex - StartIndex) + 1);
str = str.Remove(str.IndexOf(Footer) + Footer.Length + 5, 18);
str = str.Insert(str.IndexOf(Footer) - 1, " align= right");
str = str.Replace(Header, oHeader);
str = str.Replace(Footer, oFooter);
}
此函数逐个获取字段,通过转换在 DetailsView
源代码中添加的 Template 列的渲染 HTML 代码来生成其 Footer 模板。
private static void AdjustGrid(ref string str, string GridName)
{
int StartIndex = str.IndexOf(GridName);
if (StartIndex == -1) return;
int EndIndex = str.IndexOf("</table>", StartIndex);
int ColIndex = str.IndexOf("<td colspan=\"2\">", StartIndex);
if (ColIndex == -1) return;
if (ColIndex < EndIndex)
{
str = str.Remove(ColIndex, 16);
str = str.Insert(ColIndex, "<td colspan=\"3\">");
}
ColIndex = str.IndexOf("<td colspan=\"2\">", StartIndex);
if (ColIndex == -1) return;
if (ColIndex < EndIndex)
{
str = str.Remove(ColIndex, 16);
str = str.Insert(ColIndex, "<td colspan=\"3\">");
}
}
此函数调整 detailsview
的整体结构,因为对渲染 HTML 代码的修改导致的问题。