-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFormManage.aspx.cs
More file actions
154 lines (135 loc) · 5.36 KB
/
FormManage.aspx.cs
File metadata and controls
154 lines (135 loc) · 5.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml.Linq;
namespace DynamicFormFW
{
public partial class FormManage : Page
{
// 讀取配置文件中的連線字串
private string connectionString = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
private DataTable _dataTable = new DataTable();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
initialControls();
}
}
#region Function
/// <summary>
/// 控制項初始化
/// </summary>
private void initialControls()
{
_dataTable.Clear();
_dataTable = this.ExecuteQuery("SELECT[Id],[Name],[Description],[Version],[AtCreatDateTime],[EmpNo]" +
"FROM [DynamicForm].[dbo].[FormBasicInfo]");
// Check if the DataTable is empty
if (_dataTable.Rows.Count == 0)
{
// Create a new DataRow and add it to the DataTable
DataRow defaultFormListRow = _dataTable.NewRow();
_dataTable.Rows.Add(defaultFormListRow);
// Optionally, you can set default values for the columns
defaultFormListRow["Id"] = 0;
defaultFormListRow["Name"] = "請先按新增";
defaultFormListRow["Description"] = "-";
defaultFormListRow["Version"] = 0;
defaultFormListRow["AtCreatDateTime"] = DateTime.Now;
defaultFormListRow["EmpNo"] = "-";
}
GridViewFormList.DataSource = _dataTable;
GridViewFormList.DataBind();
}
#endregion
protected void btnInsert_Click(object sender, EventArgs e)
{
GridViewFormList.FooterRow.Visible = true;
}
protected void GridViewFormList_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Edit")
{
// 获取行索引
int rowIndex = Convert.ToInt32(e.CommandArgument);
// 使用 DataKeys 获取 ID
string id = GridViewFormList.DataKeys[rowIndex].Value.ToString();
Response.Redirect(string.Format("FormDeign.aspx?id={0}", id));
}
}
protected void btnSave_Click(object sender, EventArgs e)
{
String gvTbFormName, gvTbFormDescription;
gvTbFormName = ((TextBox)GridViewFormList.FooterRow.Cells[0].FindControl("gvTbFormName")).Text;
gvTbFormDescription = ((TextBox)GridViewFormList.FooterRow.Cells[0].FindControl("gvTbFormDescription")).Text;
/* 寫入資料 */
using (SqlConnection connection = new SqlConnection(connectionString))
{
// 假设您有一个 Users 表格,包含 ID、Username 和 Password 字段
string insertSql = "INSERT INTO [DynamicForm].[dbo].[FormBasicInfo] ([Name], [Description],[EmpNo]) VALUES (@Name, @Description, @EmpNo)";
using (SqlCommand command = new SqlCommand(insertSql, connection))
{
command.Parameters.AddWithValue("@Name", gvTbFormName);
command.Parameters.AddWithValue("@Description", gvTbFormDescription);
command.Parameters.AddWithValue("@EmpNo", "0000000001");
// 打开数据库连接并执行插入操作
connection.Open();
int rowsAffected = command.ExecuteNonQuery();
connection.Close();
// 检查是否成功插入数据
if (rowsAffected > 0)
{
// 插入成功的处理逻辑
initialControls();
}
else
{
// 插入失败的处理逻辑
}
}
}
}
protected void btnCancelSave_Click(object sender, EventArgs e)
{
GridViewFormList.FooterRow.Visible = false;
}
#region
/// <summary>
/// 資料庫查詢
/// </summary>
/// <param name="query"></param>
/// <returns></returns>
private DataTable ExecuteQuery(string query)
{
DataTable dataTable = new DataTable();
using (SqlConnection connection = new SqlConnection(connectionString))
{
try
{
connection.Open();
using (SqlCommand command = new SqlCommand(query, connection))
{
using (SqlDataAdapter adapter = new SqlDataAdapter(command))
{
adapter.Fill(dataTable);
}
}
}
catch (SqlException ex)
{
// 可以在此处处理异常
Console.WriteLine($"数据库操作错误:{ex.Message}");
}
}
return dataTable;
}
#endregion
}
}