Changing Appearance of Master Page based on Content Page - asp.net

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

Related

How to prevent validation for an event handler of a user control?

I have a user control with a delete button. When a button is clicked, an event fires which deletes a record from the database. Now, the control is placed in Default.aspx. The whole body markup of Default.aspx (including the user control with its button itself) resides in <form runat="server"> as required by ASP.NET. Everything works so far.
However, the problem is when I put some validation controls inside Default.aspx (meaning inside <form runat="server"> because otherwise the page will report server errors). When validation controls are added, the delete button in the user control stops working. Clicking on this button no longer triggers the event as before.
Now, I disabled event validation in Default.aspx using EnableEventValidation="false" directive. I am also including UnobtrusiveValidationMode = System.Web.UI.UnobtrusiveValidationMode.None; in the code behind file. However, none of this helps.
How do I fix this problem and make the button clickable?
Update:
I know for sure that the validation controls are causing the problem, because I only need to add EnableClientScript="False" to each of them, and the button becomes clickable. But I don't want to rewrite validation on the client side manually!
It turns out the PostBack cannot occur if the form is not IsValid regardless of what element causes it. As long as that element (button) is inside the form with runat="server"that is invalid, the posback will not happen.
A very simple workaround is to just make the Button in my user control bypass validation: CausesValidation="False" (thanks to this question).
Another solution and maybe a more efficient one, is to use ValidationGroup. This way, all TextBoxes together with the Submit Button will belong to one group, and those controls that do not belong to that group will NOT be validated. In fact, they might have their own ValidationGroup; this will avoid interference between different controls within one Web Form.

How to switch among tabs with the Ajax Control Toolkit TabContainer?

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.

How to set the checked status of an asp.net user control'checkbox from one to another

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​

Hide Checkbox on a particular page

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.

jQuery/AJAX redirect?

Ok, this is a bit different scenario. I guess I would have to think about doing it this way sort of with MVC anyway if we were actually using MVC...but we're not at the moment.
So I've got and .aspx page. In that .aspx page is a user control (.ascx). And in that user control is a custom control (.cs).
The custom control has a repeater in it. So I'm showing a list of items on that .aspx through the .ascx's custom control. For each item in the repeater is a button. It's just a hyperlink, just a regular on my page
When you click that button, it redirects to whatever page you're on. Since the custom control never knows what your parent .aspx page is, I'm doing a redirect to the self .aspx by doing a Response.Redirect(Request.Path). So that way it always redirects to whatever .aspx is using that user control and custom control.
So after it redirects to self, I check the querystring in the page_load of whatever .aspx is using it. If the value is true, then I handle it however the .aspx wants to. In this case when it's true, I call a method in the code behind of my .aspx that handles the action for the button. For example lets say that button was "Add to Shopping Cart", the .aspx handles that action and calls a AddToCart method in the .aspx.cs.
I'm not using an ASP.NET control for the actual hyperlink and button because I just don't need it and in my particular case I'm using a user control and a custom server control. For this instance, I had some issue where I didn't wnat to use an ASP.NET control...I forget why but the point is, no this is what it is.
So with that, I'm trying to figure out how I can apply some AJAX here call to call that method instead. I still need to somehow redirect again back to the same page like I'm doing...I'm doing the redirect in that method after all the logic at the end. I am redirecting again back to the same page, because I need my Page_Load methods in my .aspx and also in an .ascx to still fire off after that method is completed.
So I am not sure where to start on this. Let me go through this once again:
Custom control has a repeater in it and in the repeater, each item has a standard HTML hyperlink (non ASP.NET control) which wraps a standard image tag (image is a button)
User control contains the custom control
The .aspx page contains the user control
User clicks the button and hyperlink redirects them to the parent .aspx page that is using this custom control...so it calls Response.Redirect(Request.Path)
In the code-behind of this .aspx, in my page_load I check a querystring flag to see if I performed that action..meaning user clicked that button. For example one of the querystring params is "AddItem" and another querystring param is "itemID". If movedItem is true, then I fire off a method called MoveItem(int itemID)
Method MoveItem is called
Method MoveItem redirects again back to this same .aspx using Response.Redirect(Request.Path).. this is so that the page load is hit again as well as my .ascx page load is hit. Because in both those page loads, I rebind a repeater so I can show the latest state of the lists. I call a method in my .aspx page_load which rebinds a grid and then page_load in my .ascx also calls another method which rebinds some other list
You can use $.get() to pass the variables to a server-side method that performs any server-side functions you need. You don't need to run page_load or have a code behind.
If you only need to update the HTML in the client's browser then you can use jQuery to add/remove them from the lists in the HTML. You can use the html() function in jQuery to append the item to the list.
You should look at DataTables.net as you can build a client side, editable grid that will perform Ajax updates as well. You would be able to keep your repeater control, but eliminate all the back and forth to the server and the deciphering of clicks and coordinating between Page_Load and etc. Here is a good post from Dave Ward(Encosia) that covers jQuery, Page_Methods and repeater controls.
If you want to preserve your work so far, why not try Ajax Update Panels around the region that you do not want to visible "refresh"?
Remember that the first A of AJAX is asynchronous. You'll want to avoid having your button reload the page, of course. Something like:
Figure out the data you need to send to your "add to shopping cart" handler page -- part number (SKU, etc.), colors, sizes, quantity, etc.
Ensure this data is on every applicable page, in an identical fashion: perhaps one or more hidden input elements, or even from the page URL.
Replace the custom control hyperlink/button href with a [client side] onclick handler instead, which will get the data from step 2 and send it via $.ajax() (or $.post or $.get), specifying the response handlers. You probably want to disable the button or give other visual feedback to avoid duplicate clicks.
In the response handlers, update the shopping cart section of the page with the number of items or indicate success or failure.

Resources