Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 475
  • 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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