0
votes

I have a gridview where all its fields are TemplateField which contain textboxes as its ItemTemplate. The idea of the application is for the gridview to get a template from the database via an SqlDataSource. Then the user may edit the desired gridview rows via the textboxes and press a button to convert the gridview to a csv file.

I resulted to this method do to my understanding that the edit and update gridview controls updates the database values and I do not want to alter the database records.

My Code

I have tried using this method but from my understanding it only works with BoundFields and not Templatefields because it returns blank values when using Templatefields.

Sub ExcelConvertion2()
    Response.Clear()
    Response.Buffer = True
    Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.csv")
    Response.Charset = ""
    Response.ContentType = "application/text"

    Dim sb As StringBuilder = New StringBuilder()

    For Each cell As TableCell In GridView1.HeaderRow.Cells
        'Append data with separator.
        sb.Append(cell.Text & ",")
    Next

    'Append new line character.
    sb.Append(vbCr & vbLf)

    For Each row As GridViewRow In GridView1.Rows

        For Each cell As TableCell In row.Cells
            'Append data with separator.
            sb.Append(cell.Text & ",")
        Next

        'Append new line character.
        sb.Append(vbCr & vbLf)
    Next

    Response.Output.Write(sb.ToString())
    Response.Flush()
    Response.End()
End Sub

Gridview code

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource1" BackColor="White" BorderColor="#336666" BorderStyle="Double" BorderWidth="3px" CellPadding="4" Font-Size="Small">
            <Columns>
                <asp:TemplateField HeaderText="Handle">
                    <ItemTemplate>
                        <asp:TextBox ID="HandleTextBox" runat="server" Text='<%# Eval("Handle") %>'></asp:TextBox>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Title">
                    <ItemTemplate>
                        <asp:TextBox ID="TittleTextBox" runat="server" Text='<%# Eval("Title") %>'></asp:TextBox>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="BodyHTML">
                    <ItemTemplate>
                        <asp:TextBox ID="BodyHTMLTextBox" runat="server"  Text='<%# Eval("BodyHTML") %>'></asp:TextBox>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Vendor">
                    <ItemTemplate>
                        <asp:TextBox ID="VendorTextBox" runat="server" Text='<%# Eval("Vendor") %>'></asp:TextBox>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Type">
                    <ItemTemplate>
                        <asp:TextBox ID="TypeTextBox" runat="server" Text='<%# Eval("TYPE") %>'></asp:TextBox>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Tags">
                    <ItemTemplate>
                        <asp:TextBox ID="TagsTextBox" runat="server"  Text='<%# Eval("Tags") %>'></asp:TextBox>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
            <FooterStyle BackColor="White" ForeColor="#333333" />
            <HeaderStyle BackColor="#336666" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="#336666" ForeColor="White" HorizontalAlign="Center" />
            <RowStyle BackColor="White" ForeColor="#333333" />
            <SelectedRowStyle BackColor="#339966" Font-Bold="True" ForeColor="White" />
            <SortedAscendingCellStyle BackColor="#F7F7F7" />
            <SortedAscendingHeaderStyle BackColor="#487575" />
            <SortedDescendingCellStyle BackColor="#E5E5E5" />
            <SortedDescendingHeaderStyle BackColor="#275353" />
        </asp:GridView>

Example of a tempate:

enter image description here

Question

How can I convert the TemplateField gridview to a csv file.

1
If you can use javascipt, you can do it with a function using getElementsByTagName("tr")B.S.

1 Answers

0
votes

You can use the method that you have above but you need to specifically get the values of each textbox.

Dim txtType as Textbox
Dim txtVendor as Textbox


 For Each row As GridViewRow In GridView1.Rows

      txtType = CType(row.FindControl("TypeTextBox"), TextBox)
      txtVendor = CType(row.FindControl("VendorTextBox"), TextBox)

       sb.Append(txtType.Text & ",")
       sb.Append(txtVendor.Text & ",")

        'Append new line character.
        sb.Append(vbCr & vbLf)

    Next