如何在asp.net c#中从数据列表中隐藏特定行?

如何在asp.net c#中从数据列表中隐藏特定行?,asp.net,Asp.net,在我的项目中,我有一个请求页面,其中包含一个数据列表,其中包含四个模板:图像(profilepic)、标签(firstname)、按钮1(accept)、按钮2(deny),以及一个隐藏字段,其中包含请求者的地址 并编写了代码 受保护的无效DataList1_ItemCommand(对象源,DataListCommandEventArgs e) { 单击“接受”按钮,请求表中的frnshpstatus更新为“Y”,但它仍在数据列表中显示已接受或拒绝的请求。 我希望它们只从datalist中删除,

在我的项目中,我有一个请求页面,其中包含一个数据列表,其中包含四个模板:图像(profilepic)、标签(firstname)、按钮1(accept)、按钮2(deny),以及一个隐藏字段,其中包含请求者的地址 并编写了代码 受保护的无效DataList1_ItemCommand(对象源,DataListCommandEventArgs e) {

单击“接受”按钮,请求表中的frnshpstatus更新为“Y”,但它仍在数据列表中显示已接受或拒绝的请求。 我希望它们只从datalist中删除,但在数据库中保留请求的记录。
在asp.net中使用c#进行回答。

您需要在“DataList1_ItemCommand”中再次绑定DataList,并在“DataList1_ItemDataBound”事件中编写以下代码。我假设您在绑定到DataList的数据源中具有“frnshpstatus”

void  DataList1_ItemDataBound(Object sender, DataListItemEventArgs e)
  {

     if (e.Item.ItemType == ListItemType.Item || 
         e.Item.ItemType == ListItemType.AlternatingItem)
     {
           DataRowView drv = (DataRowView)(e.Item.DataItem);        
           string frnshpstatus= drv.Row["frnshpstatus"].ToString(); 
           if(frnshpstatus="Y")
           {
             // Retrieve the Button control in the current DataListItem.
             Button Button1  = (Label)e.Item.FindControl("Button1");
             Button Button2  = (Label)e.Item.FindControl("Button2");
             Button1.Visible=False;
             Button2.Visible=False;

             //You Can Also Hide Row but for that you need to set it an ID and set it attribute "runat=server" 

             HtmlTableRow row=(HtmlTableRow)e.Item.FindControl("trId");
             row.Visible=False;   
           }                        

     }

  }

我认为您希望在单击“接受”或“拒绝”后将行从数据列表中完全删除,对吗?如果是这种情况,只需在查询中添加一个where子句来填充数据列表

where frnshpstatus is null
如果不为null,则默认值为。然后在ItemCommand事件处理程序结束时完成接受/拒绝过程后,需要再次手动对数据列表进行数据绑定

DataList1.DataBind();

它正在给出异常“异常详细信息:System.ArgumentException:列'frnshpstatus'不属于表DefaultView。源错误:第83行:{84行:DataRowView drv=(DataRowView)(e.Item.DataItem);第85行:字符串frnshpstatus=drv.Row[“frnshpstatus”].ToString();第86行:如果(frnshpstatus==“Y”)第87行:{“我已经提到“frnshpstatus”需要存在于数据源中,您需要在绑定的数据源中添加“frnshpstatus”列。。。
DataList1.DataBind();