gpt4 book ai didi

asp.net数据录入表

转载 作者:行者123 更新时间:2023-12-04 06:50:06 24 4
gpt4 key购买 nike

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

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

如何从文本框中获取数据到“网格”中?

编辑

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

   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);

最佳答案

好的,您的评论中的第一个问题:

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.
}

关于asp.net数据录入表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3259685/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com