Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Changing the value of an HTML element through C# code

Posted on 2011-09-15
9
442 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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…

792 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