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
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.
EDIT
As stated in the answers/comments, you can use the
InnerTextproperty of adivtag whenrunat="server"is specified, but you will only be able read out theInnerTextthat is set when the page is rendered. Client side updates to theInnerTextof the div will not be sent to the server on postback.