C# append to placeholder - c#

So I am trying to make a dynamic form builder where people can add a new form and add fields to that form (the add field brings up several textboxes & dropdown options for that new field). Is there any way to append to a placeholder control when clicking 'add new field'? Also, what is the best way to get the values from those dynamically added controls?

A few simple steps...
1) Create a new instance of the control. Populate any desired properties.
2) Add it to the PlaceHolder using the PlaceHolder's .Controls.Add method.
3) Add the control's event handler. By using a delegate, as shown, you can access the control's values.
DropDownList ddl = new DropDownList();
ListItem li0 = new ListItem(string.Empty, "0");
ListItem li1 = new ListItem("Hello", "1");
ListItem li2 = new ListItem("World", "2");
ddl.Items.Add(li0);
ddl.Items.Add(li1);
ddl.Items.Add(li2);
ddl.AutoPostBack = true;
PlaceHolder1.Controls.Add(ddl);
ddl.SelectedIndexChanged += delegate(object snd, EventArgs evt) { DoSomething(ddl.SelectedValue); };
public void DoSomething(string SelectedValue)
{
//Do something spectacular here...
}

This is the other option (appending to a Literal Control). Each time the user clicks a button, a new field is created. I don't know how to add those dynamic fields to the database. Should I do a foreach?
protected void Button1_Click(object sender, EventArgs e)
{
var thisstring = new StringWriter();
var writer = new HtmlTextWriter(thisstring);
var formLabel = new TextBox();
formLabel.Text = idValue.ToString();
writer.Write("Field Label:");
formLabel.RenderControl(writer);
var typeOptions = new DropDownList();
typeOptions.DataSource = getfieldtypes();
typeOptions.DataTextField = "description";
typeOptions.DataValueField = "id";
typeOptions.DataBind();
writer.Write("Field Type:");
typeOptions.RenderControl(writer);
writer.WriteBreak();
Literal1.Text += thisstring;
}

Related

Change text of a TextBox clicking a Button with Controls created at run-time

