1

I 'm newer in development .NET

I create a label dynamically in javascript. After pressing a button that is created on the page.

<label name="lblPerson1" id="lblPerson1" runat="server">Person:</label>

After that I want to capture this label in server side.

Somebody help me!

EDIT:

what I want is to be able to create a set of labels and input when you click a button. (on the client side) Then when you click in another button to change the text of the labels (on the server side) It is possibel?

user1671697
  • 271
  • 1
  • 2
  • 9
  • no u cant do that... additionally maybe you could have a div or something that has runat="server" and then u can create that label within that. and then on server sided code you can do the DivName.InnerHtml and look for its content to see whats created – JohnMalkowich Mar 06 '13 at 15:34
  • are you looking something like this? http://stackoverflow.com/questions/5381139/asp-net-linkbutton-send-value-to-code-behind-onclick – ncubica Mar 06 '13 at 15:35
  • 1
    Why are you doing this? May be there is a better way... – codingbiz Mar 06 '13 at 15:36
  • 1
    What are you trying to achieve? Perhaps we could suggest an alternative solution if we know why you're trying to capture this label? – Luke Baughan Mar 06 '13 at 15:38
  • 1
    Remove runat="server" because it makes no sense - you are adding a label html tag in client side DOM. What do you mean by "capture this label in server side"? Do you want to get the text between label tags? – Floremin Mar 06 '13 at 15:38
  • 1
    @codingbiz great minds think alike! ;o) – Luke Baughan Mar 06 '13 at 15:38
  • what I want is to be able to create a set of labels and input when you click a button. (on the client side) Then when you click in another button to change the text of the labels (on the server side) It is possibel? – user1671697 Mar 06 '13 at 15:44
  • It is possible, but trying to mix client side control creation with runat-server controls is not the good way to do it. Go for a full server-side logic (with postbacks) or for a full client side logic (with some DOM manipulation, and javascript serialisation of the generated controls) – jbl Mar 06 '13 at 16:15

2 Answers2

1

The label won't be posted to the server as part of the post request because it is not an input control.

What you can do (for example) :

  • have a asp.net hidden input control where, using some javascript, you will store the label's html markup upon click on the submit button, before submitting the form
  • or, upon click on the submit button, perform a post ajax request, where you will put your label's html markup

And BTW, I don't see how you will be able to use the runat="server" unless you parse the HTML markup server side to generate server controls (which does not seem a simple idea to me)

Hope this will help

jbl
  • 15,179
  • 3
  • 34
  • 101
-1

Because this control is not created server side, you will need to use the FindControl method, like this:

With a control like this:

    <label name="lblPerson1" id="lblPerson2" runat="server">Person:</label>

    Dim myLabel As HtmlGenericControl
    myLabel = Me.lblPerson2

    Dim test As String = myLabel.InnerText

Let me know if this works or not.

jason
  • 3,821
  • 10
  • 63
  • 120
  • i guess this wont work? as the label is created on client side and cannot be running at server. – JohnMalkowich Mar 06 '13 at 15:36
  • No, it should work. You can find controls that are created on the client side. You just need to know it's ID. That's what the FindControl method is used for. Let me test and if it works Ill post back an example – jason Mar 06 '13 at 15:37
  • no i guess not because the runat=server doesnt event count when its created on client. so as far as the server sided code knows, there is nothing called lblPerson1 because it does not runat server – JohnMalkowich Mar 06 '13 at 15:39