asp.net数据输入表

时间:2010-07-15 20:20:52

标签: asp.net

我想构建一个表单,用户可以在几个文本框中输入一些数据,然后单击“添加”按钮,让数据显示在网格或类似的内容中。他们需要能够输入多行。

然后,当他们完成后,他们可以单击“保存”按钮将数据保存到数据库。

如何将文本框中的数据导入“网格”?

修改

这是我到目前为止所拥有的

   protected void Page_Load(object sender, EventArgs e)

{

    DataTable myDataTable = new DataTable();

    DataColumn dc1 = new DataColumn("Employee");
    myDataTable.Columns.Add(dc1);
    DataColumn dc2 = new DataColumn("Category");
    myDataTable.Columns.Add(dc2);
    DataColumn dc3 = new DataColumn("Description");
    myDataTable.Columns.Add(dc3);
    DataColumn dc4 = new DataColumn("P/S/N");
    myDataTable.Columns.Add(dc4);
    DataColumn dc5 = new DataColumn("Hours");
    myDataTable.Columns.Add(dc5);
    DataColumn dc6 = new DataColumn("WeekEnding");
    myDataTable.Columns.Add(dc6);

}
protected void btnAddToGrid_Click(object sender, EventArgs e)

{
    DataRow row = myDataTable.NewRow();// i'm getting error here sayind myDataTable does not exist

    row["Employee"] = LoginName1.ToString();
    row["Category"] = ddlCategory.SelectedValue;
    row["Description"] = txtDescription.Text;
    row["P/S/N"] = ddlPSN.SelectedValue;
    row["Hours"] = ddlHours.SelectedValue;
    row["WeekEnding"] = txtWeekEnding.Text;

    myDataTable.Rows.Add(row);

2 个答案:

答案 0 :(得分:2)

好的,你的评论第一个问题:

i'm getting error here sayind myDataTable does not exist

是因为您在Page_Load中定义了表,然后它在函数末尾超出了范围。听起来你不了解ASP.NET的基本概念以及你想要做的事情。

这是一个快速而肮脏的未经测试的解决方案,但我必须强调,在尝试扩展或在ASP.NET中执行更多操作之前,您应该尝试理解此解决方案的原因。我希望我的10分钟时间可以帮助您更好地了解C#和ASP.NET。

[Serializable]
public class YourData
{
    public string Employee { get; set; }
    public string Category { get; set; }
    public string Description { get; set; }
    public string P_S_N { get; set; }
    public string Hours { get; set; }
    public string WeekEnding { get; set; }
}

// Used to add your list of data to the viewstate cache
// (There are other ways to store data between posts but I am trying to keep it simple)
public void SetYourCachedData(List<YourData> data)
{
    ViewState["YourDataCache"] = data;
}

// Used to get your save data so far
public List<YourData> GetYourCachedData()
{
    return ViewState["YourDataCache"] == null ?
        new List<YourData>() : (List<YourData>)(ViewState["YourDataCache"]);
}

protected void Page_Load(object sender, EventArgs e)
{
    // Do nothing
}

protected void btnAddToGrid_Click(object sender, EventArgs e)
{
    // Get the data and store it in the ViewState to cache it between post backs
    // Assuming one record added with each click.
    List<YourData> data = GetYourCachedData();
    data.Add(new YourData()
    {
        Employee = LoginName1.ToString(),
        Category = ddlCategory.SelectedValue,
        Description = txtDescription.Text,
        P_S_N = ddlPSN.SelectedValue,
        Hours = ddlHours.SelectedValue,
        WeekEnding = txtWeekEnding.Text
    });            

    // You can bind to any type of collection easily.
    // You don't need to use a DataTable with a GridView
    yourGrid.DataSource = data;
    yourGrid.DataBind();

    SetYourCachedData(data);
}

protected void btnSaveData_Click(object sender, EventArgs e)
{
    // Pull data from the ViewState cache
    List<YourData> data = GetYourCachedData();

    // Now iterate through data to save it to your database...
    // look this up if you don't know how as this is a lot more work.
}

答案 1 :(得分:1)

看起来你只需要设置输出对象的数据源并绑定它。

即:

myGrid.datasource = myDataTable
myGrid.databind()

正如Kelsey所说 - 保持数据库的范围。

如果您只是想尝试一下,那么在您的page_load上方的公共暗淡会更容易。否则,单独的类方法是一个很好的方法。