I'm trying to change the text of a TextBox when I click a Button: both Controls are dynamically created as run-time.
The Buttons and the TextBoxes are created every time I click on another Button.
The Name Property for each control is specified by the User, using a TextBox.
For example, the user inputs "Test1", then the Button is named btn_Test1, and the TextBox is named txt_Test1.
The Button should open a FolderBrowserDialog and after a selection has been made, the TextBox shows the path selected.
I'm using the following code:
protected void button_Click(object sender, EventArgs e)
{
Button button = sender as Button;
folderBrowserDialog.ShowDialog();
string TextName = button.Name.Replace("btn_", "txt_");
TextBox selectText = new TextBox();
selectText = this.Controls[TextName] as TextBox;
selectText.Text = folderBrowserDialog.SelectedPath;
}
however this part gives me null:
selectText = this.Controls[TextName] as TextBox;
I did check with the debugger when I create the controls, so TextName is setting the correct Name.
The Buttons and TextBoxes are inserted in a TabControls, the Tab Name is set to the value the user inputs, so the main TabControl gets 2 controls.
I'm using a hidden TabControl named "TabFolders" that will be the main reference for creating tab clones
I'm using this code:
private void CreateDynamicPathButtons(string TabName)
{
TabPage MyNewTab = new TabPage(TabName);
TabPage TabCopy1;
tabControlEmpresas.TabPages.Add(MyNewTab);
TabControl tc = new TabControl();
tc.Location = new System.Drawing.Point(6, 6);
tc.Size = TabFolders.Size;
for (int i = 0; i < TabFolders.TabCount; i++) {
TabFolders.SelectTab(i);
TabCopy1 = new TabPage(TabFolders.SelectedTab.Text);
foreach (Control c in TabFolders.SelectedTab.Controls) {
Control cNew = (Control)Activator.CreateInstance(c.GetType());
cNew.Text = c.Text;
cNew.Size = c.Size;
cNew.Location = new System.Drawing.Point(c.Location.X, c.Location.Y);
cNew.Visible = true;
if (cNew is TextBox) {
cNew.Name = "txt_" + MyNewTab.Text + "_" + TabFolders.SelectedTab.Text;
}
if (cNew is Button) {
cNew.Name = "btn_" + MyNewTab.Text + "_" + TabFolders.SelectedTab.Text;
cNew.Click += new EventHandler(button_Click);
}
TabCopy1.Controls.Add(cNew);
}
tc.TabPages.Add(TabCopy1);
}
MyNewTab.Controls.Add(tc);
}
After many attempts I did find a very simple solution.
TextBox selectText = new TextBox();
selectText = button.Parent.Controls[TextName] as TextBox;
The button parent hast all the controls.
Assuming that button is the Button control you're creating at run-time you mentioned, you're creating a TextBox control but you're not adding it to the Form.Controls collection (this.Controls.Add([Control])).
Also, you should assign a Location, using a logic that fits your current Layout, to position the newly created Controls. Otherwise, all new controls will be positioned one on top of the other. In the example, the new Control position is determined using a field (int ControlsAdded) that keeps track of the number of Controls created at run-time and add some basic layout logic.
But, if you want to keep a reference of these new Controls, you should add them to a List<Control> or some other collection that allows to select them if/when required.
int ControlsAdded = 0;
protected void button_Click(object sender, EventArgs e)
{
TextBox selectedText = new TextBox();
selectedText.Size = new Size(300, this.Font.Height);
selectedText.Location = new Point(100, ControlsAdded * selectedText.Height + 30);
ControlsAdded += 1;
this.Controls.Add(selectedText);
selectedText.BringToFront();
using (var fBD = new FolderBrowserDialog()) {
if (fBD.ShowDialog() == DialogResult.OK)
selectedText.Text = fBD.SelectedPath;
}
}
with selectText = this.Controls[TextName] as TextBox;, you are trying to find button with replaced name which is not available in this case, and hence it returns null. This is logical mistake.
also string TextName = button.Name.Replace("btn_", "txt_"); does not replace button name, it just assigns replaced string to TextName.
The proper implementation would be
protected void button_Click(object sender, EventArgs e)
{
Button button = sender as Button;
folderBrowserDialog.ShowDialog();
button.Name = button.Name.Replace("btn_", "txt_");
TextBox selectText = new TextBox();
selectText = this.Controls[button.Name] as TextBox;
selectText.Text = folderBrowserDialog.SelectedPath;
}

How to create click event for dynamically created user controls in C#?

I'm creating a Windows Universal App which contains a ListView filled with User-Controls. User-Controls are added to ListView dynamically during runtime, based on the elements from the database.
public void ShowFavorites()
{
using (SQLite.Net.SQLiteConnection conn = new SQLite.Net.SQLiteConnection(new SQLite.Net.Platform.WinRT.SQLitePlatformWinRT(), (Application.Current as App).DBPath))
{
var Favorites = conn.Table<Favorites>();
lvFavorites.Items.Clear();
foreach (var fav in Favorites)
{
FavoriteItem favItem = new FavoriteItem();
favItem.Favorite = fav;
lvFavorites.Items.Add(favItem);
}
}
}
So how can i create an event that that triggers when the user-control is pressed?
When you create the control, all you need to do is simply link the control to a new event:
// Dynamically set the properties of the control
btn.Location = new Point((lbl.Width + cmb.Width + 17), 5);
btn.Size = new System.Drawing.Size(90, 23);
btn.Text = "Add to Table";
// Create the control
this.Controls.Add(btn);
// Link it to an Event
btn.Click += new EventHandler(btn_Click);
Then, when you (in this case) click on the newly added button, it will call your btn_Click method:
private void btn_Click(object sender, EventArgs e)
{
//do stuff...
}
I got it working even for items with diffrent text etc.
I gonna explain it based on a button beeing added to a panel.
You need to have a List<> in which you store the items, in this case buttons.
List<Button> BtList = new List<Button>();
and I also have a Panel in this case.
Panel PanelForButtons = new Panel();
Here is my code I hope it helps you:
void AddItemToPanel()
{
//Creating a new temporary item.
Button TempBt = new Button();
TempBt.Text = "Hello world!";
//Adding the button to our itemlist.
BtList.Add(TempBt);
//Adding the event to our button.
//Because the added item is always the last we use:
PanelForButtons.Controls.Add(BtList.Last());
BtList.Last().Click += MyButtonClicked;
}
And here is the event:
void MyButtonClicked(object sender, EventArgs e)
{
//First we need to convert our object to a button.
Button ClickedButton = sender as Button;
//And there we have our item.
//We can change the text for example:
ClickedButton.Text = "The world says: \"Hello!\"";
}

