防止Telerik GridButtonColumn上的回发单击RadGrid

时间:2015-03-02 06:22:01

标签: asp.net telerik

当我在RadGrid的GridButtonColumn中单击“图像”按钮时,页面将被刷新。对此有什么解决方案吗?

<telerik:RadGrid ID="GrdCol" runat="server" Height="200px" Width="970px"
                 CellSpacing="0" GridLines="Horizontal" TabIndex="30"
                 AutoGenerateColumns="False" Skin="WebBlue" Font-Size="8pt"
                 Font-Names="Tahoma" AllowPaging="false" EnableViewState="False">
  <ClientSettings>
    <Scrolling ScrollHeight="300" AllowScroll="true" UseStaticHeaders="true" />
    <ClientEvents OnCommand="GrdCol_OnCommand" OnRowDataBound="GrdCol_RowDataBound" />
    <DataBinding ShowEmptyRowsOnLoad="true" SelectMethod="GetMetrics"/>
  </ClientSettings>
  <MasterTableView ShowFooter="false" EditMode="InPlace"
                   CommandItemDisplay="Top" TableLayout="Fixed">
    <CommandItemSettings ShowRefreshButton="false"/>
    <Columns>
      <telerik:GridButtonColumn HeaderText="Edit" ButtonType="ImageButton"
                                CommandName="Edit" UniqueName="EditColumn"
                                ImageUrl="../Images/Edit.png">
        <HeaderStyle HorizontalAlign="Center" Width="40px" />
        <ItemStyle HorizontalAlign="Center">
        </ItemStyle>
      </telerik:GridButtonColumn>
    </Columns>
  </MasterTableView>
</telerik:RadGrid>

2 个答案:

答案 0 :(得分:3)

对于其他任何寻找解决方案的人来说......

<telerik:RadGrid 
    ID="MyRadGrid"        
    runat="server">

    <ClientSettings>
        <ClientEvents OnCommand="onItemCommand"/>
    </ClientSettings>

    <MasterTableView>
        <Columns>
            <telerik:GridButtonColumn
                    ButtonType="PushButton"
                    CommandName="MyCommandName"
                    Text="Click Here"
                    CommandArgument="MyCommandArgument">
            </telerik:GridButtonColumn>        
        </Columns>
    </MasterTableView>
</telerik:RadGrid>


function onItemCommand(sender, eventArgs) {
    try {
        //retrieve the current commandName and commandArgument
        var commandName =  eventArgs.get_commandName();
        var commandArgument = eventArgs.get_commandArgument();

        //cancel the command - this prevents the post back
        eventArgs.set_cancel(true); 
    } catch (e) {
        console.log(e);
    }
}

答案 1 :(得分:-1)

在运行时在代码后面附加onClick事件并返回false。这应该如下:

protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e)
{
   if (e.Item is GridDataItem)
   {
       GridDataItem item = (GridDataItem)e.Item;
       Button button = (Button)item["EditColumn"].Controls[0];
       button.Attributes.Add("OnClick", "DoSomething(); return false;");
   }
}

不要忘记在网格中附加RadGrid1_ItemCreated事件。