RadGrid ItemDataBound 事件导致另一个事件被忽略

发布于 2024-11-29 05:02:49 字数 4976 浏览 3 评论 0原文

我有一个包含 RadGrid 和 RadToolBar 控件(如下)的用户控件。

<telerik:RadToolBar ID="RadToolBar1" runat="server" Skin="Web20" style="width:100%;" OnButtonClick="RadToolBar1_ButtonClick">
    <Items>
        <telerik:RadToolBarButton ImageUrl="~/[path_omitted]/SaveRadToolBar1.png" Text="Save" ToolTip="Save" />
    </Items>
</telerik:RadToolBar>


<telerik:RadGrid ID="RadGrid1" runat="server" Skin="Vista" AutoGenerateColumns="false" OnNeedDataSource="RadGrid1_NeedDataSource"> 
    <MasterTableView CommandItemDisplay="None" Height="30" DataKeyNames="ID" ClientDataKeyNames="ID" GroupLoadMode="Client" NoMasterRecordsText="You do not have any data"> 
        <GroupByExpressions> 
            <telerik:GridGroupByExpression> 
                <GroupByFields> 
                    <telerik:GridGroupByField FieldName="Topic.Category.Name" /> 
                </GroupByFields> 
                <SelectFields> 
                    <telerik:GridGroupByField FieldName="Topic.Category.Name" HeaderText="Category" /> 
                </SelectFields> 
            </telerik:GridGroupByExpression> 
        </GroupByExpressions> 

        <Columns> 
            <telerik:GridBoundColumn DataField="ID" HeaderText="ID" UniqueName="DataID" Visible="false" />
            <telerik:GridBoundColumn DataField="Topic.ID" UniqueName="DataTopicID" Visible="false" />
            <telerik:GridBoundColumn DataField="Topic.Category.Name" HeaderText="Name" UniqueName="DataCategoryName" Visible="false" /> 
            <telerik:GridBoundColumn DataField="Topic.Name" HeaderText="Topic" UniqueName="DataTopicName" /> 
            <telerik:GridTemplateColumn HeaderText="Go" UniqueName="DataGoTo" HeaderStyle-Width="50"> 
                <ItemTemplate> 
                    <asp:Button ID="ButtonGoTo" runat="server" ToolTip="Go to data" Text="Go" /> 
                </ItemTemplate> 
            </telerik:GridTemplateColumn> 
        </Columns> 
    </MasterTableView> 
</telerik:RadGrid> 

RadToolBar ButtonClick 事件启动回发以保存页面内容(该页面包含其他控件,例如下拉列表、文本框等,但没有一个与我遇到的问题相关)。使用上面的代码,一切都按预期工作(RadToolBar ButtonClick 事件处理程序被调用,页面上的内容被保存)。

当我为 RadGrid 的 ItemDataBound 事件添加事件处理程序时,会出现问题(见下文)。

<telerik:RadGrid ID="RadGrid1" runat="server" Skin="Vista" AutoGenerateColumns="false" OnNeedDataSource="RadGrid1_NeedDataSource" OnItemDataBound="RadGrid1_ItemDataBound"> 
    <MasterTableView CommandItemDisplay="None" Height="30" DataKeyNames="ID" ClientDataKeyNames="ID" GroupLoadMode="Client" NoMasterRecordsText="You do not have any data"> 
        <GroupByExpressions> 
            <telerik:GridGroupByExpression> 
                <GroupByFields> 
                    <telerik:GridGroupByField FieldName="Topic.Category.Name" /> 
                </GroupByFields> 
                <SelectFields> 
                    <telerik:GridGroupByField FieldName="Topic.Category.Name" HeaderText="Category" /> 
                </SelectFields> 
            </telerik:GridGroupByExpression> 
        </GroupByExpressions> 

        <Columns> 
            <telerik:GridBoundColumn DataField="ID" HeaderText="ID" UniqueName="DataID" Visible="false" />
            <telerik:GridBoundColumn DataField="Topic.ID" UniqueName="DataTopicID" Visible="false" />
            <telerik:GridBoundColumn DataField="Topic.Category.Name" HeaderText="Name" UniqueName="DataCategoryName" Visible="false" /> 
            <telerik:GridBoundColumn DataField="Topic.Name" HeaderText="Topic" UniqueName="DataTopicName" /> 
            <telerik:GridTemplateColumn HeaderText="Go" UniqueName="DataGoTo" HeaderStyle-Width="50"> 
                <ItemTemplate> 
                    <asp:Button ID="ButtonGoTo" runat="server" ToolTip="Go to data" Text="Go" /> 
                </ItemTemplate> 
            </telerik:GridTemplateColumn> 
        </Columns> 
    </MasterTableView> 
</telerik:RadGrid> 

protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e) 
{ 
    if (e.Item is GridDataItem) 
    { 
        GridDataItem dataItem = e.Item as GridDataItem; 
        (dataItem["DataGoTo"].FindControl("ButtonGoTo") as Button) 
            .PostBackUrl = String.Format( 
                "~/ShowData.aspx?id={0}", 
                dataItem["DataTopicID"].Text); 
    } 
}

我使用此处理程序来设置 Button 控件的 PostBackUrl(我在此事件上执行此操作,因为我需要 RadGrid 行内容的 ID)。问题是 RadToolBar ButtonClick 事件处理程序不再被调用。

一切正常后,事件顺序为:加载 ->需要数据源->按钮点击。添加 ItemDataBound 事件后,顺序为 Load ->需要数据源-> ItemDataBound(ButtonClick 事件被“忽略”)。

为什么在 RadGrid 中添加 ItemDataBound 事件会影响 RadToolBar 中的 ButtonClick 事件?如何让 ButtonClick 事件“不被忽略”,同时仍保留 ItemDataBound 事件?

谢谢。

I have a user control that contains a RadGrid and a RadToolBar control (below).

<telerik:RadToolBar ID="RadToolBar1" runat="server" Skin="Web20" style="width:100%;" OnButtonClick="RadToolBar1_ButtonClick">
    <Items>
        <telerik:RadToolBarButton ImageUrl="~/[path_omitted]/SaveRadToolBar1.png" Text="Save" ToolTip="Save" />
    </Items>
</telerik:RadToolBar>


<telerik:RadGrid ID="RadGrid1" runat="server" Skin="Vista" AutoGenerateColumns="false" OnNeedDataSource="RadGrid1_NeedDataSource"> 
    <MasterTableView CommandItemDisplay="None" Height="30" DataKeyNames="ID" ClientDataKeyNames="ID" GroupLoadMode="Client" NoMasterRecordsText="You do not have any data"> 
        <GroupByExpressions> 
            <telerik:GridGroupByExpression> 
                <GroupByFields> 
                    <telerik:GridGroupByField FieldName="Topic.Category.Name" /> 
                </GroupByFields> 
                <SelectFields> 
                    <telerik:GridGroupByField FieldName="Topic.Category.Name" HeaderText="Category" /> 
                </SelectFields> 
            </telerik:GridGroupByExpression> 
        </GroupByExpressions> 

        <Columns> 
            <telerik:GridBoundColumn DataField="ID" HeaderText="ID" UniqueName="DataID" Visible="false" />
            <telerik:GridBoundColumn DataField="Topic.ID" UniqueName="DataTopicID" Visible="false" />
            <telerik:GridBoundColumn DataField="Topic.Category.Name" HeaderText="Name" UniqueName="DataCategoryName" Visible="false" /> 
            <telerik:GridBoundColumn DataField="Topic.Name" HeaderText="Topic" UniqueName="DataTopicName" /> 
            <telerik:GridTemplateColumn HeaderText="Go" UniqueName="DataGoTo" HeaderStyle-Width="50"> 
                <ItemTemplate> 
                    <asp:Button ID="ButtonGoTo" runat="server" ToolTip="Go to data" Text="Go" /> 
                </ItemTemplate> 
            </telerik:GridTemplateColumn> 
        </Columns> 
    </MasterTableView> 
