invoke method batcheditcommand from C#.net [duplic

2019-09-20 12:06发布

问题:

This question is an exact duplicate of:

  • how to use webmethod with telerik batch edit grid 1 answer

I used telerik:RadGrid batch editing; to fill this grid I used below syntax:

function GridBind(GridID, GridData) {
    var TableView = GridID.get_masterTableView();
    TableView.set_dataSource(GridData); TableView.dataBind();
}

To Invoke batcheditcommand I used below syntax. It’s written under non-postback button Javascript event:

function SaveAllChanges(sender,args) {
    var batchManager = $find('<%=RadGrid1.ClientID%>').get_batchEditingManager();
    var tableViews = [];
    tableViews.push($find('<%=RadGrid1.ClientID%>').get_masterTableView());
    batchManager.saveTableChanges(tableViews);
}

But unfortunately it did not fire server event RadGrid1_BatchEditCommand, so I want to invoke Radgrid batcheditcommand from code behind in C#.net.

回答1:

A quick POC that worked for me:

markup:

    <telerik:RadGrid ID="RadGrid1" runat="server" OnBatchEditCommand="RadGrid1_BatchEditCommand">
        <MasterTableView EditMode="Batch">
            <Columns>
                <telerik:GridBoundColumn DataField="first"></telerik:GridBoundColumn>
                <telerik:GridBoundColumn DataField="second"></telerik:GridBoundColumn>
            </Columns>
        </MasterTableView>
    </telerik:RadGrid>
    <asp:Button ID="Button1" Text="bind grid" OnClientClick="bindTheGrid(); return false;" runat="server" />
    <asp:Button ID="Button2" Text="save grid" OnClientClick="SaveAllChanges(); return false;" runat="server" />
    <script>
        function bindTheGrid() {
            var grid = $find("<%=RadGrid1.ClientID%>");
            var data = [{ first: 1, second: 1 }, { first: 2, second: 2 }];
            GridBind(grid, data);
        }
        function GridBind(GridID, GridData) {
            var TableView = GridID.get_masterTableView();
            TableView.set_dataSource(GridData);
            TableView.dataBind();
        }

        function SaveAllChanges(sender, args) {
            var batchManager = $find('<%=RadGrid1.ClientID%>').get_batchEditingManager();
            var tableViews = [];
            tableViews.push($find('<%=RadGrid1.ClientID%>').get_masterTableView());
            batchManager.saveTableChanges(tableViews);
        }
    </script>

and server code

protected void Page_Load(object sender, EventArgs e)
    {
        if(!IsPostBack)
        {
            RadGrid1.DataSource="";
            RadGrid1.DataBind();
        }
    }
    protected void RadGrid1_BatchEditCommand(object sender, GridBatchEditingEventArgs e)
    {
        Response.Write(DateTime.Now.ToString());
    }

where you would need to work a bit on the client-side binding.

Make sure you have no script errors.

In the first function, do you pass a grid object or an ID in the GridID object? If only and ID, use $find() first to get a reference.