自动获取form内的name值,然后装载到Datatable

本文介绍了一种方法,用于从Web表单中提取特定前缀的字段值,并将其填充到DataTable的第一行。此过程适用于ASP.NET环境,通过遍历表单集合并筛选出符合前缀条件的项。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

using System;
using System.Data;
using System.Web;

/// <summary>
/// 获取 已 paraName 开头的From表单中的值,填充到DataTable中。
/// </summary>
/// <param name="dt">数据表</param>
/// <param name="ParaName">开头字段</param>
/// <returns></returns>
public static void getFromSameRecord(ref DataTable dt, string ParaName)
{
    if (dt != null && dt.Rows.Count != 0)
    {
        try
        {
            ParaName = ParaName.ToLower();
            int count = HttpContext.Current.Request.Form.Keys.Count;
            for (int i = 0; i < count; i++)
            {
                string text = HttpContext.Current.Request.Form.GetKey(i).ToLower();
                int num = text.IndexOf(ParaName);
                if (num >= 0)
                {
                    string text2 = text.Substring(num, text.Length - num);
                    if (dt.Columns.Contains(text2))
                    {
                        string value = HttpContext.Current.Request.Form[i].ToString().Trim();
                        if (!string.IsNullOrEmpty(value))
                        {
                            dt.Rows[0][text2] = value;
                        }
                        else
                        {
                            dt.Rows[0][text2] = DBNull.Value;
                        }
                    }
                }
            }
        }
        catch
        {
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值