3

I'm a newbie in programming thus do pardon me.

I have a .js game that outputs a variable called "score".

I require the variable to be able to be displayed on my asp label and after which grabbing its value for database storage onClick()

Currently what I have been able to do were:

@ .js:

    document.getElementById('score').value = score;

@ .aspx:

    <asp:Label runat="server"  ID="score"></asp:Label>

The score is reflected on this label right now. However, I need to do a additional function which is to obtain the value of 'score' from the label and use it as a String in C#

1
  • Side note: asp-classic is unlikely the tag you want... at least your code is in ASP.Net... re-tagged. Commented Nov 6, 2012 at 5:25

2 Answers 2

3

Use ClientID of server control as id score would have changed in the generated html by asp.

Change

document.getElementById('score').value = score;

To

document.getElementById('<%= score.ClientID %>').value = score;
Sign up to request clarification or add additional context in comments.

10 Comments

Hi Adil, I have used your above codes. However, when i call label.Text in my C# code, it shows null
Label text is not posted back you can use hidden field to store the value and access it in code behind. msdn.microsoft.com/en-us/library/…
This error occurred when I use the hiddenfield Error 10 'System.Web.UI.WebControls.HiddenField' does not contain a definition for 'Text' and no extension method 'Text' accepting a first argument of type 'System.Web.UI.WebControls.HiddenField' could be found (are you missing a using directive or an assembly reference?)
You have to use Value instead of Text
.js: document.getElementById('<%= score.ClientID %>').value = score;
|
1

Could you please try the below suggession.

in .js

document.getElementById('<%= lblScore.ClientID %>').innerHTML = 'Test Value';
document.getElementById('<%= hdnScore.ClientID %>').value = 'Test Value';

in .aspx

<asp:Label runat="server" ID="lblScore"></asp:Label>
<asp:HiddenField ID="hdnScore" runat="server" />

onclick event

var score = hdnScore.Value;

Hope this will help you to fix the issue.

Comments

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.