要求:从hr数据库info表读取数据,在Web网站中显示为如图:
用DW绘制一个表格,然后将代码拷贝到新建的网站主页代码中
1 <div> 2 3 <table bgcolor="black" border="0" cellpadding="5" cellspacing="1" width="100%"> 4 <tr> 5 <td align="center" bgcolor="#99CC66" width="10%"><strong>代号</strong></td> 6 <td align="center" bgcolor="#99CC66" width="20%"><strong>姓名</strong></td> 7 <td align="center" bgcolor="#99CC66" width="10%"><strong>性别</strong></td> 8 <td align="center" bgcolor="#99CC66" width="20%"><strong>民族</strong></td> 9 <td align="center" bgcolor="#99CC66" width="20%"><strong>生日</strong></td> 10 <td align="center" bgcolor="#99CC66" width="20%"><strong>操作</strong></td> 11 </tr> 12 <% 13 MyDBDataContext context = new MyDBDataContext(); 14 var query = context.Info; 15 foreach(Info data in query) 16 { 17 %> 18 <tr> 19 <td align="center" bgcolor="#CCFFFF"><%=data.Code %></td> 20 <td align="center" bgcolor="#CCFFFF"><%=data.Name %></td> 21 <td align="center" bgcolor="#CCFFFF"><%=(data.Sex.Value==true?"男":"女") %></td> 22 <td align="center" bgcolor="#CCFFFF"><%=data.Nation1.Name %></td> 23 <td align="center" bgcolor="#CCFFFF"><%=data.Birthday.Value.ToString("yyyy年MM月dd日") %></td> 24 <td align="center" bgcolor="#CCFFFF"><a href="Delete.aspx?aaa=<%=data.Code %>">删除</a></td> 25 </tr> 26 <% 27 } 28 %> 29 </table> 30 31 </div>
删除操作的代码:
1 <div> 2 <% 3 //获取传过来的主键值 4 string s = Request["aaa"].ToString(); 5 //执行删除 6 MyDBDataContext context = new MyDBDataContext(); 7 var q = context.Info.Where(p => p.Code == s); 8 if (q.Count() > 0) 9 { 10 Info data = q.First(); 11 12 context.Family.DeleteAllOnSubmit(data.Family); 13 context.Work.DeleteAllOnSubmit(data.Work); 14 context.Info.DeleteOnSubmit(data); 15 16 context.SubmitChanges(); 17 } 18 //跳回Default.aspx 19 Response.Redirect("Default.aspx"); 20 21 %> 22 </div>