1

I am trying to pass a querystring value via the commandargument of a linkbutton. It doesn't seem to be getting the value and just passing the actual text in the commandargument.

page:

            <asp:LinkButton ID="LinkButton1"
        runat="server" 
        CausesValidation="False" 
        CommandArgument='<%=Request.QueryString("uid")%>' 
        CommandName="uid" 
        OnCommand="LinkButton1_Command" >Close Case</asp:LinkButton>
        <asp:Button ID="Button2" runat="server" Text="Button" CausesValidation="False" />

code behind:

Protected Sub LinkButton1_Command(ByVal sender As Object, ByVal e As CommandEventArgs)

Label1.Text = "You chose: " & e.CommandName & " Item " & e.CommandArgument

End Sub

It then actually returns:

You chose: uid Item <%=Request.QueryString("uid")%>

when it should return something like:

You chose: uid Item 12345

Thanks J.

Jammer
  • 2,330
  • 11
  • 48
  • 77

3 Answers3

2
CommandArgument='<%= Request.QueryString["Some value here"] %>' 

or in codebehind:

LnkBtnSubmit.COmmandArgument = Request.QueryString["Some value here"] ;

if the first one doesn't work try changin the '=' to '#' to bind it and call a DataBind() on the linkbutton or its container

   CommandArgument='<%# Request.QueryString["Some value here"] %>' 

but since your going to need to call DataBind() in the code behind you may just want to set the value there anyway, this will make it more clear whats happening


Here is a question that is nearly identical that has some good answers:
ASP.Net LinkButton CommandArgument property ignores <%= .. %>
or this one
commandargument string not evaluating

Community
  • 1
  • 1
Letseatlunch
  • 2,432
  • 7
  • 28
  • 33
0

Try to get the value on the CB directly. Using Request.Querystring("key").

Protected Sub LinkButton1_Command(ByVal sender As Object, ByVal e As CommandEventArgs)
    Label1.Text = "You chose: " & Request.QueryString("uid")
End Sub
JAiro
  • 5,914
  • 2
  • 22
  • 21
0

Remember that asp.net shows a page based on two steps:

  1. It first builds up an object on the server (an instance of a Page class) using the steps in the asp.net page lifecycle.
  2. After this object is built, it uses it to generate html to send to the client via the response stream.

The markup in your *.aspx file is used to create the page object on the server, but the <%= syntax writes it's output directly to the response stream, bypassing that markup; it's equivalent to calling Response.Write() in your code behind. You can't assign it's result to a property here, just write it to the browser. It should be obvious to you now why this doesn't work as expected. It's not evaluating your <%= bee sting there because it's not valid.

Instead, you can use the databinding syntax (<%#, and with double quotes rather than single quotes) or write the value to your page object from the code behind.

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794