简体   繁体   English

试图在后面的代码中从ItemDataBound查找InsertItemTemplate内部的按钮

[英]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 最简单的可能是

  • When you do your initial query to populate the ListView , get the count of items in that collection. 在执行初始查询以填充ListView ,获取该集合中的项目数。 (Do this wherever you're specifying the DataSource for the ListView .) (在为ListView指定DataSource任何地方都可以执行此操作。)
  • Store it in a variable 将其存储在变量中
  • In 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.

相关问题 如何在 Java Script 中从 InsertItemTemplate 中找到控件 - How to find control from InsertItemTemplate in Java Script 无法在后面的代码中访问列表框中的按钮 - Not able to access a button inside a listbox in the code behind 更改 ItemDatabound 上数据列表内的链接按钮前景色 - Change link button fore color inside datalist on ItemDatabound 单击一个按钮,然后在一个按钮内部单击以引发另一个按钮,并单击后面的代码? - click on a button and inside a button to raise another button to be clicked with code behind? 访问和编辑按钮上的图像按钮单击后面的代码 - Access and edit Image inside Button on Button click in code behind 当从后面的代码中单击按钮时,检查Dataitemtemplate devexpress gridview内部的复选框状态 - checking checkbox state which is inside Dataitemtemplate devexpress gridview when button is clicked from code behind 代码后面的按钮边框粗细 - Button border thickness from code behind 从后面的代码更改UWP按钮的可见性 - Change visibility of a UWP button from code behind 从后面的代码创建单选按钮,但带有onclick? - create radio button from code behind but with an onclick? 在后面的代码中找到Listview中的空数据? - Find Empty Data in Listview from code behind?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM