[英]Trying to find a button inside the InsertItemTemplate from ItemDataBound in code behind
I'm currently trying to find the insert button inside the InsertItemTemplate from the code behind. 我目前正在尝试从后面的代码中找到InsertItemTemplate内部的插入按钮。 Essentially I'm trying to make this button's visibility false if the count reaches 10 or more.
本质上,如果计数达到10或更多,我试图使此按钮的可见性为false。 I keep getting the null reference exception error so I assume it's not finding the button.
我不断收到null引用异常错误,因此我假设找不到按钮。 I've pasted code from the code behind file below:
我从下面文件的代码后面粘贴了代码:
protected void externalLinksList_ItemDataBound(object sender, ListViewItemEventArgs e)
{
String connectionString = WebConfigurationManager.ConnectionStrings["UniString"].ConnectionString;
SqlConnection myConnection = new SqlConnection(connectionString);
myConnection.Open();
String linkCountQuery = "SELECT COUNT(id) FROM links";
SqlCommand linkCountQueryCommand = new SqlCommand(linkCountQuery, myConnection);
Int32 linkCountQueryCommandValue = (Int32)linkCountQueryCommand.ExecuteScalar();
if (linkCountQueryCommandValue >= 10)
{
Button InsertButton = (Button)e.Item.FindControl("InsertButton") as Button;
InsertButton.Visible = false;
Label linkLimit = (Label)e.Item.FindControl("linkLimit") as Label;
linkLimit.Visible = true;
linkLimit.Text = "Up to 10 external links are permitted. Please delete links before adding any more.";
}
else
{
Button InsertButton = (Button)e.Item.FindControl("InsertButton");
}
}
You're already executing a query once to generate the list of items to display and you're binding the ListView
to that data source. 您已经执行了一次查询以生成要显示的项目列表,并且将
ListView
绑定到该数据源。 At that point, you can inspect whatever that data source is to determine how many items it contains. 此时,您可以检查该数据源是什么,以确定它包含多少个项目。 I wouldn't repeat that query - going back to the database - for every single item displayed to get the same number again.
对于显示的每个项目,我都不会重复该查询(返回数据库)以再次获得相同的编号。
There are a few ways to do it. 有几种方法可以做到这一点。 The easiest might be
最简单的可能是
ListView
, get the count of items in that collection. ListView
,获取该集合中的项目数。 (Do this wherever you're specifying the DataSource
for the ListView
.) ListView
指定DataSource
任何地方都可以执行此操作。) externalLinksList_ItemDataBound
check that variable - the original count of items in the list - and use that to determine whether to show or hide the button. externalLinksList_ItemDataBound
检查该变量-列表中项目的原始计数-并使用该变量确定是显示还是隐藏按钮。 You could also just inspect the DataSource
directly from externalLinksList_ItemDataBound
. 您也可以直接从
externalLinksList_ItemDataBound
检查DataSource
。 For example, if the data source is any sort of list you could do 例如,如果数据源是任何种类的列表,则可以执行
var listView = (ListView)sender;
var dataSource = (IList)listView.DataSource;
if(dataSource.Count >= 10)
{
//etc.
}
There might be a more elegant solution that involves setting a property on the ListView
itself to disable that button, but this would do it. 可能有一个更优雅的解决方案,其中涉及在
ListView
本身上设置属性以禁用该按钮,但是可以做到这一点。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.