You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

RadGrid中GridCommandEventArgs的Item.Cells无有效数据值问题求助

Telerik RadGrid InsertCommand中e.Item.Cells.Text显示 的原因及解决方案

问题原因

在InsertCommand事件触发时,RadGrid的插入项(InsertItem)中的输入控件并非直接存储在e.Item.Cells中,Cells里仅包含占位符或空的HTML元素( 是HTML空格字符)。而插入数据能正常写入数据库,是因为你可能使用了RadGrid的自动数据绑定(如绑定SqlDataSource),控件值会被自动传递到数据源参数中,无需手动通过Cells读取。

正确获取插入数据的方法

1. 通过FindControl获取模板中的输入控件

无论是否使用自动数据绑定,都需要直接从InsertItemTemplate定义的控件中取值:

protected void RadGrid1_InsertCommand(object sender, GridCommandEventArgs e)
{
    // 转换为插入项
    GridEditFormItem insertItem = e.Item as GridEditFormItem;
    // 获取模板中的TextBox控件(ID对应你在前台定义的控件ID)
    TextBox txtName = (TextBox)insertItem.FindControl("txtName");
    TextBox txtAge = (TextBox)insertItem.FindControl("txtAge");
    
    // 读取控件值
    string name = txtName.Text;
    int age = int.Parse(txtAge.Text);
}

对应的前台模板示例:

<telerik:GridTemplateColumn HeaderText="姓名">
    <InsertItemTemplate>
        <asp:TextBox ID="txtName" runat="server"></asp:TextBox>
    </InsertItemTemplate>
    <ItemTemplate>
        <%# Eval("Name") %>
    </ItemTemplate>
</telerik:GridTemplateColumn>

2. 从数据源参数中取值(自动绑定场景)

如果使用SqlDataSource等数据源控件,也可以直接访问数据源的InsertParameters:

protected void RadGrid1_InsertCommand(object sender, GridCommandEventArgs e)
{
    SqlDataSource sqlDs = RadGrid1.DataSource as SqlDataSource;
    string name = sqlDs.InsertParameters["Name"].DefaultValue;
    // 注意:如果控件是双向绑定(如使用Bind()),参数值会自动填充
}

额外需求处理

1. 增改前校验

在Insert/UpdateCommand事件中,先获取控件值再执行校验,校验失败则取消操作:

protected void RadGrid1_InsertCommand(object sender, GridCommandEventArgs e)
{
    GridEditFormItem insertItem = e.Item as GridEditFormItem;
    TextBox txtName = (TextBox)insertItem.FindControl("txtName");
    
    if (string.IsNullOrWhiteSpace(txtName.Text))
    {
        e.Canceled = true; // 取消插入
        insertItem.ErrorMessage = "姓名不能为空"; // 显示错误提示
        return;
    }
    
    // 其他校验逻辑,如年龄范围、格式验证等
}

也可以在前台添加ASP.NET验证控件(如RequiredFieldValidator)实现前端校验,减少后端压力。

2. 基于其他数据设置字段

插入时自动填充创建时间、操作人等字段:

protected void RadGrid1_InsertCommand(object sender, GridCommandEventArgs e)
{
    SqlDataSource sqlDs = RadGrid1.DataSource as SqlDataSource;
    // 设置自动字段值
    sqlDs.InsertParameters["CreateTime"].DefaultValue = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
    sqlDs.InsertParameters["Operator"].DefaultValue = User.Identity.Name;
}

如果是手动拼接SQL,直接在SQL语句中加入这些字段值即可。

3. 记录操作日志

建议在Inserted/Updated/Deleted事件中记录日志,确保操作成功后再写入:

protected void RadGrid1_Inserted(object sender, GridInsertedEventArgs e)
{
    if (!e.ExceptionHandled && e.Exception == null)
    {
        // 获取插入的数据(可从e.Item或数据源参数获取)
        GridEditFormItem insertItem = RadGrid1.MasterTableView.GetInsertItem() as GridEditFormItem;
        string name = ((TextBox)insertItem.FindControl("txtName")).Text;
        
        // 写入日志
        string logContent = $"用户{User.Identity.Name}于{DateTime.Now}成功插入数据:姓名={name}";
        SaveOperationLog(logContent);
    }
}

// 日志存储方法示例
private void SaveOperationLog(string content)
{
    using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["YourConnStr"].ConnectionString))
    {
        string sql = "INSERT INTO OperationLog (Content, CreateTime) VALUES (@Content, @CreateTime)";
        SqlCommand cmd = new SqlCommand(sql, conn);
        cmd.Parameters.AddWithValue("@Content", content);
        cmd.Parameters.AddWithValue("@CreateTime", DateTime.Now);
        conn.Open();
        cmd.ExecuteNonQuery();
    }
}

内容的提问来源于stack exchange,提问作者Michael Foster

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.01 07:15:33