Solved

Changing the value of an HTML element through C# code

Posted on 2011-09-15
9
443 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
[X]
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
  • 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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
This article discusses how to create an extensible mechanism for linked drop downs.
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…

733 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