Getting values from Javascript controls to dotnet

I am creating few controls like textboxes dynamically using javascript , now how do I get values of those controls once I submit the page in dotnet.

0
dotnet2
8/13/2007 3:48:24 PM
asp.net.client-side 24353 articles. 1 followers. Follow

20 Replies
767 Views

Similar Articles

[PageSpeed] 47

You have to use the Request.Form collection.  Note that this collection only stores the values of the textboxes.  Example:

    Dim strName as String = Request.Form("txtName")
 
Hope this helps!  Please mark the helpful post(s) as Answer.
Josh Stodola ← Come check out my blog!
0
JoshStodola
8/13/2007 4:52:59 PM

In your page_load you can have something like this:

Dim test As New TextBox

test.ID = "txtTest"

test.Text = "Test"

Me.Form.Controls.Add(test)

 

Then u can call this :

ctype(me.Form.FindControl("txtTest"),  TextBox)

This will return you a reference to your dynamically added textbox

 

 

0
Kiril2006
8/13/2007 5:51:28 PM

I am creating dynamic controls in javascript ..not in code behind..

If u want I will send u the code...

var tbl = document.getElementById('tblAddRows');

var lastRow = tbl.rows.length;

// if there's no header row in the table, then iteration = lastRow + 1

var iteration = lastRow;

var row = tbl.insertRow(lastRow-1);

var cellRight = row.insertCell(0);

var el = document.createElement('input');

el.type = 'text';

el.name = 'txtExternalEmail' + iteration;

el.id = 'txtExternalEmail' + iteration;

el.size = 20;

cellRight.appendChild(el); 

Now I  to want access these textbox values in code behind.... Send me code in C#

 

 

0
dotnet2
8/14/2007 12:55:32 AM

you need to add the following parameter to your textboxes:

el.runat = 'server';

That should allow you to access them from C#


Don't forget to mark the response as answered if it answers your question!

Matthew Yost
Software Engineer
MCP, MCAD
0
myost2921
8/14/2007 1:48:40 AM

But I specify runat server then I probably wont able to access these controls in Javascript

0
dotnet2
8/14/2007 1:59:57 AM

No What is this "probably" nonsense? No

If you come here for help and then you ignore everyone's ideas, than what are you trying to accomplish?  Did you even look at my reply?!

I am done trying to help you, that's for sure!


Josh Stodola ← Come check out my blog!
0
JoshStodola
8/14/2007 1:16:53 PM

You should only have a problem if you're using a master page (which causes the controls to be named different). If this is the case, you can use $get([control name]) in javascript to access the control (provided you have ASP.NET AJAX Extensions 1.0 loaded). 


Don't forget to mark the response as answered if it answers your question!

Matthew Yost
Software Engineer
MCP, MCAD
0
myost2921
8/14/2007 3:08:01 PM
I think you have not got my question. I tried accessing dynamic controls in CS file but in vain. 
0
dotnet2
8/15/2007 1:39:16 AM

yaa..I am using masterpages... I will send u my Javascript code... 

0
dotnet2
8/15/2007 1:40:54 AM

 

My javascript function... 

 var tbl = document.getElementById('tblAddRows');

var lastRow = tbl.rows.length;

// if there's no header row in the table, then iteration = lastRow + 1

var iteration = lastRow;

var row = tbl.insertRow(lastRow-1);

var cellRight = row.insertCell(0);

var el = document.createElement('input');

el.type = 'text';

el.name = 'txtExternalEmail' + iteration;

el.id = 'txtExternalEmail' + iteration;

el.size = 20;

cellRight.appendChild(el); 

Now I  to want access these textbox values in code behind.... Send me code in C#

0
dotnet2
8/15/2007 1:43:30 AM

dotnet2.0:
yaa..I am using masterpages... I will send u my Javascript code... 

 

Hi dotnet2.0,

Based on my understanding, you want to get the value of dynamic created (by javascript) textbox in code behind. For this scenario, I think Josh’s answer: Request.Form("txtName") is appropriate.

To clarify this case, I have posted the following code for your reference. If there has been any misunderstanding, please let me know.

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<script type="text/javascript">
    function AddRow()
    {
        var tbl = document.getElementById('tblAddRows');
        var lastRow = tbl.rows.length;
        // if there's no header row in the table, then iteration = lastRow + 1
        var iteration = lastRow;
        var row = tbl.insertRow(lastRow-1);
        var cellRight = row.insertCell(0);
        var el = document.createElement('input');
        el.type = 'text';
        el.name = 'txtExternalEmail' + iteration;
        el.id = 'txtExternalEmail' + iteration;
        el.size = 20;
        cellRight.appendChild(el);
    }
