Handling dynamic controls from master page - asp.net

I have a asp:placeholder in master page. From content page (in page_init) I am dynamically creating drop downs and adding to the asp: placeholder in master page. How do I access these dynamically created controls by id?
I tried this
Dim Outer_CP As ContentPlaceHolder Outer_CP = TryCast(Me.Master.Master.FindControl("MyContent"), ContentPlaceHolder)
Dim mydropdownsAs PlaceHolder = TryCast(Outer_CP.FindControl("mydropdownsAs "), PlaceHolder)
Dim ddlControl As DropDownList = CType(mydropdownsAs .FindControl(idName), DropDownList)

Related

how do i parse a value in data table to master page from content page on button click event

I have a label called Lblcountcart on my master page. I have set the virtual path of the master page on my content page. I have also created a public property of the label on the master page. from my content page i am able to get access to the property of the label but when i parse the data the label on my master page still remains empty thus i dont see the data i parsed. below is my code
//on my master page
Public Property cartcountitems As Label
Get
cartcountitems = Me.Lblcountcart()
Return Lblcountcart
End Get
Set(value As Label)
Lblcountcart.Text.ToString()
End Set
End Property
//my content page
<%# MasterType VirtualPath="~/mysitemaster2.Master" %>
//code behind of my contentpage
Dim selcartcountda As New SqlDataAdapter(selcartcount)
Dim selcartcountdt As New DataTable()
selcartcountda.Fill(selcartcountdt)
Master.cartcountitems.Text = selcartcountdt.Rows.Count

AJAXControlToolkit TabContainer-How to add and remove tab dynamically

I am using AjaxControlToolkit version 4.1, .NET 4.0, VS2010,language-c#
Using the TabContainer control I want to add/remove tabs dynamically. Suppose I have some .aspx page. In 1st tab I have some hyperlink . While I will click on the hyperlink the corresponding .aspx page will open in a tab dynamically and also I can remove it.How to do it? Can anyone help me?
Create
Create TabContainer
Dim tbContainer As New AjaxControlToolkit.TabContainer
tbContainer.ID = "TabContainer1"
Create TabContent
Dim tbContent As New Control
tbContent.ID = "tbContent_" & dt.Rows(i).Item("PRENR").ToString
Create TabPanel
Dim tbPanel As New AjaxControlToolkit.TabPanel
tbPanel.ID = "tbPanel_" & dt.Rows(i).Item("PRENR").ToString
tbPanel.HeaderText = "PREMIUM AMOUNT"
Adding the controls to Page
tbContent.Controls.Add(dvEarner1)
tbPanel.Controls.Add(tbContent)
tbContainer.Tabs.Add(tbPanel)
tbContainer.Tabs.Add(tbPanel1)
tbContainer.Tabs.Add(tbPanel2)
tbContainer.Tabs.Add(tbPanel3)
tbContainer.Tabs.Add(tbPanel4)
tbContainer.Tabs.Add(tbPanel5)
pane.ContentContainer.Controls.Add(tbContainer)
Me.MainAccordion1.Panes.Add(pane)
Remove
Me.TabContainer1.Tabs.Remove(tab)

Displaying listbox control from another page ASP.Net VB.Net

I need to display the values from a listbox on a content page in a textbox on a masterpage in ASP.Net using VB.Net
Thanks in advance.
You can try this
Dim txt As Textbox = DirectCast(Master.FindControl("yourTextbox"), Textbox)
txt.text = "your Value here"
So you should run this code on your content page.
I assume your textbox on the masterpage is not in a updatepanel or panel etc. then you need to refer that first. but if not, this should work....
Dim tb As Textbox = DirectCast(Master.FindControl("theNameofYourTextBox"), Textbox)
tb.Text = ListBox.Item.Value (or the equivalent for getting the text value in the listbox)

How do I add a value to a TextBox that's inside an ASP:LoginView

