I have 3 ascx controls loaded in one aspx file as a tabbed manner. I have one button in first ascx control. If the button is clicked then I have to move to next tab. How to move to next tab?
I assume that you or your organization wrote the 3 ascx controls?
You're going to have to alert the parent of the tabbed control (the page?) that a child of the tabbed control (the ascx control) has requested that the tab control progress to another tab.
The simplest, least coupled way I know how to do this is with a custom event. The steps would be as follows:
Implement an event in your ascx control that is raised when the button in your ascx control is clicked. There's an example of this in this Q+A.
Since you're using Visual Basic, the syntax for the fourth step will be different. I describe this in the next step, below.
Set up a handler for that event in the page. (This is Step 4 in the example I linked to.)
The simplest way to associate the handler with the user control is in the markup:
<uc:MyUserControl TabChangeRequested="MyUserControl_TabChangeRequested"
runat="server" id="userControl1" />
This example assumes that MyUserControl_TabChangeRequested is a public or protected method of your page.
If this doesn't work, you can do the association programmatically. How you do this depends on whether the user control is visible to the page itself, or is a child of the tab control. If the user control is a child of the page, you can use this code (in Page_Load):
AddHandler userControl1.TabChangeRequested, AddressOf MyUserControl_TabChangeRequested
It's a very different syntax to that of C#, which can make examples confusing.
If the user control is a child of the tab control rather than being declared at the page control, you need to first get a reference to the user control from within the control collection of the specific tab:
Dim uc as MyUserControl = TabControl1.Tabs(0).FindControl("userControl1")
AddHandler uc.TabChangeRequested, AddressOf MyUserControl_TabChangeRequested
Depending on the tab control and how it was written, you might need to look in the tab control's own children rather than the tab's. The first line of the previous example would look like this:
Dim uc as MyUserControl = TabControl1.FindControl("userControl1")
In your handler, do whatever you need to do to change to the next tab.
Related
I am having a asp.net web page on which I am loading a single usercontrol (UC) ten times. The first user control will be used to gather data about the main employee and the rest about his dependent. There will be "next" and "Previous" navigation button to move from one UC to another. I have couple of DevExpress check boxes on the UC. If the user checks any of them on the first page and then clicks the "next" button to navigate to the next UC, I want the checkboxes in the dependent UC also checked. I tried to do that in the page load event of the dependent controls in code behind. The code is given below. But it doesn't work. Looks like that I have to use javascript to do it. Please let me know how to do it.
codebehind:
dependentCheckbox.checked = mainEmployeeCheckbox.checked;
Thanks
I have an ASP.NET web form on which I'm displaying a list of database items via user controls, generating the user controls dynamically - working fine.
Also on the page, I have an asp:dropdownlist filled with items that can be added to this database list. Along with this dropdown I have a button 'ADD'. My intent is that the user chooses and item, clicks add, and then the list of user controls on the form will include this new item.
I have all this working.
My issue is that the user control has a button 'DELETE', which removes the selected item from the list. That works, EXCEPT when I add a new item. Because my 'add' button event is always fired after Page_Load, even if I regenerate the list of user controls, the internal user control click events won't fire because the controls weren't created as part of Page_Load.
I need to know what I'm doing wrong, or best practices here, any advice. I tried to be precise in the description of the problem, but if I've not succeeded, let me know and I can add more details.
SIMPLE RESTATE: I need to know how to add a dynamically created user control to a page via a page button click event, and still have the user control internal click(etc) events firing.
Thanks for any help.
EDIT: Based on the feedback from the gentlemen here, and doing some further research related to their suggestions, I ended up implementing a solution based on what's presented on this page:
http://ryanfarley.com/blog/archive/2005/03/11/1886.aspx
Here's a snippet showing how I dealt with this. This snippet resides in my PreInit event handler. Not exactly an elegant weapon for a civilized age, but sometimes a blaster is all you've got to use.
'Look to see if button for adding a new client number has been
'clicked. If so, call the sub to add the item NOW, so that it
'is able to have it's internal click events fire.
For Each control_string As String In Request.Form
Dim ctl As Control = Page.FindControl(control_string)
If (ctl IsNot Nothing) AndAlso (ctl.ID = "cmdAddClientNumber") Then
Me.AddClientNumberToList()
Exit For
End If
Next
On the button handler, you initially add the UserControl to the Page. OnPreInit (which will next be fired when the user clicks Delete on the UserControl), you should re-add the UserControl - so that it exists, and can handle the Delete button event.
You will need to devise your own state tracking mechanism to determine that you need to add the UserControl during PreInit. I generally use ViewState, as evidenced by this seemingly similar post.
Similar question:
How to create and use custom control with UpdatePanel added to the page programmatically
Dynamic control should be re-added to the control tree OnPreInit, see documentation:
PreInit - Create or re-create dynamic controls.
ASP.NET Page Life Cycle Overview
I am working on ASP.NET application where I am reusing a user control. The user control contains a checkbox and bunch of other controls. I want to display all the controls inside the user control on all the pages but on one single page I want to hide the checkbox.
I was thinking that I can use the databind methods and see if I am on the "pagex" then hide the checkbox. Is there any other way to solve this problem?
If you have access to the code for the control you should be add a new property to the control for hiding/showing the checkbox and then just pass in the property depending upon what page you are on. You'd have to pass in the show/hide property on the load event of the page.
Do you have access to the code??
I would specify a parameter in the code behind for your ascx file. Example:
public bool HideCB = false;
THen when you put your User Control on your aspx page do this:
<uc:TestControl id="TestControl" runat="Server" HideCB="true" />
This way you can do a check on if(HideCB) to determine if you want to make it visible or not.
My Master Page has a heading with tabs. The code looks something like:
<CT:Tab ID="tabHome" runat="server" Url="/index.aspx" Text = "Home" Highlight="true" />
<CT:Tab ID="tabFun" runat="server" Url="/fun.html" Text = "Fun"/>
<CT:Tab ID="tabBlog" runat="server" Url="/blog" Text = "Blog"/>
I can think of two ways to control which tab is highlighted from within a user control:
Have the user control implement an interface. The master page can decide which tab to highlight based on which interface is implemented, or based on a method in the interface that returns a string.
In the Page_Load (or Page_Init) function, tell the master page (via FindControl or via a function in the Master Page) which control to highlight.
I don't really like either of these solutions. Is there a clean way I could control which tab is highlighted from the control at design time (i.e., in the aspx file)?
The best way I can think of is:
Create an event delegate on the UserControl that passes a parameter indicating which user control to highlight
Handle this on the master page; so whenever the event is fired it chooses the correct tab to highlight based on the parameter passed
Fire the event on the user control Page_Init
A good start on events and delegates is at MSDN here:
http://msdn.microsoft.com/en-us/library/aa645739(v=vs.71).aspx
I have a asp.net web page with place holder control and Menu control when user select an item from the menu. It will dynamically load the control based on menu item's value.
I got the control loaded but if i click on the a link button or anything on the web user control (.ascx) The web user control (.ascx) will disappear. I do not know what is causing this. Can someone take a look at my code and see what i'm missing?
Protected Sub Menu1_Click(ByVal sender As Object, ByVal e As EventArgs)
Select Case Me.Menu1.SelectedValue
Case "CustMasterMain"
Dim ccCustMasterMaint As UserControl = CType(Page.LoadControl("~/Controls/Franchise/CustMasterMaintControl.ascx"), UserControl)
Me.phHolder1.Controls.Add(ccCustMasterMaint)
Case "AcctRecInq"
Dim ccAcctRecInq As UserControl = CType(Page.LoadControl("~/Controls/Franchise/custAccountsReceivableInquiry.ascx"), UserControl)
Me.phHolder1.Controls.Add(ccAcctRecInq)
End Select
End Sub
Remember that each time you do a postback you are working with a brand new instance of your page class. If you added the control to the controls collection of a previous instance of your page class, you need to add it again for every postback that follows.
Additionally, if you want ViewState to be restored for the control then it needs to be added to the page before the Load event. That means adding the control during Init or PreInit.
Joel is correct that you need to add the control on every postback so you need a way to track what controls are stored on the page.
The Dynamic Controls Placeholder is an excellent control for helping manage dynamic controls on your page across postbacks. I'd recommend you check it out as I've used it on a number of projects and it's made life a lot easier for me.