How to get the text from an link on the master page? - asp.net

I have a master page with a few links on it, not asp:Hyperlinks, just normal tags. The links are on a menu bar that runs along the top of the page.
Then on the child page, when I click a button, I want to be able to get the value of a specific link on the menu bar at the top of the screen on the code behind page.
Does anyone know if I can do this, and if so, how?
I'm using .net web forms.

You can use jQuery to access elements within the master page.
<script>
$(document).ready(function () {
//Some function for someID on your master page:
$("#someID").toggle();
});
</script>
Since the master page and child pages are rendered before the (document).ready method completes, it ensures that all elements built onto the final page are visible.
Placing the above script into your child page will allow you to access elements in the master page file.
You will just need to ensure you have a jQuery link/reference:
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js">
</script>
</head>
EDIT #1:
For getting the text off the master page into the code-behind of the child page you can do this (add hidden field to child page):
<asp:HiddenField ID="hdField" Value="SomeValue" runat="server" />
<script>
$(document).ready(function () {
//Some function for someID on your master page:
$("#hdField").value = ("#IDofLinkOnMasterPage").Value;
});
</script>
Then when your form posts to the child code-behind, you can look for the value of the hidden field by doing this:
var x = hdField.Value.ToString();

Related

Updating a page from another page without refreshing the page

I have two web pages written in VisualBasic.Net:
Upload.aspx
Default.aspx
There is a link in the Default page that opens Upload page in a different window.
In the Upload window I upload a file, and I want to display the name of this file in a textbox, which in turn is in a gridview on the Default page.
I think I want an asyncronous process that won't cause the Default page to refresh, but I don't know how to do this.
I created a very simple example for you. Here is the code of the first page (your Default.aspx let's say):
<html>
<head>
<script>
function ow() {
window.open('w2.html');
}
function update(updatestr) {
document.getElementById('update').innerHTML = updatestr;
}
</script>
</head>
<body>
open window
<div id="update"></div>
</body>
</html>
This page contains a link, which opens a new window (this will be your Upload.aspx page). It also contains a simple function called update, which puts a parameter value as a div html content.
This is code of a second page (your Upload.aspx like):
<html>
<head>
<script>
function update() {
window.opener.update(document.getElementById('txt').value);
}
</script>
</head>
<body>
<input type="text" id="txt" />
<input type="button" value="Update" onclick="update()"/>
</body>
</html>
This page contains a textbox and a button. After a button click, the content of the textbox will appear in a div on the first page. You can do something similar in your case.
I've put a working demo for you.

Prevent Drop down list from closing

I am using ajax toolkit -> Dropdown extender and have a gridview inside a dropdown list..
it is working quite well except for one massive problem.
Each time i do something(click on a control inside the gridview the dropdown closes)
i have to open it again to do the next thing.
For example:
open Dropdown
Click on textbox inside gridview
Dropdown closes
go to 1
Found here: http://www.dotnetcurry.com/ShowArticle.aspx?ID=167
You can open the drop down through javascript. Presumably when the user interacts with your control, as partial post back occurs through the update panel? If so, you might use this code in the PageLoad javascript event.
<body>
<form id="form1" runat="server">
<script type="text/javascript">
function pageLoad()
{
var d = $get('TextBox1');
d.click();
}
</script>
... ScriptManager and other Controls come here
</body>
</html>

Add .js files at the bottom of the page from User Control

I have a user control on my web forms app and I need to reference a js file on the user control.
I would like to add this js file at the bottom of the output html page but I don't know how to do it from User Control.
I have following script references on my master page;
<script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.5.2.min.js"></script>
<script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jquery.ui/1.8.12/jquery-ui.min.js"></script>
<script src="http://ajax.cdnjs.com/ajax/libs/modernizr/1.7/modernizr-1.7.min.js" type="text/javascript"></script>
and they are staying just after the </body> tag at the bottom. I'm using my user control like below on a web form page;
<userControl:SearchEngine ID="SearchEngine" runat="server"></userControl:SearchEngine>
and from this user control, I would like to add the a js at the bottom of the outputted html markup after the 3rd js file which you can see above.
any idea?
Try using RegisterClientStartUpScript() method to inject it at the end of the page:
http://msdn.microsoft.com/en-us/library/aa478975.aspx#aspnet-injectclientsidesc_topic2
Add a literal control at the bottom and set its text property with the content of the javascript file.

Redirect parent window from iFrame

I have an asp.net page inside an iFrame. I want to redirect the parent page (that sits outside the iFrame) when an asp.net button on the asp.net page in the iFrame is clicked.
The asp.net button will need to do some processing before re-directing, so I guess I need to call javascript from the asp.net
just create simple ans: in javascript
<script type="text/javascript">
function NewWindow() {
document.forms[0].target = "_top";
}
</script>
call it any control like button
<asp:LinkButton ID="linkbutton1" Text="Enroll Now" OnClientClick="NewWindow();" runat="server" onclick="enrol_Click"/>

Using JQuery in asp.net Masterpage

JQuery is not being able to identify tags when I use in the Master Page. The following code:
<script type="text/javascript">
$("body").append('<div id="test"><p>Hello</p></div>');
</script>
Works fine in normal pages, but when the body is in the master page and I put this same code in the Master page - nothing happens!
How can I append to the page body from the ASP master page? Is there some sort of trick to this?
Any help would be greatly appreciated.
Mark.
looks like you need to wrap this in a document.ready block that way it will happen when the page is ready.
Remember the ASP.NET page lifecycle (refer here for a reference). I think this worked in your base page and not in the master page because the page happened to be ready once the base page loads, not once the master page loads.
<script type="text/javascript">
$(function() {
$("body").append('<div id="test"><p>Hello</p></div>');
});
</script>
which is also the same as
<script type="text/javascript">
$(document).ready(function() {
$("body").append('<div id="test"><p>Hello</p></div>');
});
</script>

Resources