Damini Damini - 7 months ago 19
Vb.net Question

ASP.NET Not getting CommandArgument Value from LinkButton onClick(VB)

I need to get the value from the commandArgument in the LinkButton but when I hover on the letter B for example, I see that I get this instead of the letter alone:
enter image description here

These are the codes for it:

'problem here with the commandArgument
Sub LetterGuessed(sender As Object, e As CommandEventArgs)
'First, make the letter selected disabled

Dim clickedButton As LinkButton = e.CommandArgument
clickedButton.Enabled = False
clickedButton.ForeColor = Color.Red

'Now, determine if the letter is in the word
If Session("hangman_word").ToString().ToLower().IndexOf(e.CommandArgument.ToLower()) >= 0 Then
'The letter was found
Dim i As Integer
Dim current As String = String.Empty
For i = 0 To Session("hangman_word").ToString().Length - 1
If Session("hangman_word").ToString().Substring(i, 1).ToLower() = e.CommandArgument.ToLower() Then
current &= Session("hangman_word").ToString().Substring(i, 1)
Else
current &= Session("current_word").ToString().Substring(i, 1)
End If
Next i

Session("current_word") = current
DisplayCurrentWord()

'See if they have guessed the word correctly!
If Session("hangman_word").ToString() = Session("current_word").ToString() Then
EndGame(True)
End If
Else
'The letter was not found, increment the # of wrong guesses
Session("wrong_guesses") = Convert.ToInt32(Session("wrong_guesses")) + 1

'Update the hangman image
hangmanImage.ImageUrl = "Content/hangman/hang_" & Session("wrong_guesses").ToString() & ".gif"

If Convert.ToInt32(Session("wrong_guesses")) >= 6 Then
'Eep, the person has lost
EndGame(False)
End If
End If
End Sub


This is the form:

<form>
<asp:image id="hangmanImage" runat="server" />
<p>

<asp:literal runat="server" id="currentWord" />
<p>
<asp:LinkButton runat="server" id="A" CommandArgument="A" Text="A" OnCommand="LetterGuessed" /> |
<asp:LinkButton runat="server" id="B" CommandArgument="B" Text="B" OnCommand="LetterGuessed" /> |
<asp:LinkButton runat="server" id="C" CommandArgument="C" Text="C" OnCommand="LetterGuessed" /> |
<asp:LinkButton runat="server" id="D" CommandArgument="D" Text="D" OnCommand="LetterGuessed" /> |
<asp:LinkButton runat="server" id="E" CommandArgument="E" Text="E" OnCommand="LetterGuessed" /> |
<asp:LinkButton runat="server" id="F" CommandArgument="F" Text="F" OnCommand="LetterGuessed" /> |
<asp:LinkButton runat="server" id="G" CommandArgument="G" Text="G" OnCommand="LetterGuessed" /> |
<asp:LinkButton runat="server" id="H" CommandArgument="H" Text="H" OnCommand="LetterGuessed" /> |
<asp:LinkButton runat="server" id="I" CommandArgument="I" Text="I" OnCommand="LetterGuessed" /> |
<asp:LinkButton runat="server" id="J" CommandArgument="J" Text="J" OnCommand="LetterGuessed" /> |
<asp:LinkButton runat="server" id="K" CommandArgument="K" Text="K" OnCommand="LetterGuessed" /> |
<asp:LinkButton runat="server" id="L" CommandArgument="L" Text="L" OnCommand="LetterGuessed" /> |
<asp:LinkButton runat="server" id="M" CommandArgument="M" Text="M" OnCommand="LetterGuessed" /> |
<asp:LinkButton runat="server" id="N" CommandArgument="N" Text="N" OnCommand="LetterGuessed" /> |
<asp:LinkButton runat="server" id="O" CommandArgument="O" Text="O" OnCommand="LetterGuessed" /> |
<asp:LinkButton runat="server" id="P" CommandArgument="P" Text="P" OnCommand="LetterGuessed" /> |
<asp:LinkButton runat="server" id="Q" CommandArgument="Q" Text="Q" OnCommand="LetterGuessed" /> |
<asp:LinkButton runat="server" id="R" CommandArgument="R" Text="R" OnCommand="LetterGuessed" /> |
<asp:LinkButton runat="server" id="S" CommandArgument="S" Text="S" OnCommand="LetterGuessed" /> |
<asp:LinkButton runat="server" id="T" CommandArgument="T" Text="T" OnCommand="LetterGuessed" /> |
<asp:LinkButton runat="server" id="U" CommandArgument="U" Text="U" OnCommand="LetterGuessed" /> |
<asp:LinkButton runat="server" id="V" CommandArgument="V" Text="V" OnCommand="LetterGuessed" /> |
<asp:LinkButton runat="server" id="W" CommandArgument="W" Text="W" OnCommand="LetterGuessed" /> |
<asp:LinkButton runat="server" id="X" CommandArgument="X" Text="X" OnCommand="LetterGuessed" /> |
<asp:LinkButton runat="server" id="Y" CommandArgument="Y" Text="Y" OnCommand="LetterGuessed" /> |
<asp:LinkButton runat="server" id="Z" CommandArgument="Z" Text="Z" OnCommand="LetterGuessed" />
<p>
<center>
<asp:label id="lblEndGameMessage" runat="server"
Font-Size="18pt" Font-Weight="Bold" />
</center>
</form>


And this is the page: http://asptry20160424112507.azurewebsites.net/

Can anybody help me please?

Answer

You can retrieve the LinkButton control and its CommandArgument this way:

Protected Sub LetterGuessed(object sender, CommandEventArgs e)
    Dim clickedButton As LinkButton = CType(sender, LinkButton)
    Dim cmdArgument As String = e.CommandArgument
    ...
End Sub