I am adding the dynamically TextBox in the placeholder on the button click in that.
When all the textboxes are loaded I am making changes in the values and again Press another Button to save the values to the SharePoint List. But when I press the Save button and I checked the placeholder by debugging it I found that there were no any control in the placeholder.
I am adding the Controls like follows :
TextBox[] tb = new TextBox[item_ans.Count];
Literal[] lt = new Literal[item_ans.Count];
for (int j = 0; j < item_ans.Count; j++)
{
ans_id.Add(item_ans[j]["ID"].ToString());
tb[j] = new TextBox();
tb[j].ID = "tb_ans" + (j + 1).ToString();
tb[j].Text = item_ans[j]["Title"].ToString();
lt[j] = new Literal();
lt[j].Text = "<br/>";
pl_hd_ans.Controls.Add(tb[j]);
pl_hd_ans.Controls.Add(lt[j]);
}
And on the Save Button click I am Retrieving those TextBoxes Like follows:
int n = Convert.ToInt32(ViewState["totalAns"].ToString());
foreach (var i in ans_id)
{
var item_ans = list_ans.GetItemById(i);
clientContext.Load(item_ans);
clientContext.ExecuteQuery();
for (int k = 0; k < n; k++)
{
TextBox tb = (TextBox)pl_hd_ans.FindControl("tb_ans" + (k + 1).ToString());
item_ans["Title"] = tb.Text;
item_ans.Update();
clientContext.Load(item_ans);
clientContext.ExecuteQuery();
}
}
But in this I check the Placeholder's Controls that were 0. Can Any one please help me to solve this..?
I'm assuming its ASP.NET WebForms what we're talking about here.
When you are adding controls dynamically to the webpage, you have to recreate them on each sequential postback. The reason for this, is that the dynamically created controls are not present in the .aspx file, nor in the viewstate, so asp.net cannot know that it has to recreate these controls. Therefore, you yourself have to recreate them in the initialized-event (before the page-load), including adding any event handlers that you need.
You can google about it.
Related
I'm a beginner (and not a really good english-speaker :) ), and i want to bind my sitemapdatasource to an asp accordion.
Its works well with two levels but it doesn't works with the third level
I tried this :
accordion 1
header container
first level
accordion 2
header container
second level
content container
third level
I have something like :
menu 1
menu 1.1
menu 1.2
menu 2
...
and in the other accordion
menu 1.1.1 menu 1.1.2...
menu 2.1.1 menu 2.1.1
I read a lot of threads about this so i just want to know if my example should works (if i just need to fix the code, would be better if i fix it by myself), or if my idea sucks and i need o find an other way to resolve this issue
MasterPage.master
<ajaxToolkit:Accordion ID="myAccordion" runat="server" SuppressHeaderPostbacks="true" >
</ajaxToolkit:Accordion>
<ajaxToolkit:Accordion ID="myAccordion1" runat="server" SuppressHeaderPostbacks="true" >
</ajaxToolkit:Accordion>
MasterPage.master.cs (in the page_load)
for (int i = 0; i < SiteMap.RootNode.ChildNodes.Count; i++)
{
//GRABS SITEMAP MAIN ITEMS (UNDER HOME)
SiteMapNode smn = (SiteMapNode)SiteMap.RootNode.ChildNodes[i];
//CREATES ACCORDION PANE
AjaxControlToolkit.AccordionPane p = new AjaxControlToolkit.AccordionPane();
//CREATE UNIQUE PANE ID
p.ID = "Pane" + i;
//CREATE HEADER ITEM
HyperLink hlHeader = new HyperLink();
hlHeader.NavigateUrl = SiteMap.RootNode.ChildNodes[i].Url.ToString();
hlHeader.Text = SiteMap.RootNode.ChildNodes[i].Title.ToString();
//ADDS HEADER LINK TO PANE (HEADER)
p.HeaderContainer.Controls.Add(hlHeader);
//CHECKS IF HEADER ITEM HAS CHILDREN
if (smn.HasChildNodes)
{
AjaxControlToolkit.AccordionPane psub = new AjaxControlToolkit.AccordionPane();
psub.ID = "Panel" + i;
//CREATE BULLETED LIST OF CHILDREN
BulletedList blMenu = new BulletedList();
blMenu.DisplayMode = BulletedListDisplayMode.HyperLink;
//CREATES LIST ITEMS WITHIN BULLETED LIST FOR CHILDREN
for (int j = 0; j < smn.ChildNodes.Count; j++)
{
blMenu.Items.Insert(0, (new ListItem(smn.ChildNodes[j].Title.ToString(), smn.ChildNodes[j].Url.ToString())));
HyperLink hlSubHeader = new HyperLink();
hlSubHeader.NavigateUrl = smn.ChildNodes[j].Url.ToString();
hlSubHeader.Text = smn.ChildNodes[j].Title.ToString();
psub.HeaderContainer.Controls.Add(hlSubHeader);
BulletedList blSubMenu = new BulletedList();
blSubMenu.DisplayMode = BulletedListDisplayMode.HyperLink;
for (int k = 0; k < smn.ChildNodes[j].ChildNodes.Count; k++)
{
blSubMenu.Items.Insert(0, (new ListItem(smn.ChildNodes[j].ChildNodes[k].Title.ToString(), smn.ChildNodes[j].ChildNodes[k].Url.ToString())));
}
psub.ContentContainer.Controls.Add(blSubMenu);
myAccordion1.Panes.Add(psub);
}
//ADDS BULLETED LIST TO PANE (CONTAINER)
p.ContentContainer.Controls.Add(blMenu);
}
//ADDS PANE TO ACCORDION
myAccordion.Panes.Add(p);
}
Thanks
I am new to C#.
I have Form1 and flowlayoutpanel. I dynamically add Buttons to flowlayoutpanel and the buttons details comes from a database table.
I want to know the name of the first button in the flowlayoutpanel.
for (i = 0; i < DataTable.Rows.Count; i++)
{
Button btn = new Button();
btn.Name = DataTable.Rows[i]["Name"].ToString();
btn.Text = DataTable.Rows[i]["PostCode"].ToString();
flowlayoutpanel.Controls.Add(btn);
}
String First_Button_Name = ...........
If you want to get the name of the first button to be added to the FlowLayoutPanel, regardless of how those buttons got there, use:
string firstButtonName = flowlayoutpanel.Controls.OfType<Button>().FirstOrDefault()?.Name;
This is the value you are searching for:
DataTable.Rows[0]["Name"].ToString()
but make sure you have at least an element before you try to get this value.
QUESTION UPDATE
I am using this piece of code for cor adding multiple labels on button click.
But each and every time it gives count value 0 and after execution only same label comes.
int count = (int)ViewState["count"];
for (int i = 0; i <= count; i++)
{
TextBox txtnew = new TextBox();
txtnew.ID = "label_" + (i + 1);
txtnew.Text = "label_" + (i + 1);
ViewState["count"] = i;
pnlControl.Controls.Add(txtnew);
}
ViewState["count"] = count + 1;
What i want now is how to keep that data of each control binded to it in its more convenient way.
Dynamic controls are lost on every PostBack, so you need to keep track of the created ones somewhere and recreate them when the page is reloaded.
See my anwer here: How to dynamically create ASP.net controls within dynamically created ASP.net controls
It is about buttons but the basic principle is the same. Just replace Button with Label and it will work.
That's cause it's a web application and thus the session is not retained cause web applications are stateless in nature. So, every post back you get a new page instance which will have int count is 0. Solution: Use Session to store the data for future re-use like
int count = pnlControl.Controls.OfType<Label>().ToList().Count;
Session["count"] = count;
Retrieve it back on postback request
if(Session["count"] != null)
count = (int)Session["count"];
Basically I'm making a program that allows you to add to a stackpanel another stackpanel with several horizontally aligned textboxes with the press of a button. So far, everything is working as intented. Here's my code so far ,Stacker is the name of the parent stackpanel and it starts off empty:
private void Add_Click(object sender, RoutedEventArgs e)
{
Stacker.Children.Add(NewXD(Stacker.Children.Count + 1));
}
public System.Windows.Controls.StackPanel NewXD(int XD)
{
System.Windows.Controls.StackPanel NewP = new StackPanel();
NewP.Orientation = Orientation.Horizontal;
System.Windows.Controls.TextBox HAHA = new TextBox();
HAHA.Name = "question" + XD.ToString();
//HAHA.Text = HAHA.Height.ToString()+" "+HAHA.Width.ToString();
HAHA.Height = Double.NaN;
HAHA.Width = 120;
HAHA.FontSize=20;
NewP.Children.Add(HAHA);
for (int i = 1; i < 6; i++)
{
System.Windows.Controls.TextBox newBox = new TextBox();
newBox.Name = "answer"+XD.ToString()+"_"+i.ToString();
newBox.Height = Double.NaN;
newBox.Width = 120;
NewP.Children.Add(newBox);
}
System.Windows.Controls.ComboBox correct = new ComboBox();
correct.Name = "correct" + XD.ToString();
for (int i = 1; i < 6; i++)
{
System.Windows.Controls.ComboBoxItem newItem = new ComboBoxItem();
newItem.Name = "ans" + XD.ToString() + "_" + i.ToString();
newItem.Content = i.ToString();
correct.Items.Add(newItem);
}
NewP.Children.Add(correct);
return NewP;
}
I apologize for the lack of seriousness in some of my code.
Now, what I need to do is for the child stackpanels to also contain independent file pickers that work like the one sampled in this thread: Open file dialog and select a file using WPF controls and C#
What I don't know how to perform is that each of these generated buttons have the same basic funcion but are linked with each of their corresponding textbox.
Thanks in advance :)
Edit: As I was writing this it occured to me that perhaps I could use the help of the child stackpanel's array-like properties to choose the corresponding textbox, because the file selector's textbox and button will always be the last two items in the stackpanel, but I'm not very sure how to perform this.
For functionality you can create an EventHandler that will be assigned to each button. Your event handler will then open File Dialog...
Buttons have Tag property which you could use to identify your TextBoxes, or you could derive from Button class and add AssociatedTextBox property for example.
Please help me understand what is this error that I'm getting:
lblTabCounter is a label coded in the aspx page while the lblc[index] is a collection of label created at runtime during page load.
Declaration outside of page load:
Label[] lblc = new Label[10];
Inside Page Load Event:
for (int i = 0; i < 10; i++)
{
lblc[i] = new Label() { Text = (i + 1).ToString() };
this.Controls.Add(lblc[i]);
}
Inside another event called NodeChanged:
int TabCount = Convert.ToInt32(lblTabCounter.Text.ToString());
int TabIndex = Convert.ToInt32(lblTabCounterIndex.Text.ToString());
if(TabCount <= 10)
{
divcont.Visible = true;
string tabName = getURLName(uRL);
MenuItem myItem = new MenuItem(tabName, TabIndex.ToString());
Menu1.Items.AddAt(TabIndex, myItem);
//f1.Attributes["src"] = url;
f1.Attributes.Add("src", lblURL.Text.ToString());
MultiView1.ActiveViewIndex = TabIndex;
lblc[TabCount].Text = lblTabCounter.Text;
lblc[TabCount + 1].Text = lblURL.Text;
TabCount++;
TabIndex++;
lblTabCounter.Text = TabCount.ToString();
lblTabCounterIndex.Text = TabIndex.ToString();
tvPermissions.ExpandAll();
//tvPermissions.CollapseAll();
int i = ctr;
}
Note: This are all inside site.master.
The problem is your web page is refreshing and losing the state of the labels.
Label[] lblc = new Label[10];
protected void Page_Load(object sender, EventArgs e)
{
for (int i = 0; i < 10; i++)
{
lblc[i] = new Label();
this.Controls.Add(lblc[i]);
if (Session["lblc" + i.ToString()] == null)
Session["lblc" + i.ToString()] = lblc[i].Text = (i + 1).ToString();
else
lblc[i].Text = (string)Session["lblc" + i.ToString()];
}
Then when you want to set a label you use the following (when the page is not being refreshed by the event)
lblc[4].Text = "cool";
Session["lblc4"] = "cool";
However because your click event is refreshing the page it loses contact with the lblc so you only set the Session so upon refresh you will see your new Label. (when the page is being refreshed by the event)
Session["lblc4"] = "cool";
The page is in the process of refreshing as a result of your particular event so the label disappears but the session state remains so when you set the session upon refresh the code grabs the session instead of setting it to the default number.
Rather than change the text of the label when it refreshes you are actually generating the new label with the session string you set.
Also make sure you have <sessionState mode="InProc" /> in your Web.config file under <system.web>
Please read more on Session States here http://msdn.microsoft.com/en-us/library/87069683(v=vs.80).aspx
There are two possible issues with that line of code:
lblc[TabCount] is null.
lblTabCount is null.
Since you are paused in the debugger, you can see which of those is the case, then look around in the rest of the code to find out why.
I would follow the path of the lblc[index] array to determine if the element offset is within range as well as it being created properly and not ending up there as a null (whether the null being the object lblc[index] or the text property) being referenced.