</telerik:RadGrid> 

The RadToolBar ButtonClick event inits a postback to save the contents of the page (the page contains other controls such as drop down list , text box, etc. but none are related to the issue I am having). With the above code everything works as expected (the RadToolBar ButtonClick event handler get called and the content on the page is saved).

The problem occurs when I add an event handler for the RadGrid's ItemDataBound event (see below).

<telerik:RadGrid ID="RadGrid1" runat="server" Skin="Vista" AutoGenerateColumns="false" OnNeedDataSource="RadGrid1_NeedDataSource" OnItemDataBound="RadGrid1_ItemDataBound"> 
    <MasterTableView CommandItemDisplay="None" Height="30" DataKeyNames="ID" ClientDataKeyNames="ID" GroupLoadMode="Client" NoMasterRecordsText="You do not have any data"> 
        <GroupByExpressions> 
            <telerik:GridGroupByExpression> 
                <GroupByFields> 
                    <telerik:GridGroupByField FieldName="Topic.Category.Name" /> 
                </GroupByFields> 
                <SelectFields> 
                    <telerik:GridGroupByField FieldName="Topic.Category.Name" HeaderText="Category" /> 
                </SelectFields> 
            </telerik:GridGroupByExpression> 
        </GroupByExpressions> 

        <Columns> 
            <telerik:GridBoundColumn DataField="ID" HeaderText="ID" UniqueName="DataID" Visible="false" />
            <telerik:GridBoundColumn DataField="Topic.ID" UniqueName="DataTopicID" Visible="false" />
            <telerik:GridBoundColumn DataField="Topic.Category.Name" HeaderText="Name" UniqueName="DataCategoryName" Visible="false" /> 
            <telerik:GridBoundColumn DataField="Topic.Name" HeaderText="Topic" UniqueName="DataTopicName" /> 
            <telerik:GridTemplateColumn HeaderText="Go" UniqueName="DataGoTo" HeaderStyle-Width="50"> 
                <ItemTemplate> 
                    <asp:Button ID="ButtonGoTo" runat="server" ToolTip="Go to data" Text="Go" /> 
                </ItemTemplate> 
            </telerik:GridTemplateColumn> 
        </Columns> 
    </MasterTableView> 
</telerik:RadGrid> 

protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e) 
{ 
    if (e.Item is GridDataItem) 
    { 
        GridDataItem dataItem = e.Item as GridDataItem; 
        (dataItem["DataGoTo"].FindControl("ButtonGoTo") as Button) 
            .PostBackUrl = String.Format( 
                "~/ShowData.aspx?id={0}", 
                dataItem["DataTopicID"].Text); 
    } 
}

I use this handler to set the PostBackUrl of an Button control (I do it on this event because I need the ID of the RadGrid's row content). The problem is that the RadToolBar ButtonClick event handler doesn't get called anymore.

With everything working, the event sequence is: Load -> NeedDataSource -> ButtonClick. After adding the ItemDataBound event the sequence is Load -> NeedDataSource -> ItemDataBound (the ButtonClick event gets "ignored").

Why does adding the ItemDataBound event in the RadGrid affect the ButtonClick event in the RadToolBar? How can I get the ButtonClick event to "not be ignored" while still maintaining the ItemDataBound event?

Thanks.

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

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

发布评论

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

评论(1

天煞孤星 2024-12-06 05:02:49
  1. 没有 onClick =" yourHandlerName " 可以让您的 ButtonClick 事件触发。

  2. 您可以使用 telerik GridButtonColumn 列并设置 CommandName = "DoDomething" 并在网格的 OnItemCommand="Grid1_ItemCommand 事件处理程序中触发它.

  1. there is no onClick =" yourHandlerName " for your buttonclick event to get fired.

  2. you can use telerik GridButtonColumn column and set CommandName = "DoDomething" and fire it in the OnItemCommand="Grid1_ItemCommand eventhandler of your grid.

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