944,093 Members | Top Members by Rank

Ad:
  • ASP.NET Discussion Thread
  • Unsolved
  • Views: 122703
  • ASP.NET RSS
Mar 30th, 2005
0

ASP.NET / C# Dynamic Control

Expand Post »
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?

ASP.NET Syntax (Toggle Plain Text)
  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?
Similar Threads
Team Colleague
Reputation Points: 227
Solved Threads: 37
Made Her Cry
tgreer is offline Offline
1,697 posts
since Dec 2004
Apr 4th, 2005
0

Re: ASP.NET / C# Dynamic Control

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?
Team Colleague
Reputation Points: 211
Solved Threads: 27
Master Poster
Paladine is offline Offline
793 posts
since Feb 2003
Apr 4th, 2005
0

Re: ASP.NET / C# Dynamic Control

Quote 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.
Team Colleague
Reputation Points: 227
Solved Threads: 37
Made Her Cry
tgreer is offline Offline
1,697 posts
since Dec 2004
Apr 8th, 2005
0

Re: ASP.NET / C# Dynamic Control

This little problem made me delve deeply into the issues. The results of my investigation:

http://www.tgreer.com/aspnet_html_04.html
Team Colleague
Reputation Points: 227
Solved Threads: 37
Made Her Cry
tgreer is offline Offline
1,697 posts
since Dec 2004
May 18th, 2005
0

Re: ASP.NET / C# Dynamic Control

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.



ASP.NET Syntax (Toggle Plain Text)
  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. }
Reputation Points: 10
Solved Threads: 0
Newbie Poster
sam_dlg is offline Offline
1 posts
since May 2005
Apr 1st, 2010
0
Re: ASP.NET / C# Dynamic Control
use <a href> with Eval() function instead of commandargument.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
bhavu4u is offline Offline
12 posts
since Mar 2010

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in ASP.NET Forum Timeline: Access denied error using Activex object
Next Thread in ASP.NET Forum Timeline: Help needed with radio button dynamic checked_Changed event





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC