简体   繁体   English

通过链接按钮的CommandArgument在转发器中的下拉列表中获取选定的值

[英]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. 我有一个中继器,其中有一个下拉列表和一个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. 我想通过链接按钮中的CommandArgument获取下拉列表的选定值,但它只知道下拉列表的默认值。

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. 但是,如果您认真使用它,则必须在转发器控件的ItemTemplate中使用FindControll方法。 You should find dropdownlist first, and then cast it to a object to be able to use it's value. 您应该先找到dropdownlist,然后将其强制转换为对象才能使用其值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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