How to get value of a control in a Masterpage using JQuery? - c#

I have a hidden field control in a MasterPage and I want to get the value of the hidden field control using JQuery in a page that uses the MasterPage.
I have the following javascript which exectues if the hidden field in the page has a value:
if(!$('input[type=hidden]').val().length == 0 ) { }
What javascript would I need to check the value of a hidden field in the MasterPage from the page?

There's no seperation between your page and the master page. Both of those concepts are in your ASP.NET layer and the browser simply recieves one HTML document.
If your masterpage specified a hidden input it'll be on your page like any other hidden input.

If that has the affect you want, it should also work in the masterpage, as there is no difference on the client between a master and a content page.

The master page just gets rendered down with the child page as a single HTML, so you would just access it client-side as normal.
Try rewriting this:
if(!$('input[type=hidden]').val().length == 0 ) { }
as
if ($('input[type=hidden]').val()) {}
which is a simpler conditional for if the hidden field has a value. I'm not sure that the ! combined with the == is doing what you want it to do logically. Either way $('input[type=hidden]').val() is more readable IMO.

Related

Update ContentPlaceHolder with Ajax?

Say i have a MasterPage and two subpages, that are included through the ContentPlaceHolder.
Would it possible in AJAX to update the ContentPlaceHolder to change from 1 subpage to another?
And if so, are there any problems that i may encounter by using this type of interface?
You can't use ContentPlaceHolder control, since it will not be rendered in your page.
Please use div runat="server" and use div id to load the ajax content
All the scripts in the page will work fine loaded via ajax. There are few scenarios it will break/conflict with the parent page script. In that case, you can use iframe and set a src attribute

How to call a method only once in the first page load in the master page

I have the following case:
Page1.aspx this page has the master page master.aspx.
I have some code in the master page :
if (!Page.IsPostBack)
{
adjustServiceBar();
}
when i click any button in the Page1.aspxit enters the !Page.IsPostBack and execute the method !!
i want this method in the !Page.IsPostBack) only
One way to do this is to set a session variable and then check that variable to ensure your code will fire only once.
Another way is to set a hidden control on your form and play with its text or value property.
According to each scenario the solution may be very complex such as custom derived masterpages and pages that extend the current events functionality to suit your needs.
I believe it is more consistent to check for IsPostBack in content page. You can move this condition to Page1.aspx and expose adjustServiceBar() method in your master page, so that content pages can call it, like Master.adjustServiceBar().

How to write value to html field from asp.net code behind

I have made an application in javascript using HTML fields in asp.net, as asp text boxes were disturbing the ids, so i used html fields, which are working fine with javascript, now i want to fetch database table columns on page load, and want to assign to html fields, what is the best way to do so? Help me!!!!
You could go back to using the ASP TextBoxes and access the ids in JavaScript as follows:
<%= IDofTextBox.ClientID %>
It's probably the easiest as naturally they can then be accessed in the code behind very easily.
you can use asp text boxes fine if you grab a reference in your javascript to their asp.net generated ID via <%= textboxname.ClientId %>
This is not the right way to do it (I wouldn't recommending it), but if its what you need, then it will work.
Add method="post" action="<your path here>" to your form element and when the submit button posts, you will be able to access all the form variables like so:
string txtName = Request["TextBox1"] ?? string.Empty; //replace textbox 1 with text box name
Just be sure to replace the action in form to your page etc..
But really, going back to <asp:TextBox... will save you a lot more time and as Ian suggested, you can access them with javascript by the server tags <%= TextBox1.ClientId %>
ps: also, the ?? is a null coalesce character. its a short form of saying
if(Request["textbox1"] != null)
txtName = Request["textbox1"];
else
txtName = "";
If I understand you correctly. You just need to add runat="server" and id="someName" to the html fields and access them in the code behind by its given id.

Remove statically added controls at runtime

The Scenario: I have an asp.net website where I show a div popup on page load for taking a few user details. When a user inputs the details, or closes the popup, I set up a flag cookie so that the popup is not displayed again for the user. The div is in the MasterPage so that it is displayed no matter on which page a user lands first time. The div contains an UpdatePanel which has all the controls required for taking the details. This whole functionality is working fine.
The Problem: Now this div popup is not showing(by setting display:none) on subsequent postbacks(which I want), but the html markup is still loading with the page unnecessarily adding to the page size. What I would idealy want to do is: Check if flag cookie is set. If no, show the popup, else remove the popup's markup from the page.
Now since the div is not a server control, I cannot possibly remove it and the all the controls inside it. So, I thought of removing the UpdatePanel from the page:
protected void Page_Load(object sender, EventArgs e)
{
if (Request.Cookies["flag"] != null)
{
if (Page.Controls.Contains(updpnl_contact))
{
Page.Controls.Remove(updpnl_contact);
updpnl_contact.Dispose();
}
}
}
But I guess this tends to work with dynamically added controls only, and since the control is added at Design Time, it is not being removed.
Is there any way I can achieve this?
If you add a runat="server" attribute to your <div> element, it will be available in the code-behind. You'll need an id on it as well. Then you can just toggle the Visible property. If this property is false, the control won't be rendered to the client (i.e. no HTML markup).
What you're trying to do is not at all the usual workflow. I tend to think that it will not work as it would mess up control tree, maybe even corrupt the viewstate and so on.
As a possible solution, you can put it's visibility to hidden in the code behind. This, in the contrary to the usual 'gut feeling', doesn't work like the css propery 'display:none' for example - instead the control will not even be rendered into the page when it's not visible. This may be the workaround for you.
Happy coding.
A more efficient approach would be to create the panel as a UserControl and load it dynamically in codebehind when it's needed, then add it to your page. E.g, in code:
MyPopupControl popup = (MyPopupControl)Page.LoadControl("/path/to/usercontrol.ascx");
PopupPanel.Controls.Add(popup);
Where PopupPanel is an empty <asp:Panel>. Then, not even the markup will need to be loaded/processed except when its needed.
There is no reason that all the code you use to display and process this panel couldn't also be in the usercontrol, isolating it from the master page.
Can you build the panel dynamically, based on the cookie setting?

ASP.NET User messages on MasterPages

i have an admin master page where i want to place a label which i can control with two functions (setErrorMessage and setSuccessMessage) both functions assign strings to label's text property and change CssClass property according to function type.
I want to use these functions from nested pages while the control remains centralized on master page so i can return to form in case of error so user could edit wrong input.
How would you suggest me to do that ? either VB or C#
thanks
You can use below in .aspx
<%# MasterType VirtualPath="~/MasterPages/Default.master" %>
and below in your code behind,
this.Master.yourMethod
this.Master.yourProperty
to access your controls in child page.
you must convert type of Master property in nested page:
((MyMasterPage)this.Master).lblMessage.Text = "Hi.";

Categories