将ASP.NET中的动态布局控件映射到Event上的DB条目 [英] Mapping Dynamically laid out controls in ASP.NET to DB entries on Event

查看:72
本文介绍了将ASP.NET中的动态布局控件映射到Event上的DB条目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在使用ASP.NET开发第一个Web应用程序.要求如下:

1.从数据库中读取某些行,并在页面上动态显示.
2.更新网页中的值,并在按钮单击事件中更新数据库.

控件是简单的文本框,并根据不同的用户操作动态创建.基本上,同一页面具有不同数量的文本框,具体取决于用户访问网站的方式.

我已经完成了动态填充部分,但是无法想到一种将控件的内容映射回相应的DB条目的方法.
我是一个初学者,因此任何帮助将不胜感激.

数据库表UserInfo中的样本数据:

1,1,名称,彼得
1,2,性别,男
1,3,UID,100
2,4,FirstName,Mark
2,5,姓氏,哈罗德
2,6,职业,工程师
2,7,Country,USA

如果Peter登录,则该页面仅呈现3个标签文本框对以显示Name,Gender,UID
如果Mark Harrold登录,该页面将呈现4个标签文本框对,以显示FirstName,LastName,Occupation,Country.
在任何给定的点上,我都有要更新其数据的用户的标识(1为Peter,2为Mark Harrold),但我无法找出将标签文本框对映射到第二个ID的方法(1 -3,4-7).

谢谢,
Hersh

Hi,

I am working on my first web application using ASP.NET. The requirement is as follows:

1. Read Certain rows from DB and dynamically display on the page.
2. Update the values in the web page and update the DB on a button click event.

The controls are simple text boxes and are created dynamically depending on different user actions. Basically, the same page has different number of text boxes based on which user is accessing the website.

I have completed the dynamic population part but not able to think of a way to map the contents of the controls back to the corresponding DB entries.
I am a beginner to this, so any help will be greatly appreciated.

Sample data in DB table UserInfo:

1,1,Name,Peter
1,2,Gender,Male
1,3,UID,100
2,4,FirstName,Mark
2,5,LastName,Harrold
2,6,Occupation,Engineer
2,7,Country,USA

If Peter logs in, the page renders only 3 label-textbox pairs to display Name,Gender,UID
If Mark Harrold logs in, the page renders 4 label-textbox pairs to display FirstName,LastName,Occupation,Country.
At any given point, I have the ID of the user whose data is being updated (1 for Peter, 2 for Mark Harrold) but I am not able to figure out a way to map the label-textbox pairs to the second ID (1-3, 4-7).

Thanks,
Hersh

推荐答案

就像这样:

http://www.aspfree.com /c/a/ASP.NET/Introduction-to-the-ADONET-Entity-Framework-using-ASPNET/ [
Like this:

http://www.aspfree.com/c/a/ASP.NET/Introduction-to-the-ADONET-Entity-Framework-using-ASPNET/[^]


什么在您的情况下,我会创建一个新列"UserType"或其他内容.否则,很难确定您需要更新哪些列.
然后我会做(如VB中的示例):

What I would do in your case is create a new column "UserType" or something. Otherwise, it is very hard to determine which columns you need to update.
Then I would do (as in the example in VB):

Dim ctx As New EmployeeEntities

'You said "I have the ID of the user whose data is being updated"
Dim IHaveTheUserId As Integer = 1

Dim oEmp As Emp = ctx.Emp.Where(Function(p) p.ID = IHaveTheUserId ).FirstOrDefault
If oEmp Is Nothing Then
  Me.lblMsg.Text = "Employee not found to update"
  Exit Sub
End If

If usertype = 1 Then
  oEmp.Name = Me.txtName.Text
  oEmp.Gender = Me.txtGender.Text
  oEmp.UID = Me.txtUID.Text
Else
  oEmp.FirstName = Me.txtFirstName.Text
  oEmp.LastName = Me.txtLastName.Text
  oEmp.Occupation = Me.txtOccupation.Text
  oEmp.Country = Me.txtCountry.Text
End if

ctx.SaveChanges()


如果我能找到这样的解决方案,我将把它扔掉并重新开始...
无论如何,在这种情况下,您可以使字段名与表中的列相同,然后询问控件名的值,然后在数据库中更新它.
If I would find a solution like that, I would throw it away and start again...
Anyway, in this case maybe you could make the fieldnames the same as the columns in the table, and then you could ask the value of the control name, and you would update that in the database.


这篇关于将ASP.NET中的动态布局控件映射到Event上的DB条目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