Click here to Skip to main content
6,932,036 members and growing! (13,470 online)
Email Password   helpLost your password?
Platforms, Frameworks & Libraries » Vista API » General     Intermediate License: The Microsoft Public License (Ms-PL)

detailsview.TemplateField.FooterTemplate rendering problem for using it to appear in another language for detailsview

By Kareem.Ammer

This code solves the problem of rendering detailsview.TemplateField.FooterTemplate
HTML, C#2.0.NET2.0, Vista, ASP.NET, WebForms, VS2005, Dev
Posted:17 Jan 2007
Views:17,636
Bookmarked:11 times
printPrint Friendly   add Share
      Discuss Discuss   Broken Article?Report  
8 votes for this article.
Popularity: 2.48 Rating: 2.75 out of 5
2 votes, 28.6%
1
1 vote, 14.3%
2

3
1 vote, 14.3%
4
3 votes, 42.9%
5

Sample Image - KareemCode1.jpg

Introduction

After a long search about why DetailsView.FieldTemplate.FooterTemplate is not rendered, I found out a way to generate it from scratch by modifying the HTML code that is generated by DotNet for the DetailsView control. I will explain it in the next steps.

Step 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>

The code above illustrates that when putting the fields of detailsview, I put them between two $ as if I have Field(Template Field,Bound Filed,.......) called (Name). I put its headerText property as $Name$, and added a new Template column for the Footer and put it between two $ as in the previous example but by using another language that I wanted to use.

Step 2

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);
}

This function takes the HTML code as a ref string and takes the detailsview control as a parameter. The function loops on detailsView Control fields and uses the SetFooter parameter to generate the 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);
}

This function takes fields one by one to generate its Footer template by converting the rendered HTML code of the Template Column that I added in the DetailsView Source Code.

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\">");
    }
}

This function adjusts the general figure of detailsview because of the problems that result from the modifications to the rendered HTML code.

License

This article, along with any associated source code and files, is licensed under The Microsoft Public License (Ms-PL)

About the Author

Kareem.Ammer


Member
I'm Kareem Ammer Solution developer @ AlfanarIT Company @ KSA,graduated from Faculty of Comupter And Information Helwan university, Information system department.
Occupation: Web Developer
Location: Egypt Egypt

Other popular Vista API articles:

 
Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 4 of 4 (Total in Forum: 4) (Refresh)FirstPrevNext
GeneralI want to use multiple files per culture how can i...? PinmemberNaveen Kumar M20:43 14 Jun '07  
GeneralRe: I want to use multiple files per culture how can i...? PinmemberKareem.Ammer4:12 17 Jul '07  
Generalmore languages Pinmembertamoura22:54 20 Jan '07  
GeneralRe: more languages PinmemberKareem.Ammer2:03 21 Jan '07  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+PgUp/PgDown to switch pages.

PermaLink | Privacy | Terms of Use
Last Updated: 17 Jan 2007
Editor: Deeksha Shenoy
Copyright 2007 by Kareem.Ammer
Everything else Copyright © CodeProject, 1999-2010
Web18 | Advertise on the Code Project