How to retain data for dynamically created controls?

I have the following situation:
I have 2 drop down lists (DDL) with auto-postback enabled.
When the user selects something from first DDL, he can choose stuff from 2nd DDL.
When the user selects something from 2nd DDL, I open database (WHERE clause of SQL query is filled with values from DDLs), and dynamically create several text boxes, labels and buttons which I place inside a placeholder.
The problem is: when I click on some of the dynamically created buttons, nothing happens, and I can't retrieve data from dynamically created boxes. Also, placeholder content is lost (all controls).
So my question is: how do I retain data from dynamically created controls on postback?
I have listed a lot of similar articles on this site, and none solves my problem. Another thing is - I can't use view state nor session.
Code is:
public Button btn1 = new Button();
public TextBox txtBoxC1 = new TextBox();
public TextBox txtBoxC2 = new TextBox();
public TextBox txtBoxC3 = new TextBox();
...
...
...
protected void ddl2_SelectedIndexChanged(object sender, EventArgs e)
{
...
...
if some conditions are met:
...
...
String CS4 = ConfigurationManager.ConnectionStrings["connStringName"].ConnectionString;
using (SqlConnection conn4 = new SqlConnection(CS4))
{
SqlCommand cmd4 = new SqlCommand("Select * from table where column1=" +
Convert.ToInt32(ddl2.SelectedValue)+" order by column1", conn4);
conn4.Open();
SqlDataReader rdr = cmd4.ExecuteReader();
while (rdr.Read())
{
txtBoxC1.Text = rdr["column1"].ToString(); txtBoxC1.MaxLength = 3; txtBoxC1.Columns = 3; txtBoxC1.ID = ddl2.SelectedValue + "1";
txtBoxC2.Text = rdr["column2"].ToString(); txtBoxC2.MaxLength = 3; txtBoxC2.Columns = 3; txtBoxC2.ID = ddl2.SelectedValue + "2";
txtBoxC3.Text = rdr["column3"].ToString(); txtBoxC3.MaxLength = 3; txtBoxC3.Columns = 3; txtBoxC3.ID = ddl2.SelectedValue + "3";
btn1.Text = "click me"; btn1.ID = ddl1.SelectedValue; btn1.Click += btn1_Click;
phDynamic.Controls.Add(btn1);
phDynamic.Controls.Add(txtBoxC1);
phDynamic.Controls.Add(txtBoxC2);
phDynamic.Controls.Add(txtBoxC3);
}
}
}
...
...
...
private void btn1_Click(object sender, EventArgs e)
{
Response.Write(txtBoxC1.Text+txtBoxC2.Text+txtBoxC3.Text);
}
So instead of getting back values from my 3 textboxes, I don't get back anything, and I also lose placeholder controls.

Using text from dynamically created TextBoxes

