C# 在Listview中隐藏按钮
我已经在我的网站上创建了一个搜索页面,在列表视图中显示搜索结果。每个结果都有一个按钮,如果搜索结果中的用户是朋友,我想隐藏该按钮 因此,如果任何用户id的status值为1,则该用户就是朋友隐藏按钮 检查状态是否为1的查询有效,但按钮仍然可见。为了获取用户的用户id,我使用了addFriend.CommandArgument。我的朋友是博顿 问题之一是搜索页面无法加载,因为找不到addfriend的变量C# 在Listview中隐藏按钮,c#,asp.net,asp.net-mvc,visual-studio-2010,C#,Asp.net,Asp.net Mvc,Visual Studio 2010,我已经在我的网站上创建了一个搜索页面,在列表视图中显示搜索结果。每个结果都有一个按钮,如果搜索结果中的用户是朋友,我想隐藏该按钮 因此,如果任何用户id的status值为1,则该用户就是朋友隐藏按钮 检查状态是否为1的查询有效,但按钮仍然可见。为了获取用户的用户id,我使用了addFriend.CommandArgument。我的朋友是博顿 问题之一是搜索页面无法加载,因为找不到addfriend的变量 protected void ListView1_ItemDataBound(object
protected void ListView1_ItemDataBound(object sender, ListViewItemEventArgs e)
{
if (e.Item.ItemType == ListViewItemType.DataItem)
{
if (addFriend == null)
{
// IF I REMOVE THIS THE PAGE WILL NOT LOAD BECAUSE PAGE DISPLAYS THE DATA AFTER A BUTTON CLICK ON THE PAGE
}
else
{
var addFriend = sender as Button;
// Get the UserId
Guid currentUserId = (Guid)currentUser.ProviderUserKey;
string connectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
string insertSql = "SELECT Status from User_friend WHERE ((ProfileId1 = @FriendProfileId) AND (ProfileId = (SELECT ProfileId FROM User_Profile WHERE UserId = @UserId))) OR ((ProfileId = @FriendProfileId) AND (ProfileId1 = (SELECT ProfileId FROM User_Profile WHERE UserId = @UserId)))";
using (SqlConnection myConnection = new SqlConnection(connectionString))
{
myConnection.Open();
SqlCommand myCommand = new SqlCommand(insertSql, myConnection);
myCommand.Parameters.AddWithValue("@FriendProfileId", addFriend.CommandArgument);
myCommand.Parameters.AddWithValue("@UserId", currentUserId);
object result = myCommand.ExecuteScalar();
if (Convert.ToString(result) == "1")
{
Button hdn = (Button)e.Item.FindControl("addFriend");
hdn.Visible = false;
}
}
}
}
}
我如何使它工作
谢谢尝试使用JQuery,而不是让按钮不可见。作为c#属性.Visible,使按钮对DOM完全不可见,而henseforth对页面完全不可见 通过在标记底部声明以下内容:
<script type="text/javascript">
$(document).ready(function (){
$('#<%=addFriend.ClientID %>').hide();
});
</script>
$(文档).ready(函数(){
$('#')。隐藏();
});
页面仍将加载,因为变量已找到,但已隐藏。如果您给出:
,然后通过DOM控制按钮可见性,也可以这样说。Visual Studio有一个优秀的调试工具,这是它无法提供的。不确定如何获取用于绑定到listview的数据,但是,您可能想考虑将状态返回到该调用,而不是为ListVIEW中的每个项目建立单独的数据库调用。如果这样做,则可以将按钮的visible属性绑定到status的值。像Visible=''这样的东西。希望这能让你走上正轨。非常感谢马特,你的想法好多了。我会尝试一下并给你反馈谢谢,我已经考虑过j query,但我不希望按钮永久不可见。我希望它在某些情况下是可见的