Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Andrzejek on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dynamic controls

Status
Not open for further replies.

fmt

Programmer
Aug 5, 2005
53
US
Hello,
I have created few dynamic text controls and I have a button which becomes visible when these controls are created. After I enter some text and press button, my text in dynamic text boxes is diappearing.
Please advice
Thanks
 
Lots of potential issues. Make sure that every code-path that needs the dynamic controls, creates the dynamic controls. Make sure that you're creating the dynamic controls at the appropriate point in the ASP.NET Page Life-cycle. More information about dynamic controls and the page cycle:




Thomas D. Greer
 
Thanks tgreer. Here is my code. I think I tried everything.

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

namespace DeviceAccess
{

public class GetInfo : System.Web.UI.Page

{
public string device;
protected System.Web.UI.WebControls.TextBox txtName;
protected System.Web.UI.WebControls.Label lblManager;
protected System.Web.UI.WebControls.TextBox txtmanager;
protected System.Web.UI.WebControls.Label lblitem;
protected System.Web.UI.WebControls.DropDownList dditem;
protected System.Web.UI.WebControls.TextBox txtCompany;
protected System.Web.UI.WebControls.Label lblCompany;
protected System.Web.UI.WebControls.TextBox txtDepartment;
protected System.Web.UI.WebControls.Label lblDepartment;
protected System.Web.UI.WebControls.Button btnPrint;
protected System.Web.UI.WebControls.Label lblName;
public System.Data.SqlClient.SqlConnection dataConn;
protected System.Data.SqlClient.SqlCommand deviceCmd;
protected System.Data.SqlClient.SqlParameter deviceParm;
protected System.Web.UI.WebControls.Table Table1;
protected System.Web.UI.WebControls.Table tblcontrols;
protected System.Web.UI.WebControls.PlaceHolder phControl;
protected System.Data.SqlClient.SqlDataReader deviceReader;




#region PageLoad

private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
try
{
if (IsPostBack)
{
if (ViewState["mode"].ToString() == "1")
{
CreateComputerControls();
}
}
else
{
ViewState.Add("mode","0");
}

}
catch(Exception X)
{
//Redirect to different page

}


}
#endregion

#region Load View state
protected override void LoadViewState(object savedState)
{
base.LoadViewState(savedState);
if( ViewState["mode"].ToString() == "1")
{
CreateComputerControls();
}
}
#endregion VLoad View State

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{

InitializeComponent();
base.OnInit(e);
PlaceHolder phControl =new PlaceHolder();
phControl.Visible=false;
btnPrint.Visible=false;


}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>

private void InitializeComponent()
{
this.dditem.SelectedIndexChanged += new System.EventHandler(this.dditem_SelectedIndexChanged);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

#region SelectedIndexChanged
private void dditem_SelectedIndexChanged(object sender, System.EventArgs e)
{
device=dditem.SelectedItem.Value;
if (device=="Computer")
CreateComputerControls();

}

#endregion

#region CreateComputerControls
private void CreateComputerControls()
{
if (phControl.Controls.Count>0) phControl.Controls.Clear();
SqlConnection dataConn= new SqlConnection();
dataConn.ConnectionString="xxx";
dataConn.Open();

SqlCommand deviceCmd = new SqlCommand("p_NADA_GetControls",dataConn);
deviceCmd.CommandType=CommandType.StoredProcedure;
SqlParameter deviceParm = deviceCmd.Parameters.Add("@Device", SqlDbType.NVarChar, 25);
deviceParm.Value = device;

SqlDataReader DeviceReader = deviceCmd.ExecuteReader();
phControl.Visible=true;
phControl.Controls.Add(new LiteralControl("<table>"));
while (DeviceReader.Read())
{
string ctype = (string)DeviceReader["ControlType"];


if (ctype=="Label")

{
Label lblLabel= new Label();
lblLabel.Text=DeviceReader["ControlValue"].ToString();;
lblLabel.Font.Bold=true;
lblLabel.Font.Name="Arial";
lblLabel.Font.Size=System.Web.UI.WebControls.FontUnit.XSmall;
lblLabel.ForeColor=System.Drawing.Color.DarkBlue;

phControl.Controls.Add(new LiteralControl("<tr>"));
phControl.Controls.Add(new LiteralControl("<td class='label'>"));
phControl.Controls.Add(lblLabel);
phControl.Controls.Add(new LiteralControl("</td>"));
}

else if (ctype=="TextBox")
{

TextBox txtTextBox= new TextBox();
txtTextBox.ID = DeviceReader["ControlName"].ToString();
txtTextBox.EnableViewState=true;
btnPrint.Visible=true;

phControl.Controls.Add(new LiteralControl("<td class='TextBox'>"));
phControl.Controls.Add(txtTextBox);
phControl.Controls.Add(new LiteralControl("</td>"));


}


}

phControl.Controls.Add(new LiteralControl("</tr>"));
phControl.Controls.Add(new LiteralControl("</table>"));
ViewState.Add("mode","1");

DeviceReader.Close();
dataConn.Close();
}
#endregion createcontrols

#region PrintClick
private void btnPrint_submit(object sender, System.EventArgs e)
{
ViewState.Add("mode","1");
}
#endregion






}
}
 
Try calling the CreateComputerControls() function in the Page_Init event instead of Page_Load.

Jim
 
Directly after initialization, but only during a PostBack. Since you're creating your controls in Page_Load, they have no Viewstate.

You can override LoadViewState, as my article suggested, and create your controls there, or even in Page_Init, as jbenson001 suggested.



Thomas D. Greer
 
Thanks for your quick reply.As in my code above, when I select some thing from dropdownlist, it hits pageload but returns error object referance not set to an instance of variable.
I think may be its due to my setting viewstate mode to 1 doesnt occur untill I load controls. But how do I check post back?
Thanks again
 
The Page_Init is the best solution if your process is not dependent on user input. You can't read or write values to your other controls until page_load. So you can't ask if checkbox is checked then add control dynamic control.

If you have unlimited number of controls you may be adding then this could get complicated.
If the max number may be 10 then add 10 controls and set there visible to False then turn them on as needed.
 
fmt... that article I linked to directly addresses issues with ViewState, PostBack, and dynamic controls. If there is an error or something missing, let me know. I think, though, that your question has been fully answered!

Thomas D. Greer
 
Yes,Thanks.Its very good article .I have it as referance for my current project. I am new to asp.net and I am going through pitfalls. I resolved the issue of viewstate but I am still having problems retrieving values from dynamic text boxes. Any suggestions will be greatly appreciated as ever.
THANKS
 
If the control is created PRIOR TO the LoadPostBackData stage, then there should be no problems retrieving the data from the controls during the Load stage.

As a test, try iterating through Request.Form to make sure the data is really even being collected and submitted.



Thomas D. Greer
 
Thanks tgreer. I sure will try.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top