• .NET Conf China
...

怎样隐藏动态生成的表格中的列

fish_killer 2008-06-03 11:09:00
具体需求是这样的:

一个表格,表格中的textbox,label等控件都放置在一个htmltbale内,表格的一半是静态的,另一半是由代码动态生成的。
列1 列2 列3 列4
item名称 控件(textbox) 显示%(label) 详细介绍item

因为有设定了不同的访问等级,要求向访问等级不够的用户隐藏htmltable的列3和列4.
想求教一个思路怎样实现!!谢谢!

静态表单中的列隐藏已经实现,生成表格的代码如下:


  1. private void generateForm()
  2. {
  3. //get all row description from table
  4. SqlConnection sqlconn = AllStoredProcedures.sqlconnSmartReportApp();
  5. DataSet formDesription = AllStoredProcedures.ExecuteQuery("sp_GetPLForm","isLoad", SqlDbType.Int,1,"1",sqlconn.ConnectionString);
  6. string tbArray = "['txt1','2','1'],['txt4','1','4'],['txt5','1','5'],";
  7. string lblPArray = "['LblpLessCostSales','1'],['LblpGrossMargin','2'],['LblpLabour','4'],['LblpOccCosts','5'],";
  8. string lblCArray = "['LblcGrossMargin','2'],";
  9. //Build the form
  10. ContentPlaceHolder box = Master.FindControl("ContentPlaceHolder1") as ContentPlaceHolder;
  11. //Build the rows
  12. for (int i = 0; i < formDesription.Tables[0].Rows.Count; i++)
  13. {
  14. Label lbl = new Label();
  15. TextBox txt = new TextBox();
  16. Label calField = new Label();
  17. Label calPercentage = new Label();
  18. HtmlTableCell txtCell = new HtmlTableCell();
  19. HtmlTableCell lblCell = new HtmlTableCell();
  20. HtmlTableCell calcuCell = new HtmlTableCell();
  21. HtmlTableCell percentCell = new HtmlTableCell();
  22. HtmlTableRow row = new HtmlTableRow();
  23. //RequiredFieldValidator reqValid=new RequiredFieldValidator();
  24. RegularExpressionValidator expValid = new RegularExpressionValidator();
  25. string description = formDesription.Tables[0].Rows[i].ItemArray[1].ToString();
  26. int iscalculate = Int32.Parse(formDesription.Tables[0].Rows[i].ItemArray[2].ToString());
  27. int descriptionID = Int32.Parse(formDesription.Tables[0].Rows[i].ItemArray[0].ToString());
  28. string direct = formDesription.Tables[0].Rows[i].ItemArray[4].ToString();
  29. string id = formDesription.Tables[0].Rows[i].ItemArray[3].ToString();
  30. if (id == "") { id = "null"; }
  31. switch (iscalculate)
  32. {
  33. //show the row contain input field and calculate field
  34. case (0):
  35. {
  36. //add label to description
  37. lbl.Text = description;
  38. lbl.ID = "lbl" + id;
  39. lblCell.Controls.Add(lbl);

  40. //add textbox for input data
  41. txt.ID = "txt" + descriptionID.ToString();
  42. txt.Attributes.Add("onblur", "doCal();");
  43. txtCell.Controls.Add(txt);
  44. expValid.ErrorMessage = "Number Only!";
  45. expValid.ControlToValidate = txt.ID;
  46. expValid.ValidationExpression = "^([.]|[0-9])*[.]*[0-9]+$";
  47. txtCell.Controls.Add(expValid);
  48. //add id to javascript array string
  49. tbArray += "['" + txt.ID + "','" + direct + "','" + descriptionID + "'],";

  50. //add label to show percentage calculate field
  51. //calcuCell.Controls.Add(calField);
  52. calPercentage.ID = "LblP" + id;
  53. //calPercentage.Text =
  54. lblPArray += "['" + calPercentage.ID + "','" + descriptionID + "'],";
  55. percentCell.Controls.Add(calPercentage);

  56. row.Cells.Add(lblCell);
  57. row.Cells.Add(txtCell);
  58. row.Cells.Add(percentCell);
  59. main.Rows.Add(row);
  60. break;
  61. }
  62. //show the row only contain calculate field
  63. case (1):
  64. {
  65. //add label to description
  66. lbl.Text = description;
  67. lbl.ID = "lbl" + id;
  68. lblCell.Controls.Add(lbl);
  69. //add label to show calculate field
  70. calField.ID = "LblC" + id;
  71. calcuCell.Controls.Add(calField);
  72. lblCArray += "['" + calField.ID + "','" + descriptionID + "'],";

  73. calPercentage.ID = "lblP" + id;
  74. percentCell.Controls.Add(calPercentage);
  75. lblPArray += "['" + calPercentage.ID + "','" + descriptionID + "'],";
  76. //add cells into row
  77. row.Cells.Add(lblCell);
  78. row.Cells.Add(calcuCell);
  79. row.Cells.Add(percentCell);
  80. main.Rows.Add(row);
  81. break;
  82. }
  83. //show the row only conatin text
  84. case (2):
  85. {
  86. lbl.Text = description;
  87. lbl.ID = "lbl" + id;
  88. lblCell.Controls.Add(lbl);
  89. row.Cells.Add(lblCell);
  90. main.Rows.Add(row);
  91. break;
  92. }
  93. }

  94. }//make javascript array
  95. tbArray += "[0,'','']";
  96. lblCArray += "[0,'']";
  97. lblPArray += "[0,'']";

  98. Page.ClientScript.RegisterStartupScript(this.GetType(), "", " var tbarr = [" + tbArray + "];var lblCarr = [" + lblCArray + "];var lblParr = [" + lblPArray + "]", true);
  99. }




...全文
给本帖投票
215 1 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
烟波钓 2011-09-30
  • 打赏
  • 举报
回复
生成表格的时候 给你需要操作的<tr><td>添加runat="Server"属性,并且添加Id属性,在后台去寻找,找到后进行具体的操作

62,243

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧

手机看
关注公众号

关注公众号

客服 返回
顶部