Master Page label visibility in child page - c#

I want to make a label invisible in child page which is defined in master page.
Is there a way to do that?

((Label)Master.FindControl("mylbl")).Visible = false;
put this in page load of the child page , mylbl refers to the ID of the label
it might be Master.Page.FindControl .... now that I think of it, it's been a while , but that's how you do it

You should use javascript. Normally in this situation, you would reference your label using
(assuming your label's ID is my_label_id)
document.GetElementById('<%= my_label_id.ClientId %>')
. . . or if you are using jquery . . .
$('#<%= my_label_id.ClientId %>')
However, AFAIK you cannot use clientid to reference a server-side control located on the master page from a content page. So I would either give the control a unique class name using the asp.net label attribute CssClass="myLabelClass" or retrieve the client Id by building the page, viewing the source, and finding the client ID. Steps for this can be found here:
How to use javascript in content page, asp.net
Once you correctly reference the item, simply change the "display" style attribute to "none" as seen below.
Using jQuery and assuming your CssClass name is myLabelClass:
$('.myLabelClass').css('display','none');
If you wanted this to occur on page load you could do the following:
$(function(){
$('.myLabelClass').css('display','none');
});

Related

Can we use HttpHandler and HttpModule to change the content page div from the Master page?

In my application i create a navigation menu dynamically. When I click on the anchor tags that refer to various content page I want to capture the anchor tags' text and display it in a div in the content page.
How can i possibly achieve this?
Can it be done through HttpHandlers? or do I need to look into something else??? Please help.
Assuming that you want your div to look different or be positioned in different places for each content page and all you want to display in that div is the page URL you can solve this using a simple script that you can add in your master page:
document.onload = function()
{
var titleDiv = document.getElementById("titleDiv");
titleDiv.innerText = window.url;
}
The only constraint is that your div must have the same id in all the content pages.

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

ASP.NET set hiddenfield a value in Javascript

I don't know how to set the value of a hiddenField in Javascript. Can somebody show me how to do this?
Javascript:
document.getElementById('hdntxtbxTaksit').value = "";
HTML:
<asp:HiddenField ID="hdntxtbxTaksit" runat="server" Value="" Visible="false"> </asp:HiddenField>
error : "Unable to get value of the property \'value\': object is null or undefined"
Prior to ASP.Net 4.0
ClientID
Get the client id generated in the page that uses Master page. As Master page is UserControl type, It will have its own Id and it treats the page as Child control and generates a different id with prefix like ctrl_.
This can be resolved by using <%= ControlName.ClientID %> in a page and can be assigned to any string or a javascript variables that can be referred later.
var myHidden=document.getElementById('<%= hdntxtbxTaksit.ClientID %>');
Asp.net server control id will be vary if you use Master page.
ASP.Net 4.0 +
ClientIDMode Property
Use this property to control how you want to generate the ID for you. For your case setting ClientIDMode="static" in page level will resolve the problem. The same thing can be applied at control level as well.
asp:HiddenField as:
<asp:HiddenField runat="server" ID="hfProduct" ClientIDMode="Static" />
js code:
$("#hfProduct").val("test")
and the code behind:
hfProduct.Value.ToString();
First you need to create the Hidden Field properly
<asp:HiddenField ID="hdntxtbxTaksit" runat="server"></asp:HiddenField>
Then you need to set value to the hidden field
If you aren't using Jquery you should use it:
document.getElementById("<%= hdntxtbxTaksit.ClientID %>").value = "test";
If you are using Jquery, this is how it should be:
$("#<%= hdntxtbxTaksit.ClientID %>").val("test");
document.getElementById('<%=hdntxtbxTaksit.ClientID%>').value
The id you set in server is the server id which is different from client id.
try this code:
$('hdntxtbxTaksit').val('test');
I suspect you need to use ClientID rather than the literal ID string in your JavaScript code, since you've marked the field as runat="server".
E.g., if your JavaScript code is in an aspx file (not a separate JavaScript file):
var val = document.getElementById('<%=hdntxtbxTaksit.ClientID%>').value;
If it's in a separate JavaScript file that isn't rendered by the ASP.Net stuff, you'll have to find it another way, such as by class.
My understanding is if you set controls.Visible = false during initial page load, it doesn't get rendered in the client response. My suggestion to solve your problem is
Don't use placeholder, judging from the scenario, you don't really need a placeholder, unless you need to dynamically add controls on the server side. Use div, without runat=server. You can always controls the visiblity of that div using css.
If you need to add controls dynamically later, use placeholder, but don't set visible = false. Placeholder won't have any display anyway, Set the visibility of that placeholder using css. Here's how to do it programmactically :
placeholderId.Attributes["style"] = "display:none";
Anyway, as other have stated, your problems occurs because once you set control.visible = false, it doesn't get rendered in the client response.
I will suggest you to use ClientID of HiddenField. first Register its client Id in any Javascript Variable from codebehind, then use it in clientside script. as:
.cs file code:
ClientScript.RegisterStartupScript(this.GetType(), "clientids", "var hdntxtbxTaksit=" + hdntxtbxTaksit.ClientID, true);
and then use following code in JS:
document.getElementById(hdntxtbxTaksit).value= "";
Try setting Javascript value as in document.getElementByName('hdntxtbxTaksit').value = '0';

Making A Control Visible Through Javascript

I have a label and a div called "menu" that is currently invisible. I want that when the user clicks the label. It will make the div visible. I thought of doing it through javascript, how do I make a control visible through javascript?
First, if you want to access controls on the client-side, they must be rendered as html. When you use Control.Visible it won't be rendered on the client and is only accessible on the serverside. Therefore you have to use CSS to toggle it's visibility on the clientside.
show the div:
document.getElementById('menu').style.display = 'inherit';
You could hide it with:
document.getElementById('menu').style.display = 'none';
You should keep in mind that the id of serverside-controls could change when it's inside of an other NamingContainer than the page(f.e. in a GridView or UserControl). So you should use Control.ClientID to get the correct ID that'll be generated from ASP.Net:
So this is better:
document.getElementById('<%= menu.ClientID %>').style.display = 'none';
In ASP.Net 4.0 it's possible to customize the ClientID. For further informations:
http://www.thereforesystems.com/working-with-client-id-in-asp-net-4/
http://msdn.microsoft.com/en-us/library/system.web.ui.control.clientid.aspx

Calling ajaxWatermark extender in javascript

i have a Ajax Control toolkit water mark extender for a search box which is there in a master page . Based on certain condition ,i would like to change this value from the child page which inherits from this master page .I want to do it in client side with jquery /javascript . Any ideas??
You can create a javascript function that change the watermark, andd just call it from your child page
function setWatermarkText(text)
{
var watermark = $get('<%= WatermarkExtender.ClientID %>');
watermark.set_Text("John Smith");
}

Categories

Resources