Vivek Kumar Vishwas

Vivek Kumar Vishwas

  • NA
  • 115
  • 56.1k

How to access dynamically created Textbox ID in asp.net c#

Nov 8 2017 11:50 PM
Hello Friends !,
 
How to access dynamically created textbox value or id to save into database.
 
Textbox is creating dynamically successfully. Now what I want suppose I fill the text and when I press button to save that value into database table. How do I get that textbox id.
Below what i tried to do please check and resolve this problem soon.
 
aspx-----------
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Dynamic-TextBox.aspx.cs" Inherits="Dynamic_TextBox" %>  
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  3. <html xmlns="http://www.w3.org/1999/xhtml">  
  4. <head runat="server">  
  5. <title></title>  
  6. </head>  
  7. <body>  
  8. <form id="form1" runat="server">  
  9. <div>  
  10. <asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>  
  11. <asp:Label ID="Label1" runat="server"></asp:Label>  
  12. <asp:Button ID="addnewtext" runat="server" Text="Add" onclick="addnewtext_Click" width="76px" />  
  13. <asp:Button ID="btn_Save" runat="server" Text="Save Value" onclick="btn_Save_Click" />  
  14. </div>  
  15. </form>  
  16. </body>  
  17. </html>  
.cs--------
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.UI;  
  6. using System.Web.UI.WebControls;  
  7. public partial class Dynamic_TextBox : System.Web.UI.Page  
  8. {  
  9. static Random random = new Random();  
  10. protected void Page_Load(object sender, EventArgs e)  
  11. {  
  12. }  
  13. TextBox tb;  
  14. static int i = 0;  
  15. protected void addnewtext_Click(object sender, EventArgs e)  
  16. {  
  17. i++;  
  18. for (int j = 0; j <= i; j++)  
  19. {  
  20. GenerateRandomNumber();  
  21. tb = new TextBox();  
  22. tb.ID = j.ToString();  
  23. PlaceHolder1.Controls.Add(tb);  
  24. }  
  25. }  
  26. public void GenerateRandomNumber()  
  27. {  
  28. for (int i = 0; i < 2; i++)  
  29. {  
  30. Label1.Text = "The generated Random number is " + (Convert.ToString(random.Next(10, 200))); // to specify range for random number  
  31. }  
  32. }  
  33. protected void btn_Save_Click(object sender, EventArgs e)  
  34. {  
  35. }  
  36. }

Answers (3)