I've got a question regarding textboxes in C#. I've made a button that will create textboxes when clicked:
private void helloButton_Click(object sender, EventArgs e)
{
TextBox txtRun = new TextBox();
TextBox txtRun2 = new TextBox();
txtRun2.Name = "txtDynamic2" + c++;
txtRun.Name = "txtDynamic" + c++;
txtRun.Location = new System.Drawing.Point(40, 50 + (20 * c));
txtRun2.Location = new System.Drawing.Point(250, 50 + (20 * c));
txtRun2.ReadOnly = true;
txtRun.Size = new System.Drawing.Size(200, 25);
txtRun2.Size = new System.Drawing.Size(200, 25);
this.Controls.Add(txtRun);
this.Controls.Add(txtRun2);
}
How can I pull the text which the user types into these newly generated textboxes to use it as arguments for a different function (which will be called by a different button)? I'm quite new at this and could use the help.
Thanks in advance.
var matches = this.Controls.Find("txtDynamic2", true);
TextBox tx2 = matches[0] as TextBox;
string yourtext = tx2.Text;
This will return an array of controls by the name txtDynamic2, in your case the first one would be the control you are looking for unless you create more controls having the same name.
This will allow you to fully access the textbox if you found it.
var text = (TextBox)this.Controls.Find("txtDynamic2", true)[0];
If you'd like to use the instantiated textboxes in other methods then you can achieve that by either passing them to the method, or storing them as members of your class.
Example of storing them in your class below.
public class YourForm
{
private TextBox txtRun;
private TextBox txtRun2;
private void helloButton_Click(object sender, EventArgs e)
{
txtRun = new TextBox();
txtRun2 = new TextBox();
// removed less interesting initialization for readability
this.Controls.Add(txtRun);
this.Controls.Add(txtRun2);
}
public void DoStuffWithTextBoxes()
{
if (txtRun != null && txtRun2 != null)
{
// Retrieve text value and pass the values to another method
SomeOtherMagicMethod(txtRun.Text, txtRun2.Text);
}
}
private void SomeOtherMagicMethod(string txtRunText, string txtRun2Text)
{
// Do more magic
}
}
You can do it very easily:
//get the text from a control named "txtDynamic"
string text = this.Controls["txtDynamic"].Text;
Just remember to make sure that your controls have unique Name property, otherwise You'll get the text from the first control that's found with the specified name.

Dynamically Created Textboxes not updating

