Tech
Forums
Jobs
Books
Events
Videos
Live
More
Interviews
Certification
Training
Career
Members
News
Blogs
Contribute
An Article
A Blog
A Video
An Ebook
An Interview Question
Register
Login
2
Answers
How to access dynamically created Textbox ID in asp.net c#
Vivek Kumar Vishwas
7y
334
1
Reply
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-----------
<
%@ Page
Language
=
"C#"
AutoEventWireup
=
"true"
CodeFile
=
"Dynamic-TextBox.aspx.cs"
Inherits
=
"Dynamic_TextBox"
%
>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
>
<
html
xmlns
=
"http://www.w3.org/1999/xhtml"
>
<
head
runat
=
"server"
>
<
title
>
</
title
>
</
head
>
<
body
>
<
form
id
=
"form1"
runat
=
"server"
>
<
div
>
<
asp:PlaceHolder
ID
=
"PlaceHolder1"
runat
=
"server"
>
</
asp:PlaceHolder
>
<
asp:Label
ID
=
"Label1"
runat
=
"server"
>
</
asp:Label
>
<
asp:Button
ID
=
"addnewtext"
runat
=
"server"
Text
=
"Add"
onclick
=
"addnewtext_Click"
width
=
"76px"
/>
<
asp:Button
ID
=
"btn_Save"
runat
=
"server"
Text
=
"Save Value"
onclick
=
"btn_Save_Click"
/>
</
div
>
</
form
>
</
body
>
</
html
>
.cs--------
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Web;
using
System.Web.UI;
using
System.Web.UI.WebControls;
public
partial
class
Dynamic_TextBox : System.Web.UI.Page
{
static
Random random =
new
Random();
protected
void
Page_Load(
object
sender, EventArgs e)
{
}
TextBox tb;
static
int
i = 0;
protected
void
addnewtext_Click(
object
sender, EventArgs e)
{
i++;
for
(
int
j = 0; j <= i; j++)
{
GenerateRandomNumber();
tb =
new
TextBox();
tb.ID = j.ToString();
PlaceHolder1.Controls.Add(tb);
}
}
public
void
GenerateRandomNumber()
{
for
(
int
i = 0; i < 2; i++)
{
Label1.Text =
"The generated Random number is "
+ (Convert.ToString(random.Next(10, 200)));
// to specify range for random number
}
}
protected
void
btn_Save_Click(
object
sender, EventArgs e)
{
}
}
Post
Reset
Cancel
Answers (
2
)
Next Recommended Forum
Why the speak appears before the switching of picture.
Run time error generated