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.
Related
i have a page a three buttons, when user clicks 1st control i want to display 1st user control, for button2 second user control and so on... how can i achieve this in page_init as we dont knw which button got clicked
you can find the button ID with this trick in Page_Init event
string buttonID = Request["__EVENTTARGET"].ToString();
In buttonID in buttonID variable you will find the client ID of the button..
It depends on how you want to display them.
It is easier to have the controls hidden when not needed and only visible when required.
If you want to add them dynamically the easiest way to add them is to add the control to a placeholder when you need them.
eg
ASP
<asp:PlaceHolder id="myPlaceHolder" runat="server"></asp:PlaceHolder>
Code behind (C#)
myControl = new UserControl();
myPlaceHolder.Controls.Add(myControl);
But you also need to remember that to be able to access the control on postback you need to re-add it on submit/postback.
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'm using Register at the top of the page to register an ascx control, now the thing is I want to use this control twice on the page.
Now, the problem is when I hit buttons of one instance of the control, it fires the validation of both of the controls, and it obviously breaks, because it should be validating only one control - itself! The reason I am sure about this is because if I keep only one instance of the control on the page, then it flows nicely.
What I already tried that did NOT work:
1)Putting the two instances in different ASP Panels.
2)Registering the control twice at top of the page, so each registration has only one instance on the page.
I would not like to modify the validation of the control itself, but it's a huge project and it is being used at other places, and I do not want to disrupt other things. FYI It's using "Page.IsValid" to validate.
Set the ValidationGroup property of the validators and the ValidationGroup property of the buttons dynamically in the Page_Load method of the user control. You can use the user control's ID property as part of the ValidationGroup to differentiate between the two controls.
e.g.,
myRequiredValidator.ValidationGroup = "valGroup_" + this.ID;
myButton.ValidationGroup = "valGroup_" + this.ID;
I have a user control which contains two text fields each assigned with requiredfield validators this control contains another user control contains say a button .On click of this button i need to validate the fields from the parent control text fields.
I try the Page.Validate("ValidationGroup") with Page.IsValid it validates but the error message is not shown .Error message is shown only if i try to validate it from the contains which contains the required field validators ?
I ran into the same issue just now.
I solved it by adding a customvalidator beneath my reference to the usercontrol and validated the usercontrol from the parent by exposing the properties that required validation. I was exposing these properties anyway so no big deal there.
<div>
<uc:MyChildUserControl ID="MyChildUserControl1" runat="server"></uc:MyChildUserControl >
<asp:CustomValidator ID="MyChildUserControlCustomValidator" runat="server" ValidationGroup="default_validation" ErrorMessage="errormessage to show when the sh*t hit the fan" Text="*"></asp:CustomValidator>
</div>
And then the servervalidate code:
protected void MyChildUserControlCustomValidator_ServerValidate(object source, ServerValidateEventArgs args)
{
args.IsValid = MyChildUserControl1.SomeProperty;
}
And you have the ValidationGroup property set on all the buttons and TextBox's involved to the same validation group name? I've used the validationgroups in a few projects that have fairly deeply neested User controls and custom controls and as long as
ValidationGroup="CommonName" is set on the button being clicked and all the fields involved then the validation shows up properly..
I have a control on a page (let's say a button). I want to create such a user control, that will have a property TargetControlID, which takes an ID of control (button) on a page and on render will replace (hide) it with another button (a clone of a first button). Is it possible? Did anyone ever made such thing?
It's certainly possible, but I would suggest an alternate approach that will have fewer problems.
Instead of replacing the target control, just hide it by setting it's visibility to false. This way any processing that it does will still work, control ID's will remain static on rendering (automatically generated control ID's depend on order of controls), and it will have the same effect as you're looking for.
Yes, you can do what you're talking about. Create your user control in the usual way. Here's a link in case you're not familiar with that: Creating a User Control
Then create a property in the code-behind like this:
public string TargetControlID { get; set; }
Once it is a property, you can access it through the html:
<Azat:AzatButton ID="abtn1" runat="server" TargetControlID="btnOtherButton1" />
Then in the Page_Load() event handler, you instantiate a button based on this.abtn1.TargetControlID and remove abtn1 from the Page's Controls collection.
this.Controls.Remove(this.abtn1);