how to add attributes after finding master page controls on master page? - c#

i have some LIST controls in my master page to that list i want to add attributes such as href attribute from code behind of master page based on some criteria if he is admin or user etc.
i have tried
Control mycontrol=FindControl(v.Item1) ///v.Item1 im getting from database which is actual id of control in my aspx code
mycontrol.Attributes.Add("href","~/sales.aspx")/// this is not working
please help im new to asp.net

Just use HTMLAnchor or HTML Control
//Example 1
HtmlAnchor ct = (HtmlAnchor)FindControl("CRM1");
ct.Attributes.Add("href", "~/Test.aspx");
//Example 2
HtmlControl ct2 = (HtmlControl)Page.Master.FindControl("CRM2");
ct2.Attributes.Add("href", "~/Test.aspx");
Just tested. Both worked for me in my Master page

Related

How to access controls on a master page dynamically from another user control, if the web application has nested master page

I have a user control, say control 1, that looks for control2, which is placed on the root master page : Site.Master
And this is how I am getting Control2 from Control 1 now,
MasterPage showMaster = this.Page.Master.Master;
MasterPage siteMaster = showMaster.Master;
Control2= siteMaster.FindControl("Control2");
The above code works fine. But because our application uses nested master pages, I am running into a bit of situation here.
How do I find control 2 dynamically regardless of where I put Control1 in which template? Becasue right now, depends on where I put Control1, and how nested is that template in relation to the Site.Master, I have to change how far up in the chain I get Site.Master in the Control 1 code.
Any good ideas on how I can avoid doing that?
Please advise.
i found a work around with recursively loading the control

how to hide the master page content in a web page

I want to open a regular .aspx page in a rad window.But the regular page consists of Site Map.I don't want that sitemap to be displayed when it is opened in rad window.
Please suggest me some solution.
I already tried this:
this.MasterpageFile="..//test.Master";
But in my page it does not work
So you will have ContentPlaceHolders which will automatically inherit from the master page. If you go to the regular .aspx page and head into Design view you will see sections with all the content from the Master Page.
If you click the little right arrow to the right of the area that looks like a Div, you should be able to change the content back to what you have in your regular aspx page.
Make a public property on MasterPage to show hide your site map , on your rad window page access that Property and make it hide.
public bool MyProperty()
{
// get and set your controls visibility
}
Access your property like this.Master.MyProperty = false

How to load a .aspx page into contentplaceholder dynamically without postback?

I wanted to load a .aspx page into contentplaceholder without making a postback. What I have is a master page with 3 contentplaceholders
1.headerContent
2.leftContent
3.mainContent
I have 3 links in the headercontent and according to the selection of those 3 links, i'm showing few menu's on the leftcontent. On selecting any of the loaded menu on the left content i wanted to show/load a .aspx page inside the "maincontent" through the codebehind(C#), without making a refresh or postback. The .aspx pages which i wanted to load aren't inheriting the masterpage.
Other than using Iframes, is there any way to accomplish this??
What I can suggest is to use user controls (.ascx) instead of pages. Have one page that loads all the controls from the start, this page will use the master page and will have its content place holder as the mainContent.
You can have each control inside a div on the page, and set the display of the div to none. Then you can use java script to display just the relevant user control.

Dynamically add a User Control to a page without page postback

I want to add a user control to my pages without page post backs.
I can't use Update Panels because I have file uploads control in the user controls.
I also don't want to put the custom controls in a page and then load it inside a page using jQuery load functions. Similarly I cannot use iframes.
How can I do it through some jQuery or AJAX?
This answer is posted by some user but he/she has removed the answer:
So i am posting his answer
Though i want to add a server controls in my application but i left with no choice except to use HTML Elements
StringBuilder b = new StringBuilder();
HtmlTextWriter h = new HtmlTextWriter(new StringWriter(b));
UserControl u = new Page().LoadControl("~/UserControl.ascx") as UserControl;
u.RenderControl(h);
return b.ToString();
This works only if all the controls in a USER CONTROL is of html type

access dropdown control from the master page to the content page using asp.net

i have a master page and the content pages in the master page i have the textbox and dropdown
the value in the dropdown may vary according to the content pages
e.g for one content page the dropdown may contain
branchname,
city,
address
and let for other content page under same master page the dropdown may have values like
Contactnumber,
EmailID,
...........
.........
etc.....
so please help me to how can i bind that dropdown from my content page
thanks.
Please do as follows:
I have created a property in the master page and called that on the content page as:
public DropDownList SearchList
{
get { return ddlFilterText; }
}
and on the content page:
// place here the datasource through which the dropdown is biding
Master.SearchList.DataSource = null;
Master.SearchList.DataBind();
You could go and look for the dropdown control from your content page like this:
var yourdropdown = Page.Master.FindControl("ID of the dropdown") as DropDownList;
But be aware of that the masterpage's load event is succeeded by the page's load event so you dont double bind it.
If the values in the drop-down are dictated by the individual content pages, then my suggestion would be to store these in cache, or session, or some state-persistence mechanism.
The cleanest way to accomplish this is by adding a MasterType directive to each of your content pages.
<%# MasterType virtualpath="~/Masters/Master1.master" %>
Then from your codebehind in the content pages you can reference the Master Page controls like so:
DropDownList myDropDownList = Master.TheDropDownList;
Andreas is correct in that you need to be aware of the ASP.NET Event Life Cycle, so you actually bind the dropdown at the correct time.
It's pretty simple. You can 'find' the control from the content page.
Here's a sample where i'm binding to a DropDown control.
Master:
<asp:DropDownList runat="server" ID="ddlMaster" DataTextField="Name" DataValueField="Id" />
Content Page:
var ddlMaster = (DropDownList)Master.FindControl("ddlMaster");
ddlMaster.DataSource = dt;
ddlMaster.DataBind();
If you need to edit a textbox on the master page, you can also find that control and make changes to it from the content page.

Categories

Resources