1

So what I want to do is to extract the text from a label when a button is clicked. What I've got so far is the following:

    <asp:Label ID="lbl_carID" runat="server" Text="Label"></asp:Label>
    <asp:Label ID="lbl_ownerID" runat="server" Text="Label"></asp:Label>

    <asp:Button ID="btn_wishList" runat="server" Text="Add to wish list"  CssClass="btn" CommandArgument='<%= lbl_carID.Text.ToString() %>' OnCommand="btn_wishList_Command" />
    <asp:Button ID="btn_offer" runat="server" Text="Make Offer" CssClass="btn" CommandArgument='<%= lbl_ownerID.Text.ToString() %>' OnCommand="btn_offer_Command" />

I have also tried without the ToString() method, but it is not working. Also tried without the = after %. I am new to code insertion, so I guess it is something really small, but I cannot make it work. Any suggestions ?

EDIT: This is the code behind btn_offer_command. Both commands are identical at start, so I am posting the shorter code.

protected void btn_offer_Command(object sender, CommandEventArgs e)
    {
        string id = (string)e.CommandArgument;
        Session["ownerToBeOffered"] = id;
        Response.Redirect("LoggedInFeatures/MakeOffer.aspx");
    }
Phantomazi
  • 408
  • 6
  • 22

2 Answers2

1

I believe you cannot use <%= %> inside properties with a runat="server".

Try to pass the CommandName and check it in your handler.

Example

<asp:Label ID="lbl_carID" runat="server" Text="Labeddl1"></asp:Label>
<asp:Label ID="lbl_ownerID" runat="server" Text="Label1"></asp:Label>

<asp:Button CommandName="carID" CssClass="btn" ID="btn_wishList" OnCommand="btn_wishList_Command" runat="server" Text="Add to wish list" />
<asp:Button  CommandName="ownerID" CssClass="btn" ID="btn_offer" runat="server" Text="Make Offer" OnCommand="btn_offer_Command" />

And in your Code try something like the following...

    protected void btn_wishList_Command(object sender, CommandEventArgs e)
{
    if (e.CommandName == "carID")
    {
        //Do Something here
    }
}

protected void btn_offer_Command(object sender, CommandEventArgs e)
{
    if (e.CommandName == "ownerID")
    {
        //Do Something here
    }
}
Nathan
  • 1,303
  • 12
  • 26
0

place it somewhere in your code

[ExpressionPrefix("Code")]
public class CodeExpressionBuilder : ExpressionBuilder
{
    public override CodeExpression GetCodeExpression(BoundPropertyEntry entry,
       object parsedData, ExpressionBuilderContext context)
    {
        return new CodeSnippetExpression(entry.Expression);
    }
}

register in web.config under compilation section

<compilation debug="true" targetFramework="4.5.1" >
  <expressionBuilders>
    <add expressionPrefix="Code" type="WebApplication2.CodeExpressionBuilder"/>
  </expressionBuilders>
</compilation>

now you will be able to evaluate any code at that point as would expected by following construction

 <asp:Button ID="btn_offer" runat="server" Text="Make Offer" CssClass="btn" CommandArgument='<%$ Code: lbl_ownerID.Text %>' OnCommand="btn_offer_Command" />
Victor
  • 618
  • 4
  • 12
  • Any other ideas that I may try ? – Phantomazi May 06 '15 at 20:31
  • this property is Bindable so you can bind commandName in some iteration like structures by item name, if you are not using e.g. Repeater to print them dynamicaly, you just can set commandName at code behind in Page_Load event – Victor May 06 '15 at 20:38
  • also you can do something interesting with codeExpressionBuilders http://weblogs.asp.net/infinitiesloop/The-CodeExpressionBuilder – Victor May 06 '15 at 20:41
  • @Phantomazi check the updated answer, that might be exactly what you want – Victor May 06 '15 at 20:49