locked
ListView Button Click With Control SelectedValue RRS feed

  • Question

  • User-1193791088 posted

    I looked for this issue, but I wasn't quite sure how to word the search so here goes

    I have a Listview that displays items in a database

    I added a button to each line that when clicked adds that item number to a Specific Job Number,

    I can handle the click event by adding the ItemId to the command argument on the button

    Now to complicate matters, I added a dropdownlist with qty, but being a separate control, i don't know what I would add to submit the qty as well as the Item to the List

    <ItemTemplate>
    								<tr style="">
    									<td>
    										<asp:Label Text='<%# Eval("ItemId") %>' runat="server" ID="ItemIdLabel" /></td>
    									<td>
    										<asp:Label Text='<%# Eval("Description") %>' runat="server" ID="DescriptionLabel" /></td>
    									<td>
    										<asp:DropDownList ID="DropDownList1" runat="server">
    											<asp:ListItem Value="1" Text="1" />
    											<asp:ListItem Value="2" Text="2" />
    											<asp:ListItem Value="3" Text="3" />
    											<asp:ListItem Value="4" Text="4" />
    											<asp:ListItem Value="5" Text="5" />
    											<asp:ListItem Value="6" Text="6" />
    											<asp:ListItem Value="7" Text="7" />
    										</asp:DropDownList>
    									</td>
    									<td>
    										<asp:Button ID="Button1" runat="server" Text="&#xf0fe; Add to Job" CommandArgument='<%# Eval("ItemId") %>' CssClass="btn btn-primary fa" OnClick="Button1_Click" />
    									</td>
    								</tr>
    							</ItemTemplate>

    I need to add the qty from DropDownList 1 to Button1_Click

    C#

    protected void Button1_Click(object sender, EventArgs e)
    	{
    		Button ClickedButton = (Button)sender;
    		ClickedButton.CommandArgument = _ItemId;
    	}

    Tuesday, November 8, 2016 4:38 AM

Answers

  • User1428471904 posted

    You can do this by finding the dropdown in its parent. Means, First get the parent of the button ClickedButton.Parent and cast it it. Then try to find the dropdown with its id DropDownList1 and get the quatity.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, November 8, 2016 4:56 AM
  • User-1716253493 posted

    Here how to get other values when you click the button

        protected void Button1_Click(object sender, EventArgs e)
        {
            Button ClickedButton = (Button)sender;
            ListViewItem item = ClickedButton.NamingContainer as ListViewItem;
            Label DescriptionLabel = (Label)item.FindControl("DescriptionLabel");
            DropDownList DropDownList1 = (DropDownList)item.FindControl("DropDownList1");
            int itemid = Convert.ToInt32(ClickedButton.CommandArgument);
            string desc = DescriptionLabel.Text;
            int qty = Convert.ToInt32(DropDownList1.SelectedValue);
            //next code
        }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, November 8, 2016 7:02 AM

All replies

  • User1428471904 posted

    You can do this by finding the dropdown in its parent. Means, First get the parent of the button ClickedButton.Parent and cast it it. Then try to find the dropdown with its id DropDownList1 and get the quatity.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, November 8, 2016 4:56 AM
  • User-1716253493 posted

    Here how to get other values when you click the button

        protected void Button1_Click(object sender, EventArgs e)
        {
            Button ClickedButton = (Button)sender;
            ListViewItem item = ClickedButton.NamingContainer as ListViewItem;
            Label DescriptionLabel = (Label)item.FindControl("DescriptionLabel");
            DropDownList DropDownList1 = (DropDownList)item.FindControl("DropDownList1");
            int itemid = Convert.ToInt32(ClickedButton.CommandArgument);
            string desc = DescriptionLabel.Text;
            int qty = Convert.ToInt32(DropDownList1.SelectedValue);
            //next code
        }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, November 8, 2016 7:02 AM
  • User-1193791088 posted

    Awesome - Thanks

    Tuesday, November 8, 2016 1:01 PM