473,811 Members | 3,521 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Accessing javascript-generated textboxes with ASP.NET

I'm designing a form where the user can decide how many options for a
poll he wants to add. There's a "+" button to dynamically add
textboxes with javascript, without a postback.

How do I access these newly generated <input type="text" id="..." />
elements with ASP.NET to save it to a database?
Jun 27 '08 #1
3 1568
<je**********@g mail.comwrote in message
news:de******** *************** ***********@m3g 2000hsc.googleg roups.com...
I'm designing a form where the user can decide how many options for a
poll he wants to add. There's a "+" button to dynamically add
textboxes with javascript, without a postback.

How do I access these newly generated <input type="text" id="..." />
elements with ASP.NET to save it to a database?
In a nutshell, you can't - at least, not in the normal way, because the
postback process only knows about controls which were created before the
page was rendered to the browser...

Fortunately, there's a fairly simple workaround. The following assumes
you're initiating the postback by means of an <asp:Button /webcontrol...

1) Add a hidden textbox to your page e.g.
<asp:HiddenFiel d ID="MyHiddenFie ld" runat="server" />
http://msdn.microsoft.com/en-us/libr...ddenfield.aspx

2) Add a client-side function to the <asp:Button /e.g.
<asp:Button ID="MyButton" runat="server" Value="Submit"
OnClick="MyButt on_Click" OnClientClick=" getTextBoxes(); " />

3) Create the client-side JavaScript function getTextBoxes() e.g.

<script type="text/javascript">
function getTextBoxes()
{

}
</script>

4) Add code to the above function to walk through the dynamic textboxes,
adding their id and value to the value of MyHiddenField so that it ends up
looking like e.g.

"txt001=Fir st value;txt002=Se cond value;txt003=Th ird value;"

etc

5) When the page is posted back, split the value of MyHiddenField on the
semi-colon character. This will give you a string array something like:

txt001=First value
txt002=Second value
txt003=Third value

Iterate through each element of the string array and split it on the equals
character - that will give you the ID of each dynamic textbox and its value.
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Jun 27 '08 #2
pretty trival.

the javascript textbox postback values will be in the Request.Form
collection. the collection key will be the name the javascript gave as the
name of the <input>. if the names is the same for all the controls, the value
will be a comma seperated string of each value. more common is to append a
counter to the end of the same.

it would also be pretty trival to make a server control that dynamically
created a asp:textbox for each of the postback values, and exposed them as
controls. or the control could expose a collection of values.

-- bruce (sqlwork.com)
"je**********@g mail.com" wrote:
I'm designing a form where the user can decide how many options for a
poll he wants to add. There's a "+" button to dynamically add
textboxes with javascript, without a postback.

How do I access these newly generated <input type="text" id="..." />
elements with ASP.NET to save it to a database?
Jun 27 '08 #3
Thank you very much It worked...
"Sandesh Daddi" wrote:
>

You can use following code to get it work
<%@ Page Language="C#" AutoEventWireup ="true"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dt d">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitl ed Page</title>

<script type="text/javascript" >

function createTextBox(e vent)
{

//Get the reference of DIV where we want to show textboxes
var dv = document.getEle mentById("dvTex tBoxes");

//Create the Textbox and append that to DIV.
var tx = document.create Element("INPUT" );
tx.name = "txt";
tx.value= "1";
dv.appendChild( tx);
}
</script>
<script type="text/C#" runat="server" >

protected void Page_Load(objec t sender, EventArgs e)
{
try
{
if (Page.IsPostBac k)
{
String strValues = Request.Form["txt"];

String[] strV = strValues.Split (',');

for (int i = 0; i < strV.Length; i++)
{
lblError.Text += strV[i].ToString() + "\n";
}
}
}
catch (Exception ex)
{
throw ex;
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<input type="button" id="btnAddText " value="Add text" onclick="create TextBox(event)" />
<div id="dvTextBoxes ">
</div>
<asp:Button ID="btnSubmit" runat="server" Text="Submit"/>

<asp:Label ID="lblError" runat="server" ForeColor="RED" ></asp:Label>
</div>
</form>
</body>
</html>
Reply me if you want some thing else
Thank you
Saandesh Daddi
www.sanshark.com

Aug 26 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
5408
by: P G | last post by:
Hi all, I am a newbie to javascript. Please help me with this.... I have problem accessing subelement tag content from parent tag in javascript. I would like to change the color of text from red to blue. In the following code, I am able to set the visibility but not the color. Please make any suggestions. <HEAD> <script language="JavaScript">
3
2729
by: Scott Castillo | last post by:
Is it possible to access an SQL Server database from a Javascript client? I know you can transfer variables from middleware to Javascript client, but was wondering if you can directly access database from client? Thanks for any response. Scott
6
2749
by: Chris Styles | last post by:
Dear All, I've been using some code to verify form data quite happily, but i've recently changed the way my form is structured, and I can't get it to work now. Originally : The form is called "form1", and I have selects called "PORTA", "PORTB" ... etc...
5
14752
by: Vinod Kumar | last post by:
Hi All, Can I access a Javascript function written in an ASP.NET from the codebehind (C#) of that page? If yes, kindly give the code snippet to do this. Thanks and Regards Vinod Kumar
1
3142
by: CS Wong | last post by:
Hi, I have a page form where form elements are created dynamically using Javascript instead of programatically at the code-behind level. I have problems accessing the dynamically-created elements and would like to seek a solution for this. I had looked through several articles for accessing programatically-created dynamic elements such as: 1)
6
1207
by: X l e c t r i c | last post by:
I've been accessing this group through the WebTV interface. My days with WebTV are coming to an end. My question is how do I access this group with a PC. Is there a web site I go to ? If so, could someone please post the URL. Thanks, Art.
7
3830
by: Chuck Anderson | last post by:
I'm pretty much a JavaScript novice. I'm good at learning by example and changing those examples to suit my needs. That said .... ..... I have some select fields in a form I created for a database search that I am unable to figure out how to access. (The search is implemented in Php/MySQL.) The user enters search values for: name, address1, city, .... etc., ..... and for each of these they also select whether the search should...
3
2121
by: kosmodisk | last post by:
Hi, I'm having problem accessing javascript-created elements from opened window. This occurs only when I'm including another files in opened window, javascript or css. When I comment out "<link>" and "<script>" (point (0)), the script works fine. The opening window itself (point (1)) can access the object. When page has been loaded, then I can access the object also (point (3)). The script does find included files (debug_test2.css and...
3
10525
by: judy.j.miller | last post by:
Does anyone know why i can't access a form element value using dot notation in firefox, when i'm in a function. Works ok in the body. I'm trying to do this: var FarTemp = faren.temp.value; I can get at the value using the array method, the getelements by id method, and the bracket-with-the-element-name in it method. But the dot notation doesn't work, in firefox, in the function (which i have in the head).
0
9726
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10647
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10395
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10130
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9204
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6887
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5692
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4338
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3865
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.