</script>
<table id="tblAddRows" border="1">
    <tr>
        <td>aaa
        </td>
    </tr>
    <tr>
        <td>bbb
        </td>
    </tr>
</table>
    <input id="Button1" type="button" value="Add Row" onclick="AddRow()" />
    <asp:Button ID="Button2" runat="server" Text="PostBack" OnClick="Button2_Click" />
</asp:Content>


*************** code behind
        protected void Button2_Click(object sender, EventArgs e)
        {
            string[] allkeys = Request.Form.AllKeys;
            for (int i = 0; i < allkeys.Length; i++)
            {
                if (allkeys[i].ToString().StartsWith("txtExternalEmail"))
                {
                    Response.Write(Request.Form[allkeys[i].ToString()]);
                }
            }
        }

 


Sincerely,
Benson Yu
Microsoft Online Community Support

Please remember to mark the replies as answers if they help and unmark them if they provide no help. This can be beneficial to other community members reading the thread.
0
Benson
8/17/2007 5:35:50 AM

Benson Yu - MSFT:
I think Josh’s answer: Request.Form("txtName") is appropriate.

Thanks Benson.  I posted that 4 days ago and he totally ignored it. Huh?


Josh Stodola ← Come check out my blog!
0
JoshStodola
8/17/2007 3:29:40 PM

How to retrieve value for the column (in grid) created dynamically?

0
saqib_dotnet
8/17/2007 5:53:56 PM

What value are you trying to retreive?  Is it just a plain text value?  Value from a textbox?

Please provide soem more details and then we can help.