I'm creating textboxes in the Page_Load event and giving them values, however whenever I load the details I am getting the same output. I always seem to get the first output I got, even on subsequent searches.
Here's what my code with the irrelevant information missing:
Textbox txtName = New Textbox();
protected void Page_Load(object sender, EventArgs e)
{
LoadData();
}
void LoadData()
{
txtName.Text = DropDownList.SelectedValue;
tableCell.Controls.Add(txtName);
}
If DropDownList has two values (e.g. "Item 1" and "Item 2") and has autopostback enabled, first time it will generate and show "Item 1" in the textbox (the default value for the DropDownList), but if this is changed and the autopostback fires, the textbox does not change.
I've tried getting around this by creating a new TextBox, overriding the "LoadPostData" function to prevent this from loading, which got around the issue there, but then I couldn't view the data afterwards.
Any idea how I could get around this? I may be approaching this in the wrong way.
Edit: I've added another item to DropDownList that removes TextBox, so that it can be re-created again. It seems to show the correct data when it is re-created after being removed, but if I'm attempting to just edit it, this isn't updating correctly.
Edit2: Here's the rest of the code for this page in case this helps at all. The objects which I'm having issues with are SBUName and SBUComments, which are both TextBoxes. The same issue is also happening for SBUClientDropdown but I believe the resolution will be similar:
DBHandler DBHandler = new DBHandler();
List<string> clients = new List<string>();
List<string> clientGroups = new List<string>();
List<string> sbus = new List<string>();
// Objects for SBU changes
string previousSBU = "";
string previousSBUClient = "";
TextBox SBUName = new TextBox() { ID = "SBUName" };
TextBox SBUComments = new TextBox() { ID = "SBUComments" };
DropDownList SBUClientDropdown = new DropDownList();
protected void Page_Load(object sender, EventArgs e)
{
clients = DBHandler.GetClients();
clientGroups = DBHandler.GetClientGroups();
if (!Page.IsPostBack)
{
foreach (string client in clients)
{
SBUClientList.Items.Add(GenerateItem(client));
ClientList.Items.Add(GenerateItem(client));
}
foreach (string clientGroup in clientGroups)
ClientGroupList.Items.Add(GenerateItem(clientGroup));
}
if ((SBUClientList.SelectedValue.ToString() != previousSBUClient) || (SBUList.SelectedValue.ToString() != previousSBU))
{
previousSBUClient = SBUClientList.SelectedValue.ToString();
previousSBU = SBUList.SelectedValue.ToString();
sbus = DBHandler.GetSBUs(SBUClientList.SelectedValue);
SBUList.Items.Clear();
foreach (string sbu in sbus)
SBUList.Items.Add(GenerateItem(sbu));
LoadSBU();
}
}
void LoadSBU()
{
if ((SBUClientList.SelectedValue.Trim().Length > 0) && (SBUList.SelectedValue.Trim().Length > 0))
{
Entity thisSBU = DBHandler.GetSBUInformation(SBUClientList.SelectedValue, SBUList.SelectedValue);
SBUTable.Rows.Add(GenerateHeaderRow("Client Name", "SBU Name", "Comments"));
SBUClientDropdown.Items.Clear();
foreach (string client in clients)
SBUClientDropdown.Items.Add(GenerateItem(client));
SBUClientDropdown.SelectedValue = SBUClientList.SelectedValue;
SBUClientDropdown.SelectedIndex = SBUClientList.SelectedIndex;
TableCell SBUClientCell = new TableCell();
SBUClientCell.Controls.Add(SBUClientDropdown);
SBUName.Text = thisSBU.sSBUName;
TableCell SBUNameCell = new TableCell();
SBUNameCell.Controls.Add(SBUName);
SBUComments.Text = thisSBU.sSBUComments;
TableCell SBUCommentsCell = new TableCell();
SBUCommentsCell.Controls.Add(SBUComments);
SBUTable.Rows.Add(GenerateRow(SBUClientCell, SBUNameCell, SBUCommentsCell));
Button SBUSaveButton = new Button();
SBUSaveButton.Click += new EventHandler(this.SBUSaveChanges);
SBUSaveButton.Text = "Save SBU Changes";
TableCell SBUButtonCell = new TableCell();
SBUButtonCell.Controls.Add(SBUSaveButton);
SBUButtonCell.ColumnSpan = 3;
TableRow SBUFooter = GenerateRow(SBUButtonCell);
SBUFooter.TableSection = TableRowSection.TableFooter;
SBUTable.Rows.Add(SBUFooter);
}
}
void ShowMessage(string message)
{
OutputString.Text = "<div class=\"message\">" + message + "</div>";
}
ListItem GenerateItem(string input)
{
ListItem output = new ListItem();
output.Text = input;
return output;
}
TableCell GenerateCell(string text)
{
TableCell output = new TableCell();
output.Text = text;
return output;
}
TableRow GenerateRow(params TableCell[] cells)
{
TableRow output = new TableRow();
foreach (TableCell cell in cells)
output.Cells.Add(cell);
return output;
}
TableRow GenerateHeaderRow(params string[] columns)
{
TableRow output = new TableRow();
output.TableSection = TableRowSection.TableHeader;
foreach (string column in columns)
{
TableCell thisCell = new TableCell();
thisCell.Text = column;
output.Cells.Add(thisCell);
}
return output;
}
previousSBUClient and previousSBU will always be blank on each post back, so why do you compare against them on the Page_Load? If you want their values saved between postbacks you need to save them in ViewState, I suggest code like:
public string PreviousSBU
{
get { return Convert.ToString(ViewState["PreviousSBU"] ?? string.Empty); }
set { ViewState["PreviousSBU"] = value; }
}
Perhaps its because you add multiple header rows to the table, and only the contents between the first through the second get displayed? Any header rows after added don't get displayed?
On each postback you add a new header row. But the TextBox's are created on each postback and not saved between, so you shouldn't be seeing anything at all if thats the case.
To render txtName to the page, you should have something like:
this.Controls.Add(txtName); somewhere on the page, preferably in the Page_Init, though for what you've listed, at least before the LoadData() call in Page_Load.
Taking a guess at the missing code but are you databinding your dropdown list? If you are you may be doing it every time instead of just when the page is not a postback. Like I say, guessing at what's not in your question but you might want consider something like this instead:
if (!Page.IsPostback)
{
MyDropDownList.DataSource = blah;
MyDropDownList.DataBind();
}
myTextBox.Text = MyDropDownList.SelectedValue;
Create your controls in OnInit event, becouse viewstate serialization happens between oninit and onload. Also check if it is postback, to avoid overwriting values from viewstate.
I remember strange things happening if you don't supply an ID to the textbox, are you doing this?
txtName.ID="txtName";`

Categories

Resources