1

i have a div element in my html,

<div id="userSolution" runat="server" text="1234512345123451234512345"></div>

Yes it does contain 25 characters,

i have a button:

<asp:Button id="saveGame" runat="server" text="Save Game" onclick="saveGame_Click" />

server Code:

protected void saveGame_Click(object sender, EventArgs e)
{
    string clientInput = userSolution.Attributes["text"];
}

So why... when i debug, does clientInput = "" ?

By my reckoning... text="1234512345123451234512345"

so string clientInput= userSolution.Attribute["text"]; should work right? :s

confused...

even if the div is:

<div id="userSolution" runat="server">1234512345123451234512345</div>

and i read

string clientId = userSolution.InnerHTML;

Still Fails

4
  • 4
    No. Div elements have never had a text attribute. Commented May 24, 2011 at 19:43
  • Even if i read it from innerHTML and make my text value part of divs innerhtml... still fails Commented May 24, 2011 at 19:48
  • Inner Text Doesnt work either... Its annoying because after i've stepped over the line to realise client input = "", if i go onto inspect element using chrome, i can see that the div still holds the value... before and after the server side event handler is called! Commented May 24, 2011 at 19:57
  • Did you use InnerText AND move the "12345.." between the <div> and </div> tags? There is no "text" attribute for a div. Doing both of those will work. Edit: I wrote that before I saw your last edit which says you did try that. Researching further... Commented May 24, 2011 at 20:06

3 Answers 3

3

You can't post back the data that way in a div tag if you are setting the value of the div on the client side. You'll need to use a form element, like a hidden textbox if you want to use a straight ASP.NET postback.

Here's a simple example. It uses a javascript event to set the text of a HiddenField when the button is pressed and then displays the text on postback.

<%@ Page Language="C#" AutoEventWireup="true" %>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script type="text/javascript">
    function copyToHiddenField() {
        var hidden = document.getElementById("<%= txtHidden.ClientID %>");
        var theDivText = document.getElementById("thedata").innerHTML;
        hidden.value = theDivText;
        return true;
    }
</script>
</head>
<body>
    <form id="form1" runat="server">
        <div id="thedata">Here's the data!</div>
        <asp:HiddenField ID="txtHidden" runat="server" />
        <asp:Button ID="btnSumbit" Text="Submit" OnClick="btnSumbit_OnClick" runat="server" />
        <asp:Literal ID="litText" runat="server" />
    </form>
</body>
</html>
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
    btnSumbit.Attributes.Add("onclick", "return copyToHiddenField();");
}

public void btnSumbit_OnClick(object sender, EventArgs e)
{
    litText.Text = txtHidden.Value;
}
</script>

EDIT
As stated in the answers/comments, you can use the InnerText property of a div tag when runat="server" is specified, but you will only be able read out the InnerText that is set when the page is rendered. Client side updates to the InnerText of the div will not be sent to the server on postback.

Sign up to request clarification or add additional context in comments.

4 Comments

You CAN do that if you really want to. Maybe the real question is why would the OP want to, and I think that's where you're headed with using a hidden field. If that's what you want though, why not use asp:HiddenField?
@ashelvey I would either use HiddenField (as in the sample code above) or use AJAX to send back the data. My point was just that since div isn't a form element, any data that is contained in the div isn't going to be a part of the POST request the browser makes when the submit button is clicked.
Right. It will be in ASP.NET state though, so it is possible, but it's definitely not the right way to do it as you've stated. I guess I'm just being pedantic. :) I agree with your approach, voting up.
Very well explained. Works Perfectly:) Thankyou
1

Try InnerText, and also change

<div id="userSolution" runat="server" text="1234512345123451234512345"></div>

to

<div id="userSolution" runat="server">1234512345123451234512345</div>

Comments

0

Use the asp:panel. That should give you what you're after (a div in client and .Text in server code

Edit

Wow, it's been a while and I actually thought there was a Text property - I even checked old projects in disbelief!! :-/

2 Comments

i used asp:panel Before i used div... and after asp:panel Failed i moved to divs... and now divs fail :'(
Panel does NOT have a property Text.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.