1

Hi folks i have this terrible problems i hope any of you could help me first of all i have a ListView which has (lets say) 3 columns Id, Quantity, And an image button with an action.

enter image description here

the image button launch the function createparameter that splits the string into 2 int variables, but i want to get the first 1 from the textbox QuantityTextBox, What can i do to send the value of the textbox to the function, any ideas?

protected string CreateParameter(object arg1, object arg2) 
        {
            return string.Concat(arg1, "|", arg2);
        }

i wish there's a better way to do this than do a foreach of every list item and then find control because i dont care the other list items,

1 Answer 1

1

What you want to do will not work the way you want because the value of the textbox could be changed by the user. So it's not reasonable to try and bind it to the CommandArgument at runtime because there is no value there yet.

But when you handle the ItemCommand event for the ListView, you have access to which ListViewItem the button was clicked in, so you can use FindControl to get the textbox and look at its value.

For that to work, you will need to add a CommandName value to your ImageButton controls. And you would just pass in the product id in the command argument directly.

So, your button would look like this:

<asp:ImageButton ID="btnAdd" runat="server" Tooltip="Aggregar producto" CommandName="AddProduct"
  CommandArgument="<%# Eval("IdProduct") %>" />

And your codebehind would look something like this (not tested):

  protected void lv_Productos_OnItemCommand(object sender, ListViewCommandEventArgs e)
  {
    if (String.Equals(e.CommandName, "AddProduct"))
    {
      // Get a reference to your textbox in this item
      TextBox textbox = e.Item.FindControl("QuantityTextBox") as TextBox;

      if (textbox != null)
      {
          int quant = 0;
          if (int.TryParse(textbox.Value, quant))
          {
             int prodId = (int)e.CommandArgument;

             //do what you want with the quantity and product id
          }
      }

    }
  }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.