ASP.NET:在 DataGrid 的 TemplateColumn 中声明的访问控件

发布于 2024-09-15 16:13:01 字数 1664 浏览 8 评论 0原文

ASCX 文件:

<asp:datagrid runat="server" id="gridFormFields" datakeyfield="FieldID"
autogeneratecolumns="False" 
onitemcommand="gridFormFields_ItemCommand" onitemdatabound="gridFormFields_ItemDataBound">
<columns>
    <asp:templatecolumn>
        <itemtemplate>
            <asp:imagebutton runat="server" id="buttonMoveUpFormField" resourcekey="buttonMoveUpFormField"
            commandname="Item" commandargument="MoveUp" imageurl="~/images/up.gif" />
        </itemtemplate>
    </asp:templatecolumn>
    <asp:templatecolumn>
        <itemtemplate>
            <asp:imagebutton runat="server" id="buttonMoveDownFormField" resourcekey="buttonMoveDownFormField" 
            commandname="Item" commandargument="MoveDown" imageurl="~/images/dn.gif" />
        </itemtemplate>
    </asp:templatecolumn>
</columns>

隐藏代码:

protected void gridFormFields_ItemDataBound(object sender, DataGridItemEventArgs e)
    {
        (e.Item.FindControl("buttonMoveUpFormField") as ImageButton)
         .Visible = gridFormFields.Items.Count > 1 && e.Item.ItemIndex > 0;

        (e.Item.FindControl("buttonMoveDownFormField") as ImageButton)
         .Visible = gridFormFields.Items.Count > 1 && e.Item.ItemIndex < gridFormFields.Items.Count - 1;
    }
  • 在隐藏代码中,FindControl 返回的 Control 为 null。为什么?

  • 如何访问buttonMoveUpFormField 和buttonMoveDownFormField 控件?

  • 从后面的代码来看,是否可以访问在 DataGrid 的 TemplateColumn 部分的 ItemTemplate 部分中声明的控件?

ASCX File:

<asp:datagrid runat="server" id="gridFormFields" datakeyfield="FieldID"
autogeneratecolumns="False" 
onitemcommand="gridFormFields_ItemCommand" onitemdatabound="gridFormFields_ItemDataBound">
<columns>
    <asp:templatecolumn>
        <itemtemplate>
            <asp:imagebutton runat="server" id="buttonMoveUpFormField" resourcekey="buttonMoveUpFormField"
            commandname="Item" commandargument="MoveUp" imageurl="~/images/up.gif" />
        </itemtemplate>
    </asp:templatecolumn>
    <asp:templatecolumn>
        <itemtemplate>
            <asp:imagebutton runat="server" id="buttonMoveDownFormField" resourcekey="buttonMoveDownFormField" 
            commandname="Item" commandargument="MoveDown" imageurl="~/images/dn.gif" />
        </itemtemplate>
    </asp:templatecolumn>
</columns>

Code behind:

protected void gridFormFields_ItemDataBound(object sender, DataGridItemEventArgs e)
    {
        (e.Item.FindControl("buttonMoveUpFormField") as ImageButton)
         .Visible = gridFormFields.Items.Count > 1 && e.Item.ItemIndex > 0;

        (e.Item.FindControl("buttonMoveDownFormField") as ImageButton)
         .Visible = gridFormFields.Items.Count > 1 && e.Item.ItemIndex < gridFormFields.Items.Count - 1;
    }
  • In the code behind, the Control returned by FindControl is null. Why?

  • How can I access the buttonMoveUpFormField and buttonMoveDownFormField controls?

  • From the code behind, is it possible to access controls which are declared in the ItemTemplate section of the TemplateColumn section of a DataGrid?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

还在原地等你 2024-09-22 16:13:01

因为在尝试查找该控件之前,您需要添加代码以包含“Item”和“AlternatingItem”并排除所有其他类型。

if (e.Item.Type == ...

Because you need to add code to include "Item" and "AlternatingItem" and exclude all other types, before you try to find that control.

if (e.Item.Type == ...

可是我不能没有你 2024-09-22 16:13:01

您当然可以访问 ItemTemplate 部分中的控件。我正在处理类似的问题。我发现的一件事是,根据调用“gridFormFields_ItemDataBound”的内容,您可能还无法访问这些控件。

我知道在我的实例中,我有一个“ItemTemplate”和一个“EditItemTemplate”,当我单击编辑时,它会在实际切换到“编辑模式”之前触发一个事件“RowEditing”,因此控件不会还在那里。我确实可以访问“RowUpdating”中的控件,当我在编辑模式下单击“保存”时会触发该控件。

也许这有帮助?例如,您的“OnDataBound”可能是尝试访问控件的事件,但您可能无权在数据绑定上访问它们?

只是一个想法。如果我有进一步的了解,我会编辑这个。

You can certainly access the controls that are within the ItemTemplate section. I'm dealing with a similar issue. One thing that I've found is, depending what is calling your "gridFormFields_ItemDataBound", you may not have access to those controls yet.

I know that in my instance, I've got an "ItemTemplate" and an "EditItemTemplate", when I click edit, it fires an event "RowEditing" before it is actually switched to "Edit Mode", so the control will not be there yet. I do though have access to the controls in "RowUpdating" which is fired when I click save in the edit mode.

Maybe this helps? For example, your "OnDataBound" might be the event that is trying to access your controls, but you may not have access to them on databound?

Just a thought. I'll edit this if I get any further on mine.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文