0

在我的项目中,我有一个请求页面,其中包含一个数据列表,其中包含四个模板 Image(profilepic)、Label(firstname) 和 Button1(accept)、button2(deny) 和一个包含请求者地址的隐藏字段并编写了代码 protected void DataList1_ItemCommand(对象源,DataListCommandEventArgs e){

    if (e.CommandName == "Accept")
    {
        SqlConnection con = new System.Data.SqlClient.SqlConnection();
        con.ConnectionString = ConfigurationManager.ConnectionStrings["Con"].ConnectionString;
        con.Open();
        if (con.State == ConnectionState.Open)
        {
            HiddenField hd = (HiddenField)e.Item.FindControl("HiddenField1");
            string str = hd.Value;
            SqlCommand cmd = new System.Data.SqlClient.SqlCommand();
            cmd.CommandText = "Update requests set frnshpstatus='Y' where Email='" + Session["UserName"] + "' And frnemail='" + str + "'";
            cmd.Connection = con;
            cmd.CommandType = CommandType.Text;
            cmd.ExecuteNonQuery();

        }
    }
    else if (e.CommandName == "Deny")
    {
        SqlConnection con = new System.Data.SqlClient.SqlConnection();
        con.ConnectionString = ConfigurationManager.ConnectionStrings["Con"].ConnectionString;
        con.Open();
        if (con.State == ConnectionState.Open)
        {
            HiddenField hd = (HiddenField)e.Item.FindControl("HiddenField1");
            string str = hd.Value;
            SqlCommand cmd = new System.Data.SqlClient.SqlCommand();
            cmd.CommandText = "Update requests set frnshpstatus='N' where Email='" + Session["UserName"] + "' And frnemail='" + str + "'";
            cmd.Connection = con;
            cmd.CommandType = CommandType.Text;
            cmd.ExecuteNonQuery();
        }
    }

}

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

4

2 回答 2

0

您需要在“DataList1_ItemCommand”中再次绑定 DataList 并在“DataList1_ItemDataBound”事件中编写以下代码。我假设您在绑定到 DataList 的 DataSource 中有“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;   
           }                        

     }

  }
于 2012-08-25T06:18:01.823 回答
0

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

where frnshpstatus is null

或任何默认值(如果不为空)。然后,在 ItemCommand 事件处理程序结束时完成接受/拒绝过程后,您需要再次手动对数据列表进行数据绑定

DataList1.DataBind();
于 2012-08-25T06:51:44.977 回答