pumbaaca
asked on
Passing a value through CommandArgument
I am trying to pass a value as part of my link button but cannot get the "true" value to pass to the event subroutine. A portion of my ASP.Net code is, as follows:
<%
Dim strKey as string
Dim strValue as string
strKey = "Item1"
strValue = Session(strKey).ItemID
%>
<td>
<asp:LinkButton ID="BttnRemove"
runat="server"
Text="Remove"
CommandName="Remove"
CommandArgument="<%= Session(strKey).ItemID %>"
OnCommand="bttnRemove_Comm and" />
</td>
When I run it and look at e.CommandArguement, the value is "<%= Session(strKey).ItemID %>" instead of something like "L1234". The CommandName correctly shows "Remove". When I debug on the "strValue = Session(strKey).ItemID" line, I can see the correct value of "L1234".
Why am I not able to see the correct value for "CommandArgument" within my bttnRemove_Command subroutine?
thanks
<%
Dim strKey as string
Dim strValue as string
strKey = "Item1"
strValue = Session(strKey).ItemID
%>
<td>
<asp:LinkButton ID="BttnRemove"
runat="server"
Text="Remove"
CommandName="Remove"
CommandArgument="<%= Session(strKey).ItemID %>"
OnCommand="bttnRemove_Comm
</td>
When I run it and look at e.CommandArguement, the value is "<%= Session(strKey).ItemID %>" instead of something like "L1234". The CommandName correctly shows "Remove". When I debug on the "strValue = Session(strKey).ItemID" line, I can see the correct value of "L1234".
Why am I not able to see the correct value for "CommandArgument" within my bttnRemove_Command subroutine?
thanks
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
You can not use '<%' in server controls, but you can use this:
<asp:LinkButton ID="BttnRemove"
runat="server"
Text="Remove"
CommandName="Remove"
CommandArgument=<%#Session ("Item1"). ItemID%>
OnCommand="bttnRemove_Comm and" />
and you must call LinkButton's DataBind method every time you want to change value of CommandArgument,e.g.:
Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
BttnRemove.DataBind()
End Sub
<asp:LinkButton ID="BttnRemove"
runat="server"
Text="Remove"
CommandName="Remove"
CommandArgument=<%#Session
OnCommand="bttnRemove_Comm
and you must call LinkButton's DataBind method every time you want to change value of CommandArgument,e.g.:
Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
BttnRemove.DataBind()
End Sub
ASKER
Sachitjain - I tried this and included the databind you also mentioned (though not clear what it does), but got the following compile error at the asp:linkbutton line: BC30451: Name 'strKey' is not declared. I confirmed that strKey is indeed defined in the code-behind just above the asp:linkbutton (i.e. no code changes in my example posted except for what you stated).
appari - I tried your suggestion and now CommandArgument returns "" (nothing). I have confirmed via debug that the CommandArgument did correctly set.
Additional thoughts or direction?
appari - I tried your suggestion and now CommandArgument returns "" (nothing). I have confirmed via debug that the CommandArgument did correctly set.
Additional thoughts or direction?
can you post complete code?
i tested here and it works fine.
add a new webform.
add a textbox, a button and a link button controls to the form.
now add the following code, run and enter something in text box and click on button it changes link buttonns command argument to nonzero. to test the change now click on link button it writes nonzero. now delete text from textbox and click the button and click link button to see the change in command argument.
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
If TextBox1.Text.Length <> 0 Then
LinkButton1.CommandArgumen
Else
LinkButton1.CommandArgumen
End If
End Sub
Protected Sub LinkButton1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles LinkButton1.Click
End Sub
Protected Sub LinkButton1_Command(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.
Response.Write(e.CommandAr
End Sub
You could directly try CommandArgument=<%#Session ("Item1"). ItemID%> as Item1 is value of variable strKey.
Regards
Sachit.
Regards
Sachit.
ASKER
After taking all your initial suggestions and still not able to make it work, I created a new project and set out to make a new example that would more appropriately reflect what I was doing (my prior example may have been too simplistic). In doing this new exercise and making use of the <%# %> and also the bind (as initially suggested by sachitjain), I was able to make it work. I will need to research now the difference between my new example and the original code...but at least I have a valid working example. I am going forward with sachitjain's initial suggestion.
Thanks to all for your help.
Thanks to all for your help.
why dont you try assigning the commandargument value in the codebehind itself?
something like this
<%
Dim strKey as string
Dim strValue as string
strKey = "Item1"
strValue = Session(strKey).ItemID
BttnRemove.CommandArgument
%>