使用asp.net进行Telerik网格插入更新

时间:2014-11-12 15:06:48

标签: c# asp.net telerik

在下面的代码我有radgrid我想在网格中插入,删除,更新。但我不知道是否获取值并放置代码,因为我是telerik控件的新手。

ASP:

<telerik:RadGrid ID="RadGrid1" runat="server"   AllowPaging="True" AllowAutomaticUpdates="True" AllowAutomaticInserts="True"
        AllowAutomaticDeletes="true" AllowSorting="true" OnItemCreated="RadGrid1_ItemCreated"
        OnItemInserted="RadGrid1_ItemInserted" OnPreRender="RadGrid1_PreRender"  >
            <PagerStyle Mode="NextPrevAndNumeric" />
        <MasterTableView AutoGenerateColumns="False"
            DataKeyNames="EmployeeTypeID" CommandItemDisplay="Top">
            <Columns>
                <telerik:GridEditCommandColumn ButtonType="ImageButton" UniqueName="EditCommandColumn">
                </telerik:GridEditCommandColumn>
                <telerik:GridBoundColumn DataField="EmployeeTypeID" HeaderText="EmployeeTypeID" SortExpression="EmployeeTypeID"
                    UniqueName="EmployeeTypeID" Visible="false" >
                </telerik:GridBoundColumn>
                <telerik:GridBoundColumn DataField="EmployeeType" HeaderText="EmployeeType" SortExpression="EmployeeType"
                    UniqueName="EmployeeType">
                </telerik:GridBoundColumn>
                <telerik:GridBoundColumn DataField="CostPerDay" HeaderText="CostPerDay" SortExpression="CostPerDay"
                    UniqueName="CostPerDay">
                </telerik:GridBoundColumn>
                <telerik:GridBoundColumn DataField="WorkingHours" HeaderText="WorkingHours" SortExpression="WorkingHours"
                    UniqueName="WorkingHours">
                </telerik:GridBoundColumn>

                <telerik:GridButtonColumn Text="Delete" CommandName="Delete" ButtonType="ImageButton" />
            </Columns>
            <EditFormSettings>
                <EditColumn ButtonType="ImageButton" />
            </EditFormSettings>
        </MasterTableView>
            </telerik:RadGrid>

C#

protected void RadGrid1_ItemCreated(object sender, Telerik.Web.UI.GridItemEventArgs e)
        {
            if (e.Item is GridEditableItem && e.Item.IsInEditMode)
            {
                if (!(e.Item is GridEditFormInsertItem))
                {
                    GridEditableItem item = e.Item as GridEditableItem;
                    GridEditManager manager = item.EditManager;
                    GridTextBoxColumnEditor editor = manager.GetColumnEditor("EmployeeTypeID") as GridTextBoxColumnEditor;
                    editor.TextBoxControl.Enabled = false;
                }
            }
        }
        protected void RadGrid1_ItemInserted(object source, GridInsertedEventArgs e)
        {
            if (e.Exception != null)
            {

                e.ExceptionHandled = true;
                SetMessage("Employee cannot be inserted. Reason: " + e.Exception.Message);

            }
            else
            {
                SetMessage("New Employee is inserted!");
            }
        }
        private void DisplayMessage(string text)
        {
            RadGrid1.Controls.Add(new LiteralControl(string.Format("<span style='color:red'>{0}</span>", text)));
        }

        private void SetMessage(string message)
        {
            gridMessage = message;
        }

        private string gridMessage = null;

        protected void RadGrid1_PreRender(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(gridMessage))
            {
                DisplayMessage(gridMessage);
            }
        }

2 个答案:

答案 0 :(得分:1)

你的ASPX中有insert命令处理程序,但你缺少删除处理程序。

<telerik:RadGrid ID="RadGrid1" 
                 runat="server"   
                 AllowPaging="True" 
                 OnDeleteCommand="RadGrid1_DeleteCommand"
                 ...

对网格的插入和删除会自动发生,但它们不会自动发生在您的数据库中。

protected void RadGrid1_ItemInserted(object source, GridInsertedEventArgs e)
{
    //...ADD CODE TO INSERT TO YOUR DB HERE!
    if (e.Exception != null)
    {
     ...

Telerik's edit examples

protected void RadGrid1_DeleteCommand(object source, GridInsertedEventArgs e)
{
    //...ADD CODE TO DELETE FROM YOUR DB HERE!
    ...

Telerik's deleting example

别忘了重新绑定网格以查看结果。 RadGrid1.DataBind();

答案 1 :(得分:0)

另一种选择是在数据源中定义更新/删除/插入操作(例如,SqlDataSource控件,它具有适当的SQL命令作为属性,并且它们可以具有参数)并使用内置的CRUD操作:{{3 }}