RadGrid:在编辑时获取修改后的项目

时间:2015-10-05 10:37:02

标签: asp.net c#-4.0 telerik radgrid

在RadGrid上我可以使用CommandItemTemplate来定义我自己的按钮,在我的例子中,保存并取消编辑(如下所示)



<CommandItemTemplate>
    <div style="padding: 5px 5px;">
        <asp:LinkButton ID="btnUpdateEdited" runat="server" CommandName="UpdateEdited">Update</asp:LinkButton>
        <asp:LinkButton ID="btnCancel" runat="server" CommandName="CancelAll">Cancel editing</asp:LinkButton>
    </div>
</CommandItemTemplate>
&#13;
&#13;
&#13;

在后面的代码中,我设置了ItemCommand。

> protected void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
{
    if (e.CommandName.CompareTo("UpdateEdited") == 0)
    {
        var commandItem = ((GridCommandItem)e.Item);
        //Updade code here.
    }
}

如何使用修改后的字段访问修改后的行,以便执行更新?

2 个答案:

答案 0 :(得分:0)

你应该在命令项中拥有它们,你可以像这样访问它们:

GridDataItem item = (GridDataItem)e.Item;
string value = item["ColumnUniqueName"].Text;

答案 1 :(得分:0)

这是你想要的吗?只是您可能需要修改它的示例..

的.aspx

<telerik:RadGrid ID="rg" runat="server" AutoGenerateColumns="false" 
 OnNeedDataSource="rg_NeedDataSource" OnItemCommand="rg_ItemCommand" 
 MasterTableView-CommandItemDisplay="Top" OnItemDataBound="rg_ItemDataBound">
        <MasterTableView EditMode="InPlace">
            <CommandItemTemplate>
                <div style="padding: 5px 5px;">
                    <asp:LinkButton ID="btnUpdateEdited" runat="server" 
                     CommandName="UpdateEdited">Update</asp:LinkButton>
                    &nbsp;
                    <asp:LinkButton ID="btnCancel" runat="server" 
                     CommandName="CancelAll">Cancel editing</asp:LinkButton>                        
                </div>
            </CommandItemTemplate>
            <Columns>
                <telerik:GridTemplateColumn>
                    <ItemTemplate>
                        <asp:Label ID="lbl" runat="server" 
                         Text='<%# Eval("A") %>' />
                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:TextBox ID="txt" runat="server" 
                         Text='<%# Eval("A") %>'></asp:TextBox>
                    </EditItemTemplate>
                </telerik:GridTemplateColumn>
                <telerik:GridTemplateColumn>
                    <ItemTemplate>
                        <asp:LinkButton ID="btnEdit" runat="server" Text="Edit" 
                         CommandName="Edit"></asp:LinkButton>
                    </ItemTemplate>
                    <EditItemTemplate>&nbsp;</EditItemTemplate>
                </telerik:GridTemplateColumn>
            </Columns>
        </MasterTableView>
</telerik:RadGrid>

的.cs

protected void Page_Load(object sender, EventArgs e)
{
    // Check
    if (!IsPostBack)
    {
        // Variable
        DataTable dt = new DataTable();
        dt.Columns.Add("A");
        dt.Rows.Add("A1");

        // Check & Bind
        if (dt != null)
        {
            // Viewstate
            ViewState["Data"] = dt;

            rg.DataSource = dt;
            rg.DataBind();

            dt.Dispose();
        }
    }
}

protected void rg_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
{
    rg.DataSource = ViewState["Data"] as DataTable;
}

protected void rg_ItemCommand(object sender, GridCommandEventArgs e)
{
    // Check
    if (e.CommandName == "UpdateEdited")
    {
        // Variable
        DataTable dt = new DataTable();
        dt.Columns.Add("A");

        // Loop All
        foreach (GridEditableItem item in rg.Items)
        {
            // Find Control
            TextBox txt = item.FindControl("txt") as TextBox;

            // Check & Add to DataTable
            if (txt != null) dt.Rows.Add(txt.Text.Trim());
        }

        // Check
        if (dt != null && dt.Rows.Count > 0)
        {
            // Set Viewstate
            ViewState["Data"] = dt;

            // Bind
            rg.DataSource = dt;
            rg.DataBind();

            // Dispose
            dt.Dispose();
        }
    }
    else if (e.CommandName == "CancelAll")
    {
        // Clear Edit Mode
        rg.MasterTableView.ClearChildEditItems();

        // Rebind
        rg.Rebind();            
    }
}

protected void rg_ItemDataBound(object sender, GridItemEventArgs e)
{
    // Check
    if (e.Item is GridCommandItem)
    {
        // Find Control
        LinkButton btnUpdateEdited = e.Item.FindControl("btnUpdateEdited") as LinkButton;
        LinkButton btnCancel = e.Item.FindControl("btnCancel") as LinkButton;

        // Get is Edit Mode ?
        if (rg.EditIndexes.Count > 0)
        {
            if (btnUpdateEdited != null) btnUpdateEdited.Visible = true;
            if (btnCancel != null) btnCancel.Visible = true;
        }
        else
        {
            if (btnUpdateEdited != null) btnUpdateEdited.Visible = false;
            if (btnCancel != null) btnCancel.Visible = false;
        }
    }
}