Solved

Changing the value of an HTML element through C# code

Posted on 2011-09-15
9
441 Views
Last Modified: 2012-05-12
Please let me know how I can change the value of an HTML element (say, an HTML textbox) on my ASP.NET page through C# code.

The problem in using "ScriptManager.RegisterClientScriptBlock" is that though I am able to change the value when the page loads, if the user tries to change the value of the element to something else, it automatically changes to what was defined using "ScriptManager.RegisterClientScriptBlock".

If possible, please provide some sample code. Thanks!
0
Comment
Question by:dshrenik
  • 3
  • 3
  • 2
  • +1
9 Comments
 
LVL 16

Expert Comment

by:Easwaran Paramasivam
ID: 36547564
You can access HTML elements in Codebehind by setting runat="server" attribute. Then you can set the value as same as ASP.NET control.
0
 

Author Comment

by:dshrenik
ID: 36547579
The problem is that the elements are created dynamically, on the fly. The user can create/ delete tables, and add/remove rows or columns.
0
 
LVL 12

Assisted Solution

by:Ramkisan Jagtap
Ramkisan Jagtap earned 166 total points
ID: 36547607
you need to set those input elements to runat="server".

Currently your control will be like this

<input type="text" name="email" id="myTextBox" />

 and you are not be able to access at code behind.

Now you'll be able to work with

<input type="text" name="email" id="myTextBox" runat="server" />

And read from it by using

string myStringFromTheInput = myTextBox.Text;

Regards,
Ram
0
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
LVL 16

Expert Comment

by:Easwaran Paramasivam
ID: 36547610
0
 

Author Comment

by:dshrenik
ID: 36547613
I cannot set them as runat="server".
The problem is that the elements are created dynamically, on the fly. The user can create/ delete tables, and add/remove rows or columns.
0
 
LVL 12

Expert Comment

by:Ramkisan Jagtap
ID: 36547615
You can add the runat="server" attribute to the control at the time of creation
0
 
LVL 12

Expert Comment

by:Ramkisan Jagtap
ID: 36547635
Can you please post the code for creation of dynamic controls?
0
 
LVL 2

Accepted Solution

by:
Lord_Death earned 167 total points
ID: 36547638
I think there isnt any work around for your Issue,

But :

the solution is to  save all of your user modifications like create/ delete tables, and add/remove rows or columns in a HiddenField.Value and then read the value in your code behind and re create the users structure.

and now you will be able to manage the dynamically created items in your C# code Behind.

HiddenFiled ID is the thing you should keep it in mind that will  change by IIS and you should use its ClientID in your JavaScript where you dynamically change the HTML
0
 
LVL 16

Assisted Solution

by:Easwaran Paramasivam
Easwaran Paramasivam earned 167 total points
ID: 36547717
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Problem to refer to value 8 45
Printing 1 50
Checkout Page Input Field not aligned 1 20
How do I pull the base url for use in html links 7 22
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now