How can I get the value from the client-side created element object?

Q

Quentin Huo

Hi:

I created a web-form user control. And in client-site, there is a link which
will create a Input element object when it is clicked.

<script language=javascript>

.......
var nameInput = document.createElement("input");
nameInput.setAttribute("id", "authorname");
......

</script>

But when click "save" button" on this page, I can not get the value of the
created element object in the user control by (in C#)

.......
string x=Request["authorname"];
.......

So how can I get the value from the client-side created element in a user
control of ASP.NET?

Thanks

Q.
 
F

Filip

not sure if this wold help, but you might want to add:
runat="server"
when creating the object
 
E

Eliyahu Goldin

No, this won't help on the client side. "runat" is an instruction for
ASP.NET. There is no ASP.NET on the client side.

There are several way how to transmit values from client to server. You can
use <input type=hidden> element, or you can add a parameter to the query
string, not limited to these 2 only.

Eliyahu

Filip said:
not sure if this wold help, but you might want to add:
runat="server"
when creating the object

Quentin Huo said:
Hi:

I created a web-form user control. And in client-site, there is a link which
will create a Input element object when it is clicked.

<script language=javascript>

......
var nameInput = document.createElement("input");
nameInput.setAttribute("id", "authorname");
.....

</script>

But when click "save" button" on this page, I can not get the value of the
created element object in the user control by (in C#)

......
string x=Request["authorname"];
......

So how can I get the value from the client-side created element in a user
control of ASP.NET?

Thanks

Q.
 
V

Vidar Petursson

Hi

This should work

Client
var e = document.createElement("INPUT");
e.name = "theNAME"; // Name of element
document.forms["FORMNAME"].appendChild(e);

Server
string sRes = (Request.Form["theNAME"] != null ) ?
Request.Form["theNAME"].ToString() : "";

--
Best Regards
Vidar Petursson
==============================
Microsoft Scripting MVP
http://www.microsoft.com/technet/scriptcenter
==============================
 
Q

Quentin Huo

Hi:

Unfortuately, it doesn't work. But anyway, thanks for you guys.

So I am thinking another way to handle this: how can I create elements
dynamically in server-side?

I am trying to create a page for edit books' information. Some books have
one author and some books have more than one authors. So, I want to give
only one text box for the author name. And I will create a link saying "more
authors". When a book has two authors, click the "more authors" link and a
new text box will be created in the server-side.

I know I should create a method in the code-behind to handle the click event
for that link. But I want to know how to create an element ( such as input
box) in the server-side?

I tried to create a asp:label named "newAuthors" (ID) in the page, and I
create the new text box as:

.......

newAuthors.Text = "<input runat=server id=newAuthors name=newAuthors >";

.......

but if I put a name in this box and post the page, the name that I input
cannot be shown in the box anymore (I think it is not in the VIEWSTATE). So
I think there should be another better way to create an element in ASP.NET.

Any better way?

Thank you very much!

Q.




Vidar Petursson said:
Hi

This should work

Client
var e = document.createElement("INPUT");
e.name = "theNAME"; // Name of element
document.forms["FORMNAME"].appendChild(e);

Server
string sRes = (Request.Form["theNAME"] != null ) ?
Request.Form["theNAME"].ToString() : "";

--
Best Regards
Vidar Petursson
==============================
Microsoft Scripting MVP
http://www.microsoft.com/technet/scriptcenter
==============================
Quentin Huo said:
Hi:

I created a web-form user control. And in client-site, there is a link
which
will create a Input element object when it is clicked.

<script language=javascript>

......
var nameInput = document.createElement("input");
nameInput.setAttribute("id", "authorname");
.....

</script>

But when click "save" button" on this page, I can not get the value of
the
created element object in the user control by (in C#)

......
string x=Request["authorname"];
......

So how can I get the value from the client-side created element in a user
control of ASP.NET?

Thanks

Q.
 
P

Patrice

Check :
- you have a name attribute
- it is inside the client side form tag
- it is enabled (likely by default)

Else it is not posted to the server

Patrice
 
V

Vidar Petursson

Hi

Hmmm just tested it here and it worked perfectly, no problem getting
the data in input....

--
Best Regards
Vidar Petursson
==============================
Microsoft Visual: Scripting MVP 2000-2004
http://www.icysoft.com/
http://www.deus-x.com/ Instant e-commerce
http://www.microsoft.com/technet/scriptcenter/
Playground: http://213.190.104.211/ ( IE 5.5+ only )

No matter where you go there you are
==============================
Quentin Huo said:
Hi:

Unfortuately, it doesn't work. But anyway, thanks for you guys.

So I am thinking another way to handle this: how can I create elements
dynamically in server-side?

I am trying to create a page for edit books' information. Some books have
one author and some books have more than one authors. So, I want to give
only one text box for the author name. And I will create a link saying
"more
authors". When a book has two authors, click the "more authors" link and a
new text box will be created in the server-side.

I know I should create a method in the code-behind to handle the click
event
for that link. But I want to know how to create an element ( such as input
box) in the server-side?

I tried to create a asp:label named "newAuthors" (ID) in the page, and I
create the new text box as:

......

newAuthors.Text = "<input runat=server id=newAuthors name=newAuthors >";

......

but if I put a name in this box and post the page, the name that I input
cannot be shown in the box anymore (I think it is not in the VIEWSTATE).
So
I think there should be another better way to create an element in
ASP.NET.

Any better way?

Thank you very much!

Q.




Vidar Petursson said:
Hi

This should work

Client
var e = document.createElement("INPUT");
e.name = "theNAME"; // Name of element
document.forms["FORMNAME"].appendChild(e);

Server
string sRes = (Request.Form["theNAME"] != null ) ?
Request.Form["theNAME"].ToString() : "";

--
Best Regards
Vidar Petursson
==============================
Microsoft Scripting MVP
http://www.microsoft.com/technet/scriptcenter
==============================
Quentin Huo said:
Hi:

I created a web-form user control. And in client-site, there is a link
which
will create a Input element object when it is clicked.

<script language=javascript>

......
var nameInput = document.createElement("input");
nameInput.setAttribute("id", "authorname");
.....

</script>

But when click "save" button" on this page, I can not get the value of
the
created element object in the user control by (in C#)

......
string x=Request["authorname"];
......

So how can I get the value from the client-side created element in a user
control of ASP.NET?

Thanks

Q.
 
Q

Quentin Huo

It WORKS! (I made a mistake before).

Thanks a lot!

Q.

Vidar Petursson said:
Hi

Hmmm just tested it here and it worked perfectly, no problem getting
the data in input....

--
Best Regards
Vidar Petursson
==============================
Microsoft Visual: Scripting MVP 2000-2004
http://www.icysoft.com/
http://www.deus-x.com/ Instant e-commerce
http://www.microsoft.com/technet/scriptcenter/
Playground: http://213.190.104.211/ ( IE 5.5+ only )

No matter where you go there you are
==============================
Quentin Huo said:
Hi:

Unfortuately, it doesn't work. But anyway, thanks for you guys.

So I am thinking another way to handle this: how can I create elements
dynamically in server-side?

I am trying to create a page for edit books' information. Some books have
one author and some books have more than one authors. So, I want to give
only one text box for the author name. And I will create a link saying
"more
authors". When a book has two authors, click the "more authors" link and a
new text box will be created in the server-side.

I know I should create a method in the code-behind to handle the click
event
for that link. But I want to know how to create an element ( such as input
box) in the server-side?

I tried to create a asp:label named "newAuthors" (ID) in the page, and I
create the new text box as:

......

newAuthors.Text = "<input runat=server id=newAuthors name=newAuthors >";

......

but if I put a name in this box and post the page, the name that I input
cannot be shown in the box anymore (I think it is not in the VIEWSTATE).
So
I think there should be another better way to create an element in
ASP.NET.

Any better way?

Thank you very much!

Q.




Vidar Petursson said:
Hi

This should work

Client
var e = document.createElement("INPUT");
e.name = "theNAME"; // Name of element
document.forms["FORMNAME"].appendChild(e);

Server
string sRes = (Request.Form["theNAME"] != null ) ?
Request.Form["theNAME"].ToString() : "";

--
Best Regards
Vidar Petursson
==============================
Microsoft Scripting MVP
http://www.microsoft.com/technet/scriptcenter
==============================
Hi:

I created a web-form user control. And in client-site, there is a link
which
will create a Input element object when it is clicked.

<script language=javascript>

......
var nameInput = document.createElement("input");
nameInput.setAttribute("id", "authorname");
.....

</script>

But when click "save" button" on this page, I can not get the value of
the
created element object in the user control by (in C#)

......
string x=Request["authorname"];
......

So how can I get the value from the client-side created element in a user
control of ASP.NET?

Thanks

Q.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,190
Latest member
ClayE7480

Latest Threads

Top