当前位置:   article > 正文

C#动态控件生成与数据修改_c# 动态生成控件

c# 动态生成控件

环境:.net framework4.7

需求:在前端界面显示table,用于展示数据库查询数据,但是第一列为按钮控件,用于对行数据进行编辑、提交修改、删除等操作。即table、tablerow【1】、tablecell【1】为按钮控件,其余列为数据;

实现:1、使用循环数据表获取行数/限定行数(限定行数时可以使用上下翻页按钮+文本控件记录翻页数值,再提交到C#进行拼接查询);2、在循环内创建按钮对象(后台按钮或前台按钮);3、使用合适的控件保存数据值用于展示(readonly=true禁用编辑)、点击修改按钮时:修改(readonly=false启用编辑)、点击提交时将修改后的数据同步到数据库

 

相关技术点:1、动态生成按钮的委托事件绑定,2、动态切换文本控件的编辑与禁用,3、前后端操作交互

 在网上找了很多如何动态绑定委托事件,但是版本不支持invoke

代码:C#+HTML+JavaScript

  1. using System;
  2. using System.Web.UI;
  3. using System.Web.UI.WebControls;
  4. using System.Windows.Forms;
  5. public partial class _Default : System.Web.UI.Page
  6. {
  7. protected void Page_Load(object sender, EventArgs e)
  8. {
  9. //行数,交给前端
  10. tablerowscount.Text = "8";
  11. tablerowscount.Enabled = false;
  12. //可以使用前置条件判断是否执行添加行操作
  13. if (showtable.Rows.Count == 1) { //当表格内没有数据时添加行
  14. AddRows();
  15. }
  16. }
  17. //动态生成函数
  18. private void AddRows() {
  19. for (int i = 0; i < 8; i++)
  20. {
  21. //行
  22. TableRow row = new TableRow();
  23. if (i % 2 == 1)
  24. {
  25. row.BackColor = System.Drawing.Color.AliceBlue; //行背景色,为系统预设值
  26. }
  27. else {
  28. row.BackColor = System.Drawing.Color.White;
  29. }
  30. //列
  31. TableCell cell = new TableCell();
  32. //按钮控件
  33. System.Web.UI.WebControls.Button button1 = new System.Web.UI.WebControls.Button();
  34. System.Web.UI.WebControls.Button button2 = new System.Web.UI.WebControls.Button();
  35. //关键,将控件添到页面控件集中,注册为服务器控件,防止加载刷新时动态生成的控件属性丢失
  36. button1.Attributes.Add("runat", "server");
  37. button2.Attributes.Add("runat", "server");
  38. this.Controls.Add(button1);
  39. this.Controls.Add(button2);
  40. //按钮控件属性
  41. button1.ID = "buttonA" + i.ToString();//使用数据行号对控件对象进行统一管理,方便后续定位
  42. button2.ID = "buttonB" + i.ToString();
  43. button1.CssClass = "buttonA";
  44. button2.CssClass = "buttonB";
  45. button1.Text = "删除";
  46. button2.Text = "提交";
  47. button1.Click += new EventHandler(this.button1_click);//绑定委托事件
  48. button2.Click += new EventHandler(this.button2_click);
  49. //删除、提交按钮,涉及到与后端数据进行交互的操作,使用后台控件
  50. cell.Controls.Add(button1);
  51. cell.Controls.Add(button2);
  52. //前台按钮控件,不涉及对后台数据进行操作,用于前台动态切换
  53. cell.Controls.Add(new LiteralControl("&nbsp;<input type='button' id='buttonC" + i.ToString() + "' class='button3' value='修改' onclick='xiugai(this.id)'/><input type='button' id='buttonD" + i.ToString() + "' class='button4' value='取消' onclick='quxiao(this.id)'/>"));
  54. row.Cells.Add(cell);//当前行添加按钮控件列1
  55. for (int j = 0; j < 3; j++)
  56. { //数据3列
  57. //列
  58. System.Web.UI.WebControls.TableCell cella = new System.Web.UI.WebControls.TableCell();
  59. //文本框控件
  60. System.Web.UI.WebControls.TextBox textbox = new System.Web.UI.WebControls.TextBox();
  61. //文本框控件属性
  62. textbox.ID = "textbox" + j.ToString() + i.ToString();
  63. textbox.CssClass = "textbox";
  64. textbox.Text = "数据第" + i.ToString() + "行,第" + j.ToString() + "列";
  65. textbox.BorderWidth = 0;
  66. if (i % 2 == 1)
  67. {
  68. textbox.BackColor = System.Drawing.Color.AliceBlue; //间隔,背景色,为系统预设值
  69. }
  70. else
  71. {
  72. textbox.BackColor = System.Drawing.Color.White;
  73. }
  74. cella.Controls.Add(textbox);//列添加控件对象
  75. row.Cells.Add(cella);//向行添加列对象
  76. }
  77. showtable.Rows.Add(row);//表添加行对象
  78. }
  79. }
  80. //按钮交互操作
  81. private void button1_click(object sender, EventArgs e) {
  82. MessageBox.Show("点击了删除按钮");
  83. }
  84. private void button2_click(object sender, EventArgs e) {
  85. MessageBox.Show("点击了提交按钮");
  86. }
  87. }
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
  2. <!DOCTYPE html>
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4. <head runat="server">
  5. <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
  6. <title></title>
  7. <script type="text/javascript">
  8. window.onload = function () {
  9. var rowscount = Number(document.getElementById("tablerowscount").value)
  10. for (var i = 0; i < rowscount; i++) {
  11. var tijiao = "buttonB" + String(i)
  12. var quxiao = "buttonD" + String(i)
  13. document.getElementById(tijiao).style.display = "none"
  14. document.getElementById(quxiao).style.display = "none"//设置按钮不可见
  15. for (var j = 0; j < 3; j++) {
  16. var textbox = "textbox" + String(j) + String(i)
  17. document.getElementById(textbox).readOnly=true //设置文本框控件只读
  18. }
  19. }
  20. }
  21. //启用编辑
  22. function xiugai(id) {
  23. var nid = id.replace("buttonC", "")
  24. for (var i = 0; i < 3; i++) {
  25. var textbox = "textbox" + String(i) + nid
  26. document.getElementById(textbox).readOnly = false //设置指定行的文本框可编辑
  27. document.getElementById(textbox).style.borderWidth = "2px"
  28. }
  29. //交替显示按钮控件
  30. document.getElementById("buttonA" + nid).style.display = "none"
  31. document.getElementById("buttonC" + nid).style.display = "none"
  32. document.getElementById("buttonB" + nid).style.display = "inline"
  33. document.getElementById("buttonD" + nid).style.display = "inline"
  34. }
  35. //退出编辑
  36. function quxiao(id) {
  37. var nid = id.replace("buttonD", "")
  38. for (var i = 0; i < 3; i++) {
  39. var textbox = "textbox" + String(i) + nid
  40. document.getElementById(textbox).readOnly = true //设置指定行的文本框不可编辑
  41. document.getElementById(textbox).style.borderWidth = "0px"
  42. }
  43. //交替显示按钮控件
  44. document.getElementById("buttonA" + nid).style.display = "inline"
  45. document.getElementById("buttonC" + nid).style.display = "inline"
  46. document.getElementById("buttonB" + nid).style.display = "none"
  47. document.getElementById("buttonD" + nid).style.display = "none"
  48. }
  49. </script>
  50. </head >
  51. <body bgColor ="#e9f4ff">
  52. <form id="form1" runat="server">
  53. <div>
  54. <asp:textbox runat="server" ID="tablerowscount"></asp:textbox>
  55. </div>
  56. <asp:table runat="server" ID="showtable" CellPadding="5" GridLines="Both" HorizontalAlign="Center" ForeColor="Black">
  57. <asp:tablerow runat="server">
  58. <asp:TableHeaderCell runat="server">操作</asp:TableHeaderCell>
  59. <asp:TableHeaderCell runat="server">数据列1</asp:TableHeaderCell>
  60. <asp:TableHeaderCell runat="server">数据列2</asp:TableHeaderCell>
  61. <asp:TableHeaderCell runat="server">数据列3</asp:TableHeaderCell>
  62. </asp:tablerow>
  63. </asp:table>
  64. </form>
  65. </body>
  66. </html>

当然,也可以按照不同需求声明不同的控件,只需要将控件注册到controls里

c#初学,欢迎留言

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/2023面试高手/article/detail/419019
推荐阅读
相关标签
  

闽ICP备14008679号