简体   繁体   English

编辑ASP中继器中的特定行

[英]Edit a specific row in an asp repeater

I have a repeater and on dataItembound i have something like this 我有一个中继器,并且在dataItembound上我有这样的东西

((HtmlTableRow)e.Item.FindControl("prodName")).Visible = false;

This however sets all tablerows in the repeater to be invisible. 但是,这会将转发器中的所有表行设置为不可见。 I would like a specific one to be hidden. 我想隐藏一个特定的对象。 Is there a way to do this? 有没有办法做到这一点?

Heres the full imp 继承人

 protected void RepeaterCategories_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
  if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
    {
        //Get category id
        string catId = Request.QueryString["stctid"];
        //Call function to check stock levels in the next loaded category
        bool stock = checkCategoryStockLevels(catId);

        if(stock == true)
        {
            ((HtmlTableRow)e.Item.FindControl("catName")).Visible = false;
            ((HtmlTableRow)e.Item.FindControl("catImg")).Visible = false;
        }
}

In the description you are retrieving the category identifier from the query string of the pages URL. 在描述中,您将从页面URL的查询字符串中检索类别标识符。 This request will return the same value for all Repeater items and as such every "catName" and "catImg" HtmlTableRow will be hidden. 该请求将为所有Repeater项目返回相同的值,因此,每个“ catName”和“ catImg” HtmlTableRow都将被隐藏。

I assume you wish to hide the rows based on some value stored in the DataSource being bound to the repeater. 我假设您希望基于绑定到转发器的DataSource中存储的某些值隐藏行。

To do so you will need to access the DataItem in the ItemDataBound event and perform a check to determine which item requires the row to be hidden. 为此,您将需要访问ItemDataBound事件中的DataItem并执行检查以确定哪个项目需要隐藏该行。

Below I have bound a List of strings to the Repeater, so I can access each item and perform a check like so, hiding only the HtmlTableRow in the ItemTemplate where the value of the DataItem equals "Item 1": 在下面,我将一个字符串列表绑定到了Repeater,因此我可以访问每个项目并执行类似的检查,仅将HtmlTableRow隐藏在ItemTemplate中,其中DataItem的值等于“ Item 1”:

string dataItem = (string)e.Item.DataItem;
if (dataItem == "Item 1")
{
    ((HtmlTableRow)e.Item.FindControl("prodName")).Visible = false;   
}

I assume you are binding something more complex such as a DataRow or some other Object. 我假设您正在绑定更复杂的东西,例如DataRow或其他对象。 Either way the process is the same, cast the DataItem and perform your check. 无论哪种方法,过程都是相同的,强制转换DataItem并执行检查。

我会考虑使用ListView或DataList代替,因为这样您就可以使用数据键基于某个值确定行的可见性。

I got it. 我知道了。

    for (int repeaterCount = 0; count < repeaterID.Items.Count; count++)
    {
        Label label = (Label)repeaterID.Items[repeaterCount].FindControl("labelID");
        label.Text = "Text";
    }

Thanks to all that helped 感谢所有的帮助

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

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