You must Sign In to post a response.
  • Category: ASP.NET

    Dynamic TextBox in GridView

    Hi to all,
    I have an page with textboxes and Labels. In that I have one more Control is GridView. In that have to Load based on Table count from SQL(It is dynamic arrive table). while Loading the Gridview, TExtBox have to create based on that. After Loading i have to insert the which is type in that those textboxes in Gridview in other table. Kinldy guide me do that one.

    Ex: My table name is Emp with Columns (Name. Ph.no) means that hasve to Show to Two Lable ans two textbox. I have to store that values.

    so, based on that columns it have to Dynamically generate.
  • #766379
    You can Dynamically add BoundField and TemplateField Columns to GridView in ASP.Net, first you need to make 'AutoGenerateColumns = "false"', then fire 'OnRowDataBound' event, in that you can have below code,

    protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
    {
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
    TextBox txtCountry = new TextBox();
    txtCountry.ID = "txtCountry";
    txtCountry.Text = (e.Row.DataItem as DataRowView).Row["Country"].ToString();
    e.Row.Cells[1].Controls.Add(txtCountry);

    LinkButton lnkView = new LinkButton();
    lnkView.ID = "lnkView";
    lnkView.Text = "View";
    lnkView.Click += ViewDetails;
    lnkView.CommandArgument = (e.Row.DataItem as DataRowView).Row["Id"].ToString();
    e.Row.Cells[2].Controls.Add(lnkView);
    }
    }

    hope it helps

    Thanks
    Koolprasd2003
    Editor, DotNetSpider MVM
    Microsoft MVP 2014 [ASP.NET/IIS]

  • #766380
    Hi

    you can try this code

    Client Side

    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="true">
    </asp:GridView>
    <asp:Button ID="Button1" Text="AddRow" OnClick="Button1_Click" runat="server" />


    Server Side


    int cellCount = this.GridView1.Rows[0].Cells.Count;
    int rowsCount = this.GridView1.Rows.Count;
    for (int j = 0; j < cellCount; j++)
    {
    TextBox textBox = new TextBox();
    textBox.ID = "txtDynName" + j.ToString();
    textBox.Attributes.Add("runat", "server");
    textBox.CssClass = "Color";
    this.GridView1.Rows[rowsCount - 1].Cells[j].Controls.Add(textBox);
    }

    Name : Dotnet Developer-2015
    Email Id :kumaraspcode2009@gmail.com

    'Not by might nor by power, but by my Spirit,' says the LORD Almighty.

  • #766384
    Hi,

    As per my understand the post you have textbox & button in your gridview footer, while click on the button you want to save those records into database table and rebind those records in your gridview itemtemplate.

    Design your page like below

    <asp:GridView ID="GV" runat="server" CellPadding="4" ForeColor="#333333"
    GridLines="None" AutoGenerateColumns="false" OnRowCommand="GV_OnRowCommand" ShowFooter="true">
    <Columns>

    <asp:TemplateField>
    <ItemTemplate>
    <asp:Labe ID="lblVal" runat="server" Text='<%#DataBinder.Eval(Container.DataItem,"Value") %>'></asp:Label>
    </ItemTemplate>
    <FooterTemplate>
    <asp:TextBox ID="txt1" runat="server"></asp:TextBox>
    </FooterTemplate>
    </asp:TemplateField>
    <asp:TemplateField>
    <FooterTemplate>
    <asp:LinkButton ID="lnkAdd" runat="server" CommandName="Add"></asp:TextBox>
    </FooterTemplate>
    </asp:TemplateField>
    </Columns>
    </asp:GridView>



    OnRowcommand Event insert records into database, as usual bind your data to gridview in page load event.


    Protected void GV_OnRowCommand (Object sender, GridViewCommandEventArgs e)
    {
    if(e.CommandName=="Add")
    {
    //insert records into database
    }
    }



    Hope this will helpful to you...

    --------------------------------------------------------------------------------
    Give respect to your work, Instead of trying to impress your boss.

    N@veen
    Blog : http://naveens-dotnet.blogspot.in/

  • #766520
    Hi,

    I have done this activity on Normal table itself in Design Page. But this is almost closed on your Requirement. Kindly use the Following Code for Requirement. Kindly modify based on your need.
    design:
    <h1>Dynamic TextBoxes </h1>
    <table >
    <tr>
    <td>

    </td>
    <td>

     </td>
    <td>

     </td>
    </tr>
    <tr>
    <td>
    <asp:PlaceHolder ID="text" runat="server"></asp:PlaceHolder>
    </td>
    <td>
    <asp:PlaceHolder ID="text1" runat="server"></asp:PlaceHolder>
    </td>
    <td>
     </td>
    </tr>
    <tr>
    <td>
    <asp:Button ID="Create_TextBox" runat="server" Text="Submit"
    ?nClick="Create_TextBox_Click" />

    </td>
    <td>
     </td>
    <td>
     </td>
    </tr>

    <tr>
    <td>
    <asp:PlaceHolder ID="Viewtextvalue" runat="server"></asp:PlaceHolder>
    </td>
    <td>
     </td>
    <td>
     </td>
    </tr>
    </table>
    </div>


Sign In to post your comments