Josh Stodola ← Come check out my blog!
0
JoshStodola
8/17/2007 7:11:40 PM

 One way of doing it is hide a textbox (set border size to 0, set tabstop to false and foreground colour to same as background.


Don't forget to click "Mark as Answer" on the post that helped you.
This credits that member, earns you a point and marks your thread as Resolved so we will all know you have been helped.
0
TATWORTH
8/17/2007 7:14:08 PM

TATWORTH:
set border size to 0, set tabstop to false and foreground colour to same as background

Huh?!  Hmm

Ever heard of style="display:none;" or style="visibility:hidden;"?


Josh Stodola ← Come check out my blog!
0
JoshStodola
8/17/2007 7:15:55 PM

 And it has been reported that you can use a hidden field control

See

  • http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.hiddenfield.aspx
  • http://forums.asp.net/p/1121169/1750966.aspx#1750966

Don't forget to click "Mark as Answer" on the post that helped you.
This credits that member, earns you a point and marks your thread as Resolved so we will all know you have been helped.
0
TATWORTH
8/17/2007 8:13:56 PM

 THANKS BUDDY..IT WORKED

0
dotnet2
8/18/2007 2:27:52 AM

                 for (int i = 0; i < GVName.Rows.Count; ++i)
                {

                     //if the control is label

                    Label var2 = GVName.Rows[i].FindControl("ASPControlID") as Label;

                     //if the control is textbox
                    TextBox var1 = (TextBox)GVName.Rows[i].FindControl("ASPControlID");

 

                    //

                     do others........
 

 

                } 


-- "Mark As Answer" If my reply helped you --
0
patuary
8/18/2007 6:38:29 AM

I've taken your example a little farther. It creates a new row at the bottom of the table. You can add ∞ number of rows. The postback will iterate thru the added rows then recreate the table with the new data:

I modified the ASPX file:
[yours] - var row = tbl.insertRow(lastRow-1); 
[mine] - var row = tbl.insertRow(lastRow);   <<=== I want to add to the bottom of the table

[mine] - added after the final </tr> I added a label to hold added rows - <asp:Label ID="lblAddrow" runat="server" />

I modified the .CS file:
[ADDED]  lblAddrow.Text = lblAddrow.Text + "<tr><td>" + Request.Form[allkeys[i].ToString()] + "</td></tr>";
[REMOVED] Response.Write(Request.Form[allkeys[i].ToString()]);

0
umayxa3
12/13/2008 12:24:28 PM
Reply:

Similar Artilces:

Get value of a client-side control
i have a webcontrol that has 2 viewstate variables and in the render event handler i write them as hidden input fields, because i need to be able to change them in javascript. the problem is, how can i access the changes made by the javascript? the control is actually a calendar written out as an html table and has 2 links for next/previous month which call javascript functions to change the month and then the table is updated with the new month (with AJAX). now, in my function that gets called an raisecallbackevent i need to know the values of the new month/year so that the javascript can c...

Getting Value from Client-side InputBox to .net variable
hi, im doing a login page for our timekeeping. Since there's no msgbox available on asp.net, im doing it dynamically via javascript. How do i Get the value frm a dynamically written javascript (such as inputbox) to a .net variable? here's the logic of my program, user logs in hits the button and sub-routine Login will be fired, if user is late -- call Check_Late Sub, this is whre the client-side script was built -- then after that i wanted to be able to get the value from an inputbox. '// if button login clicked Sub Login(Sender As Object, E As EventArgs) .......

Get the Dropdownlist text and value using Javascript from client side
Hi, I am encountering a problem about how to get the Dropdownlist text and value using Javascript from client side. I am using C# in Visual Studio 2005 and Javasript . The example codes are like: ( In this example code, I would like to access the text and value of the DropDownListReports in the run() function of Javascript. How can I do that? Any ideas? Thanks a lot. ) protected void Page_Load(object sender, EventArgs e) {              if (!Page.IsPostBack)            &nb...

How to get control value changed by javascript unload event in server side?
I use javascript to change the value of a HtmlInputHidden control during window.onunload event. However, the ASP.NET server side code can only get the value before the change. How can this be solved?   Hi, the clientside event "onunload" occurs at all times when the user navigate to a new page. A postback is not effected every time after the onunload event. Try to use the onsubmit event and attach an event method at the end of the page, e.g.: </body> <script> window.document.form1.attachEvent("onsubmit", SetValue); </script> </html>RegardsMarc A...

how to get value from client side script Prompt window to a session var in .net
hi guyz, i hav a problem with getting a value from a client-side inputbox to a session var in .net. is this possible? however, when i click submit button this is the code that's triggered. dim sb as New System.Text.StringBuilder sb.append("<script language=" & """" & "vbscript" & """" & ">") sb.append(vbcrlf) sb.append(vbcrlf) sb.append("do while xLate =" & """" & """") sb.append(vbcrlf) '//prompt window, val...

Question re: accessing web control values via client-side javascript
This block of variables gets generated dynamically from my _PreRender event of my formView : <SCRIPT Language='JavaScript'>  var chkInvertAliasID = 'ctl00_ContentPlaceHolder1_FormView1_chkEditInvert';  var chkAct_CurAliasID = 'ctl00_ContentPlaceHolder1_FormView1_chkEditAct_Cur';  var radBuy_SellAliasID = 'ctl00_ContentPlaceHolder1_FormView1_radioEditBuySell'; </SCRIPT> Now on the client-side I get a handle to above-declared var's : function QuoteConvention(obj) {         var objChkInvert = document.getElementById(chkInv...

How to get the Client side values in server side code.
Hi all, i have a proble i need to get the client side javascript values in C# code at server side. how can i pass those values? i am using version 1.1, i kno thatwe can use callback events mechnisam in 2.0 or ajax ,apart from that how we can pass? My problem is i need to stop the execution of the code based on after reciving values from the client   any ideas please???   Million thanks RamCheersRam MCP Hello Ram, a common approach is from javascript set an hidden field, then issue a postback. On the server-side, handle the post-back. Feel free to go deeper in your que...

Validation Controls in .NET ( Client side or server side?)
 i have a very basic question. We have all these validation controls in asp.net ( required field, range validator and others)..my question is the required field validator is a server side control. So does that mean that it does a post back?..is it client side or server side control  The only controls that post back are buttons and other inputs where you set AutoPostback="True", such as a dropdownlist or a textbox.  Another way of posting back is using an UpdatePanel and placing a trigger in it for a specified control and/or event. All controls are considered clien...

Getting value from a textbox control that was filled with a value from javascript
 I have a textbox control (seen below txtMileageDollars1) that gets filled with javascript based on the number of miles a user types into another textbox (txtMileageMiles1) control.  When I post back with the submit button, the value is blank.  I've tried setting EnableViewState to false, but still cannot get a value on postback.  Does anyone have a solution?   Miles<asp:TextBox ID="txtMileageMiles1" runat="server" Width="50px"></asp:TextBox>   Total (txtMileageMiles1 * rate with javascript)<asp:TextBox...

Getting hidden variable value from server side in client side
hi,        I am struck in problem. I have a hidden field in my form and i am setting some value in that.         Now i wnt to use this value from client side but i m not able to retrieve it. All i am getting there is an object.         I feel the hidden field is not able to hold that value.         <input type="hidden" id="hdnVar" runat="server" />         in code behind      ...

How to take value from client side control from server side code?
Hi all, I would like to know that, how to access client side control in server side code? Here is an example.. I am having the textBox in client side like <input type="text" id="textBoxUsername" name="textBoxUsername"/> I need to take the value from this textBox in button click event which is server side code. private void buttonShow(object sender, System.EventArgs e) { //I need to access this textBox here. } How to do that? Thanks R. eswaran. hi you need to make it run at server. <input type="text" id="textBoxUsername"...

server side control passing value of self in client side function
I have a server side dropdownlist control.It contains values as:"#00FFFF""#FF00FF""#FFFF00"When the selectedindex is changed by the client I want to assign the value of the dropdownlist to a <div id="example"></div> evelement.pseudo:document.getElementbyId('example').bgcolor=dropdownlist.valueHow can I do this? I think I somehow need to attach a function to my dropdownlist which passes its own value as a parameter to a function...any suggestions?Thanks!please mark answers as 'Answered' and post back solutions when you figure stuff out that isnt in the post already. You ...

How to access all the methods of server controls and infragistics controls from client-side javascript.
I have some web controls on my page, which i want to manipulate without going to the server, that is, from a client-side javascript. I know how to call them with the documents.forms.... but i do not know how to call their methods. For the text box, the method to manipulate it is 'value'. It's a property i think. for the html <select> it is the 'options' array. But for the others, I dont know.My question is the following:I have an infragistics control: the UltraWebTree.This is its code in html: <ignav:ultrawebtree id="UltraWebTree1" style="Z-INDEX: 108; LEFT: 352p...

to change value of Client Side Select html control from database value....
hello forum,  I have a select .. Demo.aspx <select id="DropAccType" name="DropAccType"> <option>Select</option> <option>Saving</option> <option >Current</option> <option>Regular</option> <option>Demat</option> </select> I want to set the value of select by fetching the data from daabase i.e. In my table if I have Value current then when i request the demo.aspx then in select Control the value Current should get selected..... Please help me to sort this out...   Thnx in advance.. Hi-I could be...

Web resources about - Getting values from Javascript controls to dotnet - asp.net.client-side

JavaScript Kit- Your comprehensive JavaScript, DHTML, CSS, and Ajax stop
Your comprehensive JavaScript, DHTML, CSS, and Ajax stop

Let's Code: Test-Driven Javascript by James Shore — Kickstarter
James Shore is raising funds for Let's Code: Test-Driven Javascript on Kickstarter! A screencast series focusing on rigorous, professional Javascript ...

Eloquent JavaScript: A Modern Introduction to Programming
... and pragmatics. I loved the tutorial-style game-like program development. This book rekindled my earliest joys of programming. Plus, JavaScript! ...

Lightbox (JavaScript) - Wikipedia, the free encyclopedia
On a Lightbox-enabled page, a user can click an image to have it magnified in a Lightbox window, which resizes itself according to the size of ...

Open Directory - Computers: Programming: Languages: JavaScript: Tools: Obfuscators
about dmoz - help Top : Computers : Programming : Languages : JavaScript : Tools : Obfuscators Tools for obfuscation/compression JavaScript code. ...

Facebook File-Transfer App Pipe Relaunches, Now Works Via JavaScript, WebRTC
... allowed Facebook users to send and receive files up to 1 gigabit in size. A new version of Pipe debuted Monday, and the app now works via JavaScript ...

Under the Hood: The JavaScript SDK - The use of polyfills - Facebook
Facebook Engineering hat eine Notiz mit dem Titel Under the Hood: The JavaScript SDK - The use of polyfills geschrieben. Du kannst den vollständigen ...

JavaScript SDK - Facebook-Entwickler
Overview The JavaScript SDK provides a rich set of client-side functionality for accessing Facebook...

Senior Front-End Developer (Javascript and AJAX)
Company: Sometrics Category: Software Development Location: Los Angeles, CA How to Apply: jobs@sometrics.com Description: We are looking for ...

Badass JavaScript (@badass_js) on Twitter
WebGLWorker is an open source library that allows using WebGL from Web Workers. Great work as always from @kripken . blog.mozilla.org/research/2014/… ...

Resources last updated: 12/24/2015 4:19:49 PM