• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1409
  • Last Modified:

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_Command" />
                </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

0
pumbaaca
Asked:
pumbaaca
  • 2
  • 2
  • 2
  • +1
1 Solution
 
sachitjainCommented:
Actually u can not pass the argument value like  CommandArgument="<%= Session(strKey).ItemID %>"
This approach is permissible only with simple HTML controls not with server controls

Instead try CommandArgument="<%# Session(strKey).ItemID %>"

But if u r using this approach then also u need to write BttnRemove.DataBind() on page load to dynamically bind value of session variable with button control.

Hope it works.

Regards
Sachit.
0
 
appariCommented:

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 = strValue
        %>
0
 
the_paabCommented:
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_Command" />


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
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
pumbaacaAuthor Commented:
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?
0
 
appariCommented:

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.CommandArgument = "NONZERO"
        Else
            LinkButton1.CommandArgument = "ZERO"
        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.CommandEventArgs) Handles LinkButton1.Command
        Response.Write(e.CommandArgument)
    End Sub
0
 
sachitjainCommented:
You could directly try  CommandArgument=<%#Session("Item1").ItemID%> as Item1 is value of variable strKey.

Regards
Sachit.
0
 
pumbaacaAuthor Commented:
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.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 2
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now