【问题标题】:Fetching values from the DropDownlist on PostBack inside Repeater Control in ASP.Net从 ASP.Net 中中继器控件内 PostBack 上的 DropDownlist 获取值
【发布时间】:2013-08-16 08:11:40
【问题描述】:

我在我的一个 asp.net 页面上有一个 Repeater Control,我在转发器控件中有一些标签和一个 dropdownlist。默认内容填充在Item_Bound 事件的标签和下拉列表中。现在我想实现以下目标:

  • 当用户从下拉列表中选择另一个项目时,标签应相应更新。

我的问题是,由于我的默认内容来自item_bound,它总是覆盖下拉列表中的内容,但如果我将!IsPostBack 条件放在 Item_Bound 事件中,那么在选择下拉列表时不会发生任何事情.

我使用了 OnSelectedIndexChange 事件,只是在事件中提供了 Response.Write,但是由于 DropDownlist 值会覆盖自身,所以我在响应中没有得到任何信息。

谁能帮我解释一下我应该如何解决这个问题。


更新问题:

好的,现在我可以从下拉列表中使用 selectedItems 获取转发器标签中的结果,但现在我的问题是我在转发器中绑定了多个结果,即每行的每个下拉列表,但是当我从另一行中选择项目时,它仍然假定第一行的值。这是我的参考代码:

protected void drpQuantity_SelectedIndexChanged(object sender, EventArgs e) //DropDown inside repeater control.
{
    foreach (RepeaterItem item in rptLatestProducts.Items)
    {
        if (item.ItemType == ListItemType.Item)
        {
            HiddenField hd = item.FindControl("hdProductId") as HiddenField;
            DropDownList drp = item.FindControl("drpQuantity") as DropDownList;
            Label mrp = item.FindControl("lblMRP") as Label;
            Label ourPrice = item.FindControl("lblOurPrice") as Label;
            Label discount = item.FindControl("lblDiscount") as Label;
            ScriptManager.RegisterStartupScript(updPriceByUnits, this.GetType(), "alert", "alert('" + hd.Value + "')", true); //Always returns product id of the first row.
            objPackage.ProductId = Convert.ToInt32(hd.Value);
            objPackage.TownId = objPackage.DefaultTown;
            int discountPercent = Convert.ToInt32(objPackage.GetProductPackages().Select("unit=" + drp.SelectedValue + " and productid=" + hd.Value)[0]["Discount"].ToString());
            mrp.Text = "<span class='rupee' style='font-size:14px;'>Rs</span>" + objPackage.GetProductPackages().Select("unit=" + drp.SelectedValue + " and productid=" + hd.Value)[0]["MRP"].ToString();
            ourPrice.Text = "<span class='rupee' style='font-size:14px;'>Rs</span>" + objPackage.GetProductPackages().Select("unit=" + drp.SelectedValue + " and productid=" + hd.Value)[0]["Price"].ToString();
            mrp.Visible = (mrp.Text != ourPrice.Text);
            if (discountPercent > 0)
            {
                discount.Visible = true;
                discount.Text = objPackage.GetProductPackages().Select("unit=" + drp.SelectedValue + " and productid=" + hd.Value)[0]["Discount"].ToString() + "% OFF";
            }
            else
            {
                discount.Visible = false;
            }
        }
    }
}

谁能帮我解决这个问题

【问题讨论】:

    标签: asp.net repeater html-select


    【解决方案1】:

    听起来您将!Page.IsPostBack 放在了错误的代码位上。

    您需要将其放在您的 DataBind 代码周围。所以;

    if (!Page.IsPostBack){
        this.myRepeater.DataSource = [yourdatasource];
        this.myRepeater.DataBind();
    }
    

    这样您的DropDownList 控件不会反弹。

    【讨论】:

    • @HanletEscaño Abbas - “!IsPostBack 条件在 Item_Bound 事件中”我 - “听起来你把 !Page.IsPostBack 放在了错误的代码位”
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-12-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多