I wanted to display no of course json objects in each textbox
but as they are unpredictable number of objects therefore i created textbox on the fly using this code
List<Course> Cdata = JsonConvert.DeserializeObject<List<Course>>(App.data);
TextBox[] Tblock = new TextBox[Cdata.Count];
double top = 0; int i = 0;
foreach (Course de in Cdata)
{
result += de.course_name + "\r\n";
result += "Total Absents = " + de.absents;
result += " + " + de.presents;
result += " = " + de.sessions + "\r\n\r\n\r\n";
Tblock[i] = new TextBox();
Tblock[i].Text = result;
Tblock[i].AcceptsReturn = true;
Tblock[i].TextWrapping = TextWrapping.Wrap;
Tblock[i].Width = 475;
Tblock[i].Height = 270;
Tblock[i].IsReadOnly = true;
Tblock[i].Margin =new Thickness (0,top,0,0);
Tblock[i].Visibility = System.Windows.Visibility.Visible;
Tblock[i].VerticalAlignment = System.Windows.VerticalAlignment.Top;
top += 270; i++;
result = "";
}
Now when i debug my app data it is working as its supposed to the only problem is textbox
never display on View
and i haven't coded any textbox in Xaml file of view
Thanks in Advance
You have to add the Textboxes to any existing Panel (generally to Grid or StackPanel) in the XAML as shown below
StackPanel sp = new StackPanel(); //Create stack panel before foreach loop
foreach (Course de in Cdata)
{
//your code which you shown above
sp.Children.Add(Tblock[i]); //Add all the Textboxes to the stackpanel
}
ContentPanel.Children.Add(sp); //And add the above stackpanel to the existing Grid named ContentPanel
By the way, I suggest you to use a ListBox with ItemTemplate to bind the data instead of creating the TextBoxes as shown above.
Also, I don't understand why you have choosen TextBox instead of TextBlock to display data
Related
This has definitely been a 1 step forward 43 steps back. Started by creating by first gridview user control. But anyway. What I have is dynamically created gridviews. The entire page is built on masterpage and each gridview resides inside its own dynamically created collapsible panel.
Now in I need to loop through each gridview's rows. Problem is I cannot find them in the code behind. I have tried various recursive functions with no luck. So I have no clue to what I am doing with this.
Here is the code I am using to create the gridviews and panels.
int x = 1;
while (sqlReader.Read())
{
// Create Panel Header
Panel PH = new Panel();
PH.ID = "PH" + Convert.ToString(x);
PH.CssClass = "cheader";
// Add Label inside header panel to display text
Label lblPH = new Label();
lblPH.ID = "lblPH" + Convert.ToString(x);
lblPH.Text = sqlReader["Category"].ToString();
PH.Controls.Add(lblPH);
//Create Body Panel
Panel PB = new Panel();
PB.ID = "PB" + Convert.ToString(x);
// Create CollapsiblePanelExtender
CollapsiblePanelExtender CPE = new CollapsiblePanelExtender();
CPE.ID = "CPE" + Convert.ToString(x);
CPE.TextLabelID = lblPH.ID;
CPE.ExpandControlID = PH.ID;
CPE.CollapseControlID = PH.ID;
CPE.TargetControlID = PB.ID;
CPE.ScrollContents = false;
CPE.Collapsed = true;
CPE.ExpandDirection = CollapsiblePanelExpandDirection.Vertical;
CPE.SuppressPostBack = true;
this.UpdatePanel1.ContentTemplateContainer.Controls.Add(PH);
this.UpdatePanel1.ContentTemplateContainer.Controls.Add(PB);
this.UpdatePanel1.ContentTemplateContainer.Controls.Add(CPE);
//*** This is loading the gridview user control
EPOGVSurvey ctl = Page.LoadControl("EPOGVSurvey.ascx") as EPOGVSurvey;
ctl.MyParameterValue = sqlReader["Category"].ToString();
ctl.ID = "CAT" + x;
PB.Controls.Add(ctl);
PB.CssClass = "cdata";
x++;
}
sqlReader.Close();
Code to try to read the gridviews
GridView gv = FindControlRecursive(Page, "Gridview" + i) as GridView;
if (gv != null)
{
foreach (GridViewRow row in gv.Rows)
{
class TextColumn : ITemplate
{
private string controlId;
private string cssClass;
public TextColumn(string id, string cssClass = "inputFromTo")
{
controlId = id;
this.cssClass = cssClass;
}
public void InstantiateIn(Control container)
{
TextBox txt = new TextBox();
txt.ID = controlId;
txt.CssClass = cssClass;
container.Visible = true;
container.Controls.Add(txt);
}
}
/************************************ Add column code snippet ****************************/
TemplateField dentry = new TemplateField();
TemplateField dexit = new TemplateField();
TemplateField dslack = new TemplateField();
dentry.ItemTemplate = new TextColumn("txtHH" + nameCount + "DEntry");
dexit.ItemTemplate = new TextColumn("txtHH" + nameCount + "DExit");
dslack.ItemTemplate = new TextColumn("txtHH" + nameCount + "DSlack");
gvOfcBlowingReport.Columns.Insert(startPoint, dentry);
gvOfcBlowingReport.Columns.Insert(startPoint + 1, dexit);
gvOfcBlowingReport.Columns.Insert(startPoint + 2, dslack);
/********************************* Remove column code snippet ************************/
gvOfcBlowingReport.Columns.RemoveAt(startPoint - 1);
gvOfcBlowingReport.Columns.RemoveAt(startPoint - 2);
gvOfcBlowingReport.Columns.RemoveAt(startPoint - 3);
// after executing this code all the columns vanish.
Anyone know how to remove this text box template column?
In the above code I am adding templated field textbox and later on removing the same on button click but due to some reason all the templated field are getting affected and returning null when i am using FindControl in grid. In display also grid is displayed as empty.
Some other people are also facing the same problem over http://forums.asp.net/t/1162011.aspx but so far no valuable solution.
I have a problem I seem to stumble over all the time, I have a Drop Down box and you can select a number which creates x number of textboxes with images buttons its for a survey it the image buttons are used to create "Sub-Answers" so they can have answers to answers so my question is I need to when they hit the image button to create a textbox under the orginal textbox here is the code.
for (Int32 i = 1; i <= NumberOfAnwsers; i++)
{
Literal l1 = new Literal();
l1.Text = "<tr><td>Answer " + i + " text.</td><td>";
TextBox tb = new TextBox();
tb.ID = "TextBoxAnswer" + i;
tb.EnableViewState = false;
tb.Width = 300;
Literal l3 = new Literal();
l3.Text = "</td><td>";
Literal l2 = new Literal();
l2.Text = "</td></tr>";
RadColorPicker CPI = new RadColorPicker();
CPI.PaletteModes = PaletteModes.WebPalette;
CPI.ID = "RadColorPicker" + i;
CPI.ShowIcon = true;
CPI.SelectedColor = System.Drawing.Color.Black;
ImageButton IBVideo = new ImageButton();
IBVideo.ID = "IBVideo" + i;
IBVideo.ImageUrl = "/images/video-icon.jpg";
IBVideo.ToolTip = "Add Video";
IBVideo.Height = 20;
IBVideo.Width = 20;
ImageButton IBAdd = new ImageButton();
IBAdd.ID = "IBAdd" + i;
IBAdd.ImageUrl = "/images/add-icon.png";
IBAdd.ToolTip = "Add Sub-Answers";
//IBAdd.OnClientClick = "showDialog(" + i + ");return false;";
IBAdd.Height = 20;
IBAdd.Width = 20;
//Add Textbox
PanelAnswersToQuestions.Controls.Add(l1);
PanelAnswersToQuestions.Controls.Add(tb);
PanelAnswersToQuestions.Controls.Add(l3);
PanelAnswersToQuestions.Controls.Add(CPI);
PanelAnswersToQuestions.Controls.Add(IBVideo);
PanelAnswersToQuestions.Controls.Add(IBAdd);
PanelAnswersToQuestions.Controls.Add(l2);
}
As you can see I just add controls to the panel, I need to know when that ImageBUtton is hit I can add a Textbox and in this case it could be more then just one textbox to it.
I hope this is clear but for some reason I dont think it is ... sorry.
I have added a radwindow and poping that up sending the Data to the partent via javascript the which created a new problem for me, I can not in javascript seem to find the dynamicly created hiddenfield
function OnClientClose(radWindow) {
var oWnd = $find("<%=RadWindowAddSubAnswer.ClientID%>");
var SubAnswerValues = oWnd.get_contentFrame().contentWindow.document.forms(0).HiddenFieldSubAnswers.value;
alert(SubAnswerValues);
var AnswerID = oWnd.get_contentFrame().contentWindow.document.forms(0).HiddenFieldAnswerID.value;
alert(AnswerID);
var HiddenName = "HiddenFieldSubAnswers" + AnswerID;
alert(HiddenName);
document.getElementById(HiddenName).value = SubAnswerValues;
$get("DivSubAnswers" + AnswerID).innerHTML = SubAnswerValues;
}
The "document.getElementById(HiddenName).value = SubAnswerValues;" seems to never be found, I also tried $get(HiddenName).value = SubAnswerValues; that does not seem to work either both come back as null as for the code behind its:
HiddenField HFSubAnswers = new HiddenField();
HFSubAnswers.ID = "HiddenFieldSubAnswers" + i;
HFSubAnswers.Value = "0";
Im not sure if I got your question right but if you need to dynamically add controls on a Page here is what I can say.
Before adding your control I guess you need to find the control where you need to add it on, Add the control then assign the properties.
PlaceHolder myPlaceHolder = (PlaceHolder)Page.FindControl("PlaceHolder1");
myPlaceHolder.Controls.Add(myButton);
myButton.Text = "Hello World";
For a more detailed expalnation go here http://anyrest.wordpress.com/2010/04/06/dynamically-removing-controls-in-a-parent-page-from-a-child-control/
I have a web app where the user can create dynamic textboxes at run time. When the user clicks SUBMIT, the form sends data to the database and I want remove the dynamic controls.
The controls are created in the following code:
Table tb = new Table();
tb.ID = "tbl";
for (i = 0; i < myCount; i += 1)
{
TableRow tr = new TableRow();
TextBox txtEmplName = new TextBox();
TextBox txtEmplEmail = new TextBox();
TextBox txtEmplPhone = new TextBox();
TextBox txtEmplPosition = new TextBox();
TextBox txtEmplOfficeID = new TextBox();
txtEmplName.ID = "txtEmplName" + i.ToString();
txtEmplEmail.ID = "txtEmplEmail" + i.ToString();
txtEmplPhone.ID = "txtEmplPhone" + i.ToString();
txtEmplPosition.ID = "txtEmplPosition" + i.ToString();
txtEmplOfficeID.ID = "txtEmplOfficeID" + i.ToString();
tr.Cells.Add(tc);
tb.Rows.Add(tr);
}
Panel1.Controls.Add(tb);
The Remove section of the code is:
Table t = (Table)Page.FindControl("Panel1").FindControl("tbl");
foreach (TableRow tr in t.Rows)
{
for (i = 1; i < myCount; i += 1)
{
string txtEmplName = "txtEmplName" + i;
tr.Controls.Remove(t.FindControl(txtEmplName));
string txtEmplEmail = "txtEmplEmail" + i;
tr.Controls.Remove(t.FindControl(txtEmplEmail));
string txtEmplPhone = "txtEmplPhone" + i;
tr.Controls.Remove(t.FindControl(txtEmplPhone));
string txtEmplPosition = "txtEmplPosition" + i;
tr.Controls.Remove(t.FindControl(txtEmplPosition));
string txtEmplOfficeID = "txtEmplOfficeID" + i;
tr.Controls.Remove(t.FindControl(txtEmplOfficeID));
}
}
However, the textboxes are still visible.
Any ideas?
I would assume that your creating these controls each time the page is loaded or else when your remove code ran on postback it would fail because they would not exist.
So you need to move the create code to NOT happen with every page load by making sure it is wrapped in a if (!IsPostBacK) { ... } statement.
If you do this then you wouldn't need to remove them manually since they are created dynamically and therefore not created by default on each postback.
If you can post where the code that creates the controls is being called from, and the remove as well, I could help you a bit more.
What I ended up doing was instead of deleting the textbox, I deleted the TableRow;
I am attempting to remove dynamically created controls in C# 2008 asp.net
The controls are created here:
int i;
for (i = 0; i < myCount; i += 1)
{
TextBox txtAuto = new TextBox();
TextBox txtModel = new TextBox();
TextBox txtMiles = new TextBox();
TextBox txtVINumber = new TextBox();
TextBox txtPlateNumber = new TextBox();
txtAuto.ID = "txtVehAuto" + i.ToString();
txtModel.ID = "txtVehModel" + i.ToString();
txtMiles.ID = "txtVehMilage" + i.ToString();
txtVINumber.ID = "txtVehVINumber" + i.ToString();
txtPlateNumber.ID = "txtVehPlate" + i.ToString();
phAuto.Controls.Add(txtAuto);
phModel.Controls.Add(txtModel);
phMiles.Controls.Add(txtMiles);
phVINumber.Controls.Add(txtVINumber);
phPlateNumber.Controls.Add(txtPlateNumber);
dyntxtAuto[i] = txtAuto;
dyntxtModel[i] = txtModel;
dyntxtMiles[i] = txtMiles;
dyntxtVINumber[i] = txtVINumber;
dyntxtPlateNumber[i] = txtPlateNumber;
LiteralControl literalBreak = new LiteralControl("<br />");
phAuto.Controls.Add(literalBreak);
phModel.Controls.Add(literalBreak);
phMiles.Controls.Add(literalBreak);
phVINumber.Controls.Add(literalBreak);
phPlateNumber.Controls.Add(literalBreak);
}
}
How can I remove the controls?
The user will click "submit" at which point the user entered data will be written to the db. The page will retrun back to itself (with blank fields). I want to remove the dynamic controls after the data has been written to the db. Remove meaning delete, not hide. I tried to set ViewState to false, but it doesn't help.
Can you do this?
phAuto.Controls.Clear();
phModel.Controls.Clear();
phMiles.Controls.Clear();
phVINumber.Controls.Clear();
phPlateNumber.Controls.Clear();
Are there other controls in these controls that I'm not aware of that you do not want cleared?
It depends on what you are trying to do...
If you are simply wanting to remove ALL the controls within your placeholder "phAuto", "phModel", etc... you can use the *.Controls.Clear(); method.
However, if you're looking to remove individual controls... you can use the *.FindControl(string id) method. It will return a generic Control object of with the id you pass to it. You could then pass that object to the *.Controls.Remove(...) method.
If you're wanting to use Page.FindControl(), Page.Controls.Remove(), etc AND you're using master pages... it gets kinda tricky. Read this article to learn about Recursive Control searching.
I'm still new to .NET myself and I'm finding new (and easier) ways of doing things daily. I would not be surprised if there's a better way of doing this.
Good Luck!
private List<Control> ControlList = new List<Control>;
private void CreateControls();
{
int i;
for (i = 0; i < myCount; i += 1)
{
TextBox txtAuto = new TextBox();
TextBox txtModel = new TextBox();
TextBox txtMiles = new TextBox();
TextBox txtVINumber = new TextBox();
TextBox txtPlateNumber = new TextBox();
txtAuto.ID = "txtVehAuto" + i.ToString();
txtModel.ID = "txtVehModel" + i.ToString();
txtMiles.ID = "txtVehMilage" + i.ToString();
txtVINumber.ID = "txtVehVINumber" + i.ToString();
txtPlateNumber.ID = "txtVehPlate" + i.ToString();
ControlList.Add(txtAuto);
ControlList.Add(txtModel);
ControlList.Add(txtMiles);
ControlList.Add(txtVINumber);
ControlList.Add(txtPlateNumber);
phAuto.Controls.Add(txtAuto);
phModel.Controls.Add(txtModel);
phMiles.Controls.Add(txtMiles);
phVINumber.Controls.Add(txtVINumber);
phPlateNumber.Controls.Add(txtPlateNumber);
dyntxtAuto[i] = txtAuto;
dyntxtModel[i] = txtModel;
dyntxtMiles[i] = txtMiles;
dyntxtVINumber[i] = txtVINumber;
dyntxtPlateNumber[i] = txtPlateNumber;
LiteralControl literalBreak = new LiteralControl("<br />");
phAuto.Controls.Add(literalBreak);
phModel.Controls.Add(literalBreak);
phMiles.Controls.Add(literalBreak);
phVINumber.Controls.Add(literalBreak);
phPlateNumber.Controls.Add(literalBreak);
}
}
private void RemoveControls(List<Control> ControlList)
{
foreach (Control item in ControlList)
{
item.Remove();
}
}
I haven't used ASP.NET in a while, and this is untested, but I would assume this (or a variation of it) would work. I can't remember if List<Control> works or not... Might have to make it List<TextBox>.