0

我在处理 Detailsview InsertItemTemplate 时遇到问题。我正在关注 WROX 的 ASP.Net 4 for Beginners。

我有一个带有表 Employee(EmpId, Name, DoB, Sex, DeptId, Position) 的 dbase 我正在尝试使用 Detailsview 将值插入到该表中。

使用常规的详细信息视图,我可以插入所有值。它工作正常。但是当我将详细信息视图转换为模板时,出现了问题。

我做了什么?

- 在 Detailsview 的 Smart 标签上,我点击了 Edit Fields。删除除 EmpID 之外的所有可用字段,并从可用字段中添加模板字段。- 再次在 Detailsview 的 Smart Tag 上,单击 Edit Template 并在 DropDownList 中选择 InsertItemTemplate。- 使用工具箱中的标准组件,我为名称添加了文本框,并为其他字段添加了下拉列表。(我没有删除 EmpId,所以我不需要它)。- 这是标记视图的其余部分:

输入以下信息进行注册: 姓名:  
出生日期:      
性别:
部门编号:
职位:

- 这是用于详细信息视图的插入事件的 CS 文件背后的代码

protected void DetailsView1_ItemInserting1(object sender, DetailsViewInsertEventArgs e) { string id = System.Guid.NewGuid().ToString(); 文本框 txt_name = (TextBox)DetailsView1.FindControl("EmpId"); e.Values["EmpId"] = id;

    DropDownList ddl_month = (DropDownList)DetailsView1.FindControl("DDL_Month");
    string month = ddl_month.SelectedValue;
    e.Values["DDL_Month"] = month;

    DropDownList ddl_day = (DropDownList)DetailsView1.FindControl("DDL_Day");
    string day = ddl_day.SelectedValue;
    e.Values["DDL_Day"] = day;

    DropDownList ddl_year = (DropDownList)DetailsView1.FindControl("DDL_Year");
    string year = ddl_year.SelectedValue;
    e.Values["DDL_Year"] = year;

    DropDownList ddl_sex = (DropDownList)DetailsView1.FindControl("DDL_Sex");
    string sex = ddl_sex.SelectedValue;
    e.Values["DDL_Sex"] = sex;

    DropDownList ddl_pos = (DropDownList)DetailsView1.FindControl("DDL_Pos");
    string pos = ddl_pos.SelectedValue;
    e.Values["DDL_Pos"] = pos;

    DropDownList ddl_deptid = (DropDownList)DetailsView1.FindControl("DDL_DeptId");
    string deptid = ddl_deptid.SelectedValue;
    e.Values["DDL_DeptId"] = deptid;
}

-您能否建议我如何将下拉列表中的三个选定值组合为出生日期并将其添加到数据库中。

谢谢!!!

4

2 回答 2

1

To elaborate:

Yes that would be the first step. The connection string should be added to your web.config under the '' element:

http://msdn.microsoft.com/en-us/library/bf7sd233.aspx

You can then read this out in your code:

Read connection string from web.config

The following shows a reasonably good general form for doing a database update into a SQL database based on values you're taking from a form - done in C# (which I'd recommend getting started with instead of VB.NET, for various good reasons)

Update database with values from textbox

This shows how to add the connection string directly inside the code instead of from the web.config, which may be simpler for you in the first instance. This shows how to do the database update by using 'parameters' in the query (the 'variables' if you like that are denoted with @ symbols). Using parameters is basic good practise that it's worth getting used to (don't worry too much about why, but you may want to research the 'SQL injection' technique that hackers use to attack websites). This is a good starting point for you. It may seem complex at first, but ask any developer with a few years of experience and they'll tell you it's generally the way to go. As you progress in development the tempting 'out of the box' stuff provided by .NET becomes increasingly limiting, and you lose the ability to finely tune performance and other characteristics of good applications.

The example I've given is actually still relative simple. As you develop, there are other aspects you should start bringing into your code, especially creating methods that do the bit that accesses the database (by using a SQL query) in a separate class that gets called a 'Data Access Layer' (often an intermediate layer gets used called the 'Business Logic Layer' or similar, but don't worry about that yet).

Also the example I've given should really be packaging up some of the code in methods that give a clear indication of what the code is doing e.g. at a simpler level a method 'UpdateMachineDetails' could at least be used to encapsulate the code that updates the database.

There are lots of tutorials if you Google for it that show you how to update a datbase 'manually' in .NET, but the form I'm giving you puts you on a good track towards best practice that is used industry-wide by software developers.

于 2012-07-31T21:29:41.400 回答
1

使用内置的 .NET 控件,如果您尝试删除已生成的字段,通常会遇到问题。让事情以这种方式工作的最佳选择是完全删除并重新添加您正在使用的控件,然后尝试更改您尝试使用模板的字段。你可能仍然有一些问题要让它工作,但应该不会太难。

以这种方式使用控件往往不够灵活,并且很难解决问题。这就是为什么大多数有经验的开发人员放弃这种“简单”的做事方式,并通过创建表单、读取值并将其传递给将执行相关 SQL 更新的方法来手动更新数据库。学习需要一些时间,但一旦掌握了窍门,它就会变得很容易,并解决了很多困难,试图解决您需要使用控件的非常特定的 .NET 方式。

于 2012-07-30T21:40:07.927 回答