• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 490
  • Last Modified:

Changing the value of an HTML element through C# code

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
dshrenik
Asked:
dshrenik
  • 3
  • 3
  • 2
  • +1
3 Solutions
 
Easwaran ParamasivamCommented:
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
 
dshrenikAuthor Commented:
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
 
Ramkisan JagtapLead DeveloperCommented:
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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

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

 
dshrenikAuthor Commented:
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
 
Ramkisan JagtapLead DeveloperCommented:
You can add the runat="server" attribute to the control at the time of creation
0
 
Ramkisan JagtapLead DeveloperCommented:
Can you please post the code for creation of dynamic controls?
0
 
Lord_DeathCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

  • 3
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now