2

现在我查看了 ItemTemplates 上的 MSDN,但我没有看到如何通过 ID 访问它们。

这是一个链接http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.templatefield.itemtemplate.aspx

我认为这与访问代码隐藏或服务器脚本中的任何其他控件一样简单,但它不起作用。当我尝试通过 ID 引用它时,我不断收到“在当前上下文中不存在”错误。

我想要做的是访问标题复选框的选中属性并使用它来选择或取消选择 ItemTemplate 中的所有复选框。我还需要以后是否选择它们以用于我的代码中的其他用途。

这是我在项目中使用的 gridview 的代码。

<asp:GridView ID="ApplicationsGridView" runat="server"
   AutoGenerateColumns="True"
   visible="true"
   Font-Size="Smaller"
   CellPadding="5"
   Width="1200px"
   BorderStyle="Solid"
   BorderColor="Black"
   OnDataBinding="ApplicationsGridView_DataBinding">
<%-- Add the checkboxes declaratively  --%>
<Columns>
  <asp:TemplateField>
    <HeaderTemplate>
      <asp:CheckBox runat="server" ID="checkall" Checked="true" OnCheckedChanged="checkall_CheckedChanged" />
      <script runat="server">
        protected void checkall_CheckedChanged(object sender, EventArgs e)
        {
          if(checkall.checked)
          {
            foreach (GridViewRow row in ApplicationsGridView.Rows { }
          }         
        }
      </script>
    </HeaderTemplate>
    <ItemTemplate>
      <asp:CheckBox runat="server" ID="checkboxes" Checked="true" />
    </ItemTemplate>
  </asp:TemplateField>
</Columns>
<AlternatingRowStyle BackColor="#d2d2f2" />
<HeaderStyle Font-Bold="true" BackColor="#052a9f"  ForeColor="#eeeeff"  Font-Size="Medium"/>
</asp:GridView>

最初,我曾尝试在代码隐藏中访问 ID。但即使尝试使用服务器脚本,它仍然找不到它。如果不是通过 ID,我如何访问复选框?

编辑:这有效=)

    protected void checkall_CheckedChanged(object sender, EventArgs e)
    {
        //get whether its checked or not.
        CheckBox theCheckBox = sender as CheckBox;      

        //check them all if checked. Uncheck them all when unchecked.
        if (theCheckBox.Checked)
        {
            foreach (GridViewRow row in ApplicationsGridView.Rows)
            {

                CheckBox cb = row.FindControl("checkboxes") as CheckBox;
                cb.Checked = true;
            }
        }

        else if (!(theCheckBox.Checked))
        {
            foreach (GridViewRow row in ApplicationsGridView.Rows)
            {

                CheckBox cb = row.FindControl("checkboxes") as CheckBox;
                cb.Checked = false;
            }

        }
    }
4

2 回答 2

1

如果您想在客户端执行此操作(通常您会这样做,用户希望选中复选框来选中所有框),您将需要在页面生命周期的渲染时来自控件的 ClientID。

在 4.0 之前,您可以“作弊”并查看呈现的页面(从浏览器查看源代码)。但是,这是一种脆弱的方法,因为它可能会随着 .aspx 页面的每次编辑而改变。

如果您有最新的框架(4.0 或更高版本),您可以设置ClientIDMode为静态。然后,您将能够使用 ID 属性中的值作为 ClientID。

http://msdn.microsoft.com/en-us/library/1d04y8ss%28v=vs.100%29.aspx

于 2013-08-14T17:14:12.863 回答
1

当您遍历网格中的所有行时,您需要检查行的类型,如下所示:

foreach (GridViewRow row in ApplicationsGridView.Rows)
{
    if(row.RowType == DataControlRowType.Header)
    {
        // Search for checkbox by ID here
        CheckBox theCheckBox = row.FindControl("checkall") as CheckBox;

        // Do whatever you need to do with checkbox here
    }
}

更新:

您不需要搜索实际控件,因为复选框启动了事件,所以您可以这样做:

protected void checkall_CheckedChanged(object sender, EventArgs e)
{
    // Cast the sender of the event to a check box, because the check box created this event
    CheckBox theCheckBox = sender as CheckBox;

    if (theCheckBox.Checked)
    {
        foreach (GridViewRow row in ApplicationsGridView.Rows)
        {   
            // Here is where you want to search for the existing check boxes, not create new ones
            CheckBox cb = row.FindControl("checkboxes") as CheckBox;
            cb.Checked = true;
        }
    }
}
于 2013-08-14T17:16:42.000 回答