ASP.NET / C# Dynamic Control

Please support our ASP.NET advertiser: Intel Parallel Studio Home
Reply

Join Date: Dec 2004
Posts: 1,655
Reputation: tgreer is an unknown quantity at this point 
Solved Threads: 35
Team Colleague
tgreer tgreer is offline Offline
Made Her Cry

ASP.NET / C# Dynamic Control

 
0
  #1
Mar 30th, 2005
I'm having a problem with a dynamic LinkButton.

For a result returned from a database query, I create two LinkButtons, dynamically. One is "Update" and the other is "Delete".

I store Update or Delete in the .CommandName property. I store the identity value (the primary key) of the database record in the .CommandArgument property.

All of this is in a method that is called when a user clicks a particular record displayed from a previous operation. Still with me?

  1. public void createUpDelPaper(int xPaperID, string xPaperTitle)
  2. {
  3.  
  4. PlaceHolder1.Controls.Add(new LiteralControl("<fieldset>"));
  5. PlaceHolder1.Controls.Add(new LiteralControl("<legend>Update/Delete Paper</legend>"));
  6. PlaceHolder1.Controls.Add(new LiteralControl("<label for='title'>Title:</label>"));
  7.  
  8. txtNewPaper = new TextBox();
  9. txtNewPaper.Columns = 45;
  10. txtNewPaper.ID = "title";
  11. txtNewPaper.CssClass = "input-box";
  12. txtNewPaper.Text = xPaperTitle;
  13. txtNewPaper.TextMode = System.Web.UI.WebControls.TextBoxMode.SingleLine;
  14. txtNewPaper.Attributes.Add("onkeypress","enterSubmit(this,event)");
  15. PlaceHolder1.Controls.Add(txtNewPaper);
  16.  
  17. lnkAddPaper = new LinkButton();
  18. lnkAddPaper.CommandName = "UpdatePaper";
  19. lnkAddPaper.CssClass = "submit-button";
  20. lnkAddPaper.Text = "Update";
  21. lnkAddPaper.ID = "Update";
  22. lnkAddPaper.Command += new System.Web.UI.WebControls.CommandEventHandler(paperTitle_Click);
  23. PlaceHolder1.Controls.Add(lnkAddPaper);
  24.  
  25. lnkAddPaper = new LinkButton();
  26. lnkAddPaper.CommandName = "DeletePaper";
  27. lnkAddPaper.CssClass = "submit-button";
  28. lnkAddPaper.Text = "Delete";
  29. lnkAddPaper.ID = "Delete";
  30. lnkAddPaper.Command += new System.Web.UI.WebControls.CommandEventHandler(paperTitle_Click);
  31. PlaceHolder1.Controls.Add(lnkAddPaper);
  32.  
  33. PlaceHolder1.Controls.Add(new LiteralControl("</fieldset>"));
  34. ViewState.Add("ManagePapers","2");
  35. }

Now, I do know about dynamic controls. The next time the page loads, these controls have to be recreated, and then the ViewState mechanism ties these "new" controls to their delegates for event processing.

All well and good. But when I recreate these controls, I cannot recreate the .CommandArgument. It's a chicken-and-egg thing. I store the primary key value in the .CommandArgument so that I know which record to update or delete. But the value of .CommandArgument is set conditionally, and that condition doesn't exist on PostBack.

How can I make the value persist?

One answer: don't store it in .CommandArgument, put it in a ViewState variable.

But this process has revealed a gap in my knowledge in how to deal with dynamically created controls. Any insights?
Reply With Quote Quick reply to this message  
Join Date: Feb 2003
Posts: 793
Reputation: Paladine has a spectacular aura about Paladine has a spectacular aura about Paladine has a spectacular aura about 
Solved Threads: 27
Team Colleague
Paladine's Avatar
Paladine Paladine is offline Offline
Master Poster

Re: ASP.NET / C# Dynamic Control

 
0
  #2
Apr 4th, 2005
It is late and I am a little tired so reading code is getting hard at this hour.... but why not store this variable value in a Session variable?
Assistant Manager, Pharmacy Informatics
Wordpress Learning Blog
Updated : ASP.Net Login Code
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 1,655
Reputation: tgreer is an unknown quantity at this point 
Solved Threads: 35
Team Colleague
tgreer tgreer is offline Offline
Made Her Cry

Re: ASP.NET / C# Dynamic Control

 
0
  #3
Apr 4th, 2005
Originally Posted by Paladine
It is late and I am a little tired so reading code is getting hard at this hour.... but why not store this variable value in a Session variable?
The ViewState mechanism is much better, for my purposes. I don't need the value to persist through the entire Session.

The real question is, why doesn't it persist through the Viewstate automatically?