I have some DropDownLists and some TextBoxes on a page. Normally I use this to clear and add value to the items:
Clear control:
txtEventNote.Text = String.Empty
ddlHours.SelectedIndex = 0
Add value:
ddlHours.SelectedValue = dtEvents.Rows(0)("EventDate")
txtEventTitle.Text = dtEvents.Rows(0)("EventHome").ToString()
But if I do this for items thats inside an ASP:LoginView I get an error because the ID's cant be found.
So my question is:
How do I clear the ddlSize DropDownList ID and how do i add String.Empty to txtEventName.Text that's inside the LoginView which has an ID of "loginview2"?
And how do I add a value to a TextBox and DropDownList inside an LoginView called "loginview2" where I have a TextBox with the ID textEventName.Text and the DropDownList have ID ddlSize.
If I delete the LoginView it's easy for me to do, but when I add the LoginView the ID's can't be found.
The LoginView control is a container control. It can hold other ASP.NET WebForm controls. To get a reference to the controls contained within the LoginView control use the FindContol method of the LoginView control.
TextBox tb = lcLoginView.FindControl("txtEventNote") as TextBox
if (tb != null)
{
tb.Text = "My New Value For This TextBox control";
}
Do the same for your DropDownList.
I found In Search Of ASP.Net Controls to be helpful for more about finding ASP.NET controls in other ASP.NET controls.

Losing <asp:Label> Text value from ViewState for dynamically added control

I am adding controls to a page programatically in the code behind. I add an asp:Label and set it's Text value. I add an asp:TextBox and set it's Text value. Both Text values are returned in the Response and displayed in the browser. All fine so far.
The user performs an action that causes a postback. I re-load the dynamically added asp:Label and asp:TextBox. When the Response is returned to the browser, only the asp:TextBox Text value is displayed. The asp:Label Text value is not.
If I inspect the HTML I can see the asp:Label control (rendered as an HTML span tag) but no value.
How can I get the code to automatically re-load the Text value of an asp:Label on each postback? Why is the behaviour different for an asp:Label and an asp:TextBox? I do not want to have to manually re-set the Text value on each postback.
Here is some code similar to what I am doing (placeHolderNameplates is an asp:PlaceHolder control on the aspx page):
Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
If Not Page.IsPostBack Then
Dim lbl As Label = New Label()
lbl.ID = "xxx1"
lbl.Text = "yo"
placeHolderNameplates.Controls.Add(lbl)
Dim tb As TextBox = New TextBox
tb.ID = "xxx2"
tb.Text = "yoyo"
placeHolderNameplates.Controls.Add(tb)
Else
Dim lbl As Label = New Label()
lbl.ID = "xxx1"
placeHolderNameplates.Controls.Add(lbl)
Dim tb As TextBox = New TextBox
tb.ID = "xxx2"
placeHolderNameplates.Controls.Add(tb)
End If
What you need to do is add the control to the placeholder before setting the values, so it should be
Dim lbl As Label = New Label()
placeHolderNameplates.Controls.Add(lbl)
lbl.ID = "xxx1"
lbl.Text = "yo"
See these posts for details:
http://www.yakkowarner.com/2008/01/aspnet-dynamic-controls-and-viewstate.html
http://codebetter.com/jefferypalermo/2004/11/25/key-to-ensuring-dynamic-asp-net-controls-save-viewstate-level-300/
Before they are added to the page, they have not initialized themselves. When a dynamic control is added to another control, the new control plays catch-up to get to the stage that the parent control is in. For instance, if in your Page_Load, you add a textbox, it will play catch-up and go through its Init and Load phases. This is important beceause it will start tracking its viewstate. Values added before it is tracking viewstate won’t make it to viewstate and will be lost on PostBack.
It seems like dynamically created controls won't be added to the ViewState automatically. The TextBox Control retains it's value however because of it's nature of being rendered to a <input type="text" value="xyz" /> html element.
Have a look at this article:
http://www.codeproject.com/Articles/3684/Retaining-State-for-Dynamically-Created-Controls-i
Hey check thsi site MSDN
You have to add your control with following event( so viewstate maintain automaticly)
override protected void OnInit(EventArgs e)
example of Add Dynamic control
http://support.microsoft.com/kb/317794/en-us

Resources