Click here to Skip to main content
15,886,724 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hye all.

How to retrieve dynamically-created-textbox text inside dynamic template field of a gridview? Here's my code.


------------------------------------HTML ------------------------------

XML
<div>
    <table>
        <tr>
            <td>
                No of Rows:
            </td>
            <td>
                <asp:TextBox ID="txtNumber" runat="server" ValidationGroup="vgNumbers">

                </asp:TextBox>
            </td>
            <td>
                <asp:Button ID="btnPopulate" runat="server" Text="Populate" ValidationGroup="vgNumbers" />
            </td>
        </tr>
        <tr>
            <td align="center" colspan="3">
                <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
                    <HeaderStyle ForeColor="White" BackColor="DarkSlateGray" />
                </asp:GridView>
            </td>
        </tr>
        <tr>
            <td colspan="3">
                <asp:Button ID="btnViewData" runat="server" Text="View Data" Visible="false" />
                <br />
                <br />
                <asp:Label ID="Label1" runat="server"></asp:Label>
            </td>
        </tr>
    </table>
</div>



------------------------------------CODE BEHIND ------------------------------

VB
Protected Sub btnPopulate_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnPopulate.Click

      Me.GridView1.Controls.Clear()
      PopulateGrid()

  End Sub

  Public Sub PopulateGrid()

      Dim tf As New TemplateField()
      tf.HeaderText = "Header"
      GridView1.Columns.Add(tf)

      GridView1.DataSource = GetmyDataTable(CInt(txtNumber.Text))
      GridView1.DataBind()

      btnViewData.Visible = True

  End Sub

  Public Function GetmyDataTable(ByVal intNumber As Int32) As DataTable
      Dim datatable As New DataTable()
      Dim dataRow As DataRow = Nothing

      For i As Integer = 0 To intNumber - 1
          dataRow = datatable.NewRow()

          datatable.Rows.Add(dataRow)

          datatable.AcceptChanges()

      Next

      datatable.AcceptChanges()

      Return datatable
  End Function

  Protected Sub btnAddtoDB_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnViewData.Click

      For i As Integer = 0 To GridView1.Rows.Count - 1
          Dim myNames As New TextBox
          myNames = CType(GridView1.Rows(i).FindControl("txtName"), TextBox)

          Label1.Text += Environment.NewLine & myNames.Text

      Next


      txtNumber.Text = ""

  End Sub

  Private Sub GridView1_RowDataBound(sender As Object, e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
      If e.Row.RowType = DataControlRowType.DataRow Then
          For i As Integer = 0 To e.Row.Cells.Count - 1
              Dim txt As New TextBox
              txt.ID = "txtName" & e.Row.RowIndex & "_" & i

              txt.Width = 60
              txt.BackColor = ColorTranslator.FromHtml("#C6E2FF")
              txt.BorderColor = Color.Black
              txt.BorderStyle = BorderStyle.Solid
              txt.BorderWidth = 1

              e.Row.Cells(i).Controls.Add(txt)
          Next
      End If

  End Sub
Posted
Updated 13-Apr-15 21:49pm
v3

1 solution

I have solved this by using Request.Form(key) in PreInit function.


VB
Protected Sub Page_PreInit(sender As Object, e As EventArgs) Handles Me.PreInit

       Dim keys As List(Of String) = Request.Form.AllKeys.Where(Function(key) key.Contains("txtName")).ToList()
       Dim i As Integer = 1
       For Each key As String In keys

           Dim txt As String = Request.Form(key)
           Label1.Text += Environment.NewLine & txt

           i += 1
       Next
   End Sub
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900