If you create a dynamic textbox, conditionally, say in response to some other control's click event. Then, on PostBack, you recreate a "new" TextBox with the same object name, then "magically" that new textbox becomes the dynamically-created textbox. You get all of the user-initiated and code-generated properties.

What I've discovered is that, at least with ListButtons, is that the CommandArgument property doesn't act this way.
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 1,655
Reputation: tgreer is an unknown quantity at this point 
Solved Threads: 35
Team Colleague
tgreer tgreer is offline Offline
Made Her Cry

Re: ASP.NET / C# Dynamic Control

 
0
  #4
Apr 8th, 2005
This little problem made me delve deeply into the issues. The results of my investigation:

http://www.tgreer.com/aspnet_html_04.html
Reply With Quote Quick reply to this message  
Join Date: May 2005
Posts: 1
Reputation: sam_dlg is an unknown quantity at this point 
Solved Threads: 0
sam_dlg sam_dlg is offline Offline
Newbie Poster

Re: ASP.NET / C# Dynamic Control

 
0
  #5
May 18th, 2005
i'm doing the following and when i click the button to submit it ends up adding an additional textbox.

I have a calculator where you can dynamically add controls. And I'm able to add new textboxes for additional numbers and on that same page I have a "sum values" button that does just that. The problem is that when I do a sum value it pulls the view state again and creates a new textbox. That's because for some reason my viewstate is updated after the page is created.



  1. using System;
  2. using System.Collections;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Web;
  7. using System.Web.SessionState;
  8. using System.Web.UI;
  9. using System.Web.UI.WebControls;
  10. using System.Web.UI.HtmlControls;
  11.  
  12. namespace DynamicControlLoading
  13. {
  14. /// <summary>
  15. /// Summary description for WebForm1.
  16. /// </summary>
  17. public class WebForm1 : System.Web.UI.Page
  18. {
  19. protected System.Web.UI.WebControls.Button Button1;
  20. protected System.Web.UI.WebControls.Panel Panel1;
  21. protected System.Web.UI.WebControls.Button Button2;
  22.  
  23. public Int32 Count
  24. {
  25. get
  26. {
  27. if (ViewState["count"] == null)
  28. ViewState["count"] = 0;
  29. return Int32.Parse(ViewState["count"].ToString());
  30. }
  31. set
  32. {
  33. ViewState["count"] = value;
  34. }
  35. }
  36. private void Page_Load(object sender, System.EventArgs e)
  37. {
  38. if (IsPostBack)
  39. {
  40. if (ViewState["mode"].ToString() == "1")
  41. Response.Write("clicked");
  42. }
  43. else
  44. {
  45. ViewState.Add("mode", "0");
  46. Count = 0;
  47. }
  48. }
  49.  
  50. #region Web Form Designer generated code
  51.  
  52. override protected void OnInit(EventArgs e)
  53. {
  54. //
  55. // CODEGEN: This call is required by the ASP.NET Web Form Designer.
  56. //
  57. InitializeComponent();
  58. base.OnInit(e);
  59. }
  60.  
  61. /// <summary>
  62. /// Required method for Designer support - do not modify
  63. /// the contents of this method with the code editor.
  64. /// </summary>
  65. private void InitializeComponent()
  66. {
  67. this.Button1.Click += new System.EventHandler(this.Button1_Click);
  68. this.Button2.Click += new System.EventHandler(this.Button2_Click);
  69. this.Load += new System.EventHandler(this.Page_Load);
  70. }
  71.  
  72. protected override void LoadViewState(object savedState)
  73. {
  74. base.LoadViewState(savedState);
  75. if (ViewState["mode"].ToString() == "1")
  76. AddTextBox();
  77. }
  78. #endregion
  79.  
  80. private void Button2_Click(object sender, System.EventArgs e)
  81. {
  82. Count += 1;
  83. AddTextBox();
  84. ViewState.Add("mode", "1");
  85. }
  86.  
  87. private void AddTextBox()
  88. {
  89. for (int i = 1; i <= Count; i++)
  90. {
  91. TextBox txtBox = new TextBox();
  92. txtBox.ID = String.Format(
  93. "txtBox{0}", i);
  94. Panel1.Controls.Add(txtBox);
  95. }
  96. }
  97.  
  98. private void Button1_Click(object sender, System.EventArgs e)
  99. {
  100. int grandTotal = 0;
  101. /*
  102.   * for (int i = 1; i <= Count; i++)
  103.   {
  104.   grandTotal += int.Parse(Request.Form[
  105.   String.Format("txtBox{0}", i)]);
  106.   }
  107.   */
  108. for (int i = 1; i <= Count-1; i++)
  109. {
  110. TextBox txt = (TextBox) Panel1.FindControl(
  111. String.Format("txtBox{0}", i));
  112.  
  113. grandTotal += int.Parse(txt.Text.ToString());
  114. }
  115. Response.Write(grandTotal);
  116. }
  117. }
  118. }
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC