简体   繁体   中英

get selected value by drop down list in a repeater by CommandArgument of link button

I have a repeater that in it has one dropdown list and one linkbutton.
I want to get the selected value of the dropdown list by CommandArgument in linkbutton, but it just knows default value of dropdown list.

My code :

<asp:Repeater runat="server" ID="Repeater1" OnItemDataBound="Page_Load2"OnItemCommand="list_ItemCommand" >
    <ItemTemplate>
        <asp:DropDownList ID="dlPricelist" CssClass="width100darsad dropdownlist" runat="server"  AutoPostBack="true" ViewStateMode="Enabled"  >
        </asp:DropDownList>
        <asp:LinkButton ID="btnAddToCart" runat="server" class="btn btn-success btnAddtoCardSinglepage"  CommandArgument='<%#Eval("id") %>' CommandName="addtocard">اضافه به سبد خرید</asp:LinkButton>
        <asp:Label ID="xxxxxx" runat="server" Text="Label"></asp:Label> 
    </ItemTemplate>
</asp:Repeater>

Code behind:

protected void Page_Load2(object sender, RepeaterItemEventArgs e)
{
    if (!IsPostBack)
    {
        string id = Request.QueryString["id"].ToString();

        DataSet dsselectcategory = BLLTour.left3join(id.Trim().ToString());
        var dlPricelist = (DropDownList)e.Item.FindControl("dlPricelist");
        dlPricelist.DataSource = dsselectcategory.Tables[0];
        dlPricelist.DataTextField = "pricelistPrice";
        dlPricelist.DataValueField = "priceid";
        dlPricelist.DataBind();
    }
}

protected void list_ItemCommand(object source, RepeaterCommandEventArgs e)
{
    if (e.CommandName == "addtocard")    
    {
        foreach (RepeaterItem dataItem in Repeater1.Items)
        {
            Label xxxxxx = (Label)e.Item.FindControl("xxxxxx");
            LinkButton btnAddToCart = (LinkButton)e.Item.FindControl("btnAddToCart");
            xxxxxx.Text = ((DropDownList)dataItem.FindControl("dlPricelist")).SelectedItem.Text; //No error
        }
    }
}

I don't know how I should fix it.

You are using very old technology to show data that's not appropriate. But if you are serious to use it, you must use FindControll method in ItemTemplate of your repeater control. You should find dropdownlist first, and then cast it to a object to be able to use it's value.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM