Nested pages in ASP .NET? - c#

I have a page called
EditProject.aspx?id=xxx
I would like to invoke it wherever I want in a modal dialog. The modal dialog is simple with bootstrap.
I would just like to know if there is a control to invoke the page somehow in a div or modal dialog.
I know about IFrame, but is there a nicer more modern way with asp .net?
Thanks

You'd be better off moving EditProject.aspx to a user control, EditPorject.ascx.
Userconrols work much the same as aspx pages, supporting the same events, but you can embed them within ASPX pages like so:
<div id="edit-project-popup">
<namespace:EditProject ID="editProject" runat="server" />
</div>
You can still access the query string parameters from the usercontrol. You can also pass values to the usercontrol by adding a property:
public partial class EditProject : UserControl
{
public int ID
{
get;
set;
}
protected void Page_Load(object sender, EventArgs e)
{
// Your Code
}
}
You can then set this property in the ASPX pages markup:
<uc:EditProject ID="editProject" runat="server" ID="xxx" />
or in the ASPX pages code behind:
editProject.ID = "xxx";
Hope this helps.
For more information on user controls see this overview on MSDN:
http://msdn.microsoft.com/en-us/library/fb3w5b53(v=vs.100).aspx

Related

How to call a C# application from outside using JavaScript?

I have a C# application that uses WebForm. I am using the WebForm to display my application content (HTML / JavaScript).
My question is how do I communicate between them (API)?
Example: I like to minimize the program by using HTML buttons, etc....
If you have a web browser control on top of a normal control you could use the navigation event. E.g. make a link like:
Minimize
And in the navigation event of the browser control (I don't know how the event is actually called - just an example):
public void browser_OnNavigate(object sender, NavigateArgs e)
{
if (e.Target == "#MinimizeWindow")
// minimize and cancel event
else
// navigate to target
}
Local or Remote WebForms Application
You can use AJAX if you are trying to communicate with an external (or local) application.
Local WebForms Application
If by "application" you are actually referring to the back-end (code-behinds, etc.) of your WebForms project, then the other thing to look into are "server tags," otherwise known as "bee-stings." Here are just a few exsamples:
<% %>
<%-- --%>
<%# %>
<%= %>
Additionally, you can use event handlers for things like server-side button or anchor clicks, dropdownlist value changes, etc. You can make standard HTML controls server-side by adding the runat="server" attribute, or you can use .NET's WebControls (though they will still have to have the runat="server" attribute). Examples of these would be:
Front End
<button runat="server" onserverclick="btn_click">Click me</button>
...
or
...
<asp:Button runat="server" OnClick="btn_click">Click me</asp:Button>
Back End
protected void btn_click(object sender, EventArgs e)
{
...
}

How to programmatically add stuff to contentPlaceHolder?

I have a master page and all of my pages are inheriting it.
For formatting, I thought to place the content that differs from one page to another in a ContentPlaceHolder.
Now, how can I insert everything into that? Since I am planning to populate the ContentPlaceHolder with stuff from a database I suppose I will have to do it programmatically.
How can I add controls to ContentPlace Holder?
I checked other answers, but I cannot access it by its ID.
Should I use multiple ContentPlaceHolders from the beginning? Let's say I want to put movies. Should there be only one with all the images and descriptions and ratings, ore one ContentPlaceHolder for each thing?
I am opened to other solutions, as I have no experience with ASP.
Old question... but I just ran into this issue and this was the #1 post that kept coming up on Google, so figure I'd add my answer since the others didn't work in my case.
Here is how I did it when a regular <asp:Content wouldn't work (though in normal use, the answer #JayC is how you do it):
MasterPage has this ContentPlaceHolder:
<asp:ContentPlaceHolder ID="ScriptsPlace" runat="server"></asp:ContentPlaceHolder>
Had to dynamically add some JavaScript from a User Control. Trying to use the ContentPlaceHolder directly gives this error:
Parser Error Message: Content controls have to be top-level controls
in a content page or a nested master page that references a master
page.
So I wanted to add the script from the code-behind. Here is the Page Load for the .ascx file:
protected void Page_Load(object sender, EventArgs e)
{
ContentPlaceHolder c = Page.Master.FindControl("ScriptsPlace") as ContentPlaceHolder;
if (c != null)
{
LiteralControl l = new LiteralControl();
l.Text="<script type=\"text/javascript\">$(document).ready(function () {js stuff;});</script>";
c.Controls.Add(l);
}
}
UPDATE: So it turns out I had to use this in more places than I expected, and ended up using a way that was much more flexible / readable. In the user control itself, I just wrapped the javascript and anything else that needed to be moved with a regular div.
<div id="_jsDiv" runat="server">
$(document).ready(function() {
//js stuff
});
Other server controls or HTML junk
</div>
And then the code behind will find that div, and then move it into the ContentPlaceHolder.
protected void Page_Load(object sender, EventArgs e)
{
ContentPlaceHolder c = Page.Master.FindControl("ScriptsPlace") as ContentPlaceHolder;
HtmlGenericCOntrol jsDiv = this.FindControl("_jsDiv") as HtmlGenericControl;
if (c != null && jsDiv != null)
{
c.Controls.Add(jsDiv);
}
}
I actually put this code in a custom user control, and I just have my regular user controls inherit from the custom user control, so once I wrap the javascript/etc with a <div id="_jsDiv" runat="server">, the custom user control takes care of the rest and I don't have to do anything in the code behind of the user control.
What normally happens is
you set up your master pages with the proper html and ContentPlaceHolders
you create pages based off that master page. If you use Visual Studio, and tell it to create a new page based upon a existing Master page, it will add the Content areas for you.
you add things to the Content areas in the newly created page.
If you want to dynamically add controls to the master (or any) page, you could add controls to any existing control. If it shouldn't be wrapped in any way, just add a Placeholder (it is an asp.net control).
I did like this
<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
<asp:Literal ID="jsstuff" runat="server"></asp:Literal>
</asp:Content>
And this went into code behind:
string stuff = #"<script type=""text/javascript"">
var searchBox = 0;
var currentCountry = '';
</script>";
jsstuff.Text = stuff;
If the namespace for content Page and Master page is not same then the content page control not accessible in Codebehind in content page.
Also, check your designer files. if the control not listed in designer file then delete the file and recreate (project->convert to web application)

How do you call master page methods from a content page when the button is inside an update panel?

I have a masterpage and a content page. In the content page I have a script manager and an update panel. In the update panel I want to be able to click a button which would hit a public method on the master page to show a message. This works if I don't have an update panel on the content page but is there a way to get it to work when the button is in an update panel?
Master Page:
public void ShowMessage(string Message)
{
lblError.Text = Message;
lblError.Visible = True;
}
Content Page:
Master.ShowMessage("something");
I think it's a bit late , but for those who are looking for the solution,
Assuming your master page class like:
public MyMAsterPage: MasterPage
{
public void ShowMessage(string Message)
{
// DO SOMETHING
}
}
from your content page you can easily call any public method as follow:
(this.Master as MyMasterPage).ShowMessage("Some argument");
Function which define in masterpage:
public void Mesaj(string msj)
{
lbl_Mesaj.Text = msj;
}
Function which define in content page
protected void Page_Load(object sender, EventArgs e)
{
MasterPageWeb master = (MasterPageWeb)this.Master;
master.Mesaj("www.zafercomert.com");
}
You can call masterpage's function from content page like this.
I ended up just sucking it up and putting the script manager on the master page and putting the label on the master page inside an update panel.
<%# MasterType VirtualPath="~/masters/SourcePage.master" %>
Master.Method(); (in code behind)
You'll need to
this.button1 = this.Master.FindControl("UpdatePanel1").FindControl("Button1") as Button;
Here's a blog post to help describe the process. Basically you're drilling down into your controls.
EDIT
Sorry I just re-read your question. The code above will allow you to FIND a Button on your Masterpage from your Content Page codebehind.
It's a little more difficult to execute the Masterpage codebehind method from your content page. A better way might be to a JavaScript event to your button (or just use jQuery), and put the code for a JavaScript modal window in your Masterpage.
<!-- script stuff -->
<script>
$(function() {
$( "#dialog" ).dialog({
autoOpen: false
});
$( "#opener" ).click(function() {
$( "#dialog" ).dialog( "open" );
return false;
});
});
</script>
<!-- end script stuff -->
<!-- dialog div -->
<div id="dialog" title="Basic dialog">
<p>say something meaningful</p>
</div>
<!-- end dialog div -->
<!-- content page stuff -->
<button id="opener">open alert</button>
<!-- end content page stuff-->
HOWEVER
If you're really hooked on calling a masterpage method from your content page, you need to make the method public
Find info in Step 4: Calling the Master Page's Public Members from a Content Page
There are two ways that a content page can programmatically interface with its master page:
Using the Page.Master property, which returns a loosely-typed reference to the master page, or
Specify the page's master page type or file path via a #MasterType directive; this automatically adds a strongly-typed property to the page named Master.
Based on Amin's solution, i used an extension method to solve this more often.
public static T GetMasterPageObject<T>(this MasterPage masterPage) where T : MasterPage
{
return (T)masterPage;
}
Example:
this.Master.GetMasterPageObject<MasterPageClass>().MethodYouNeed("Parameters");

Call (show) a modal popup located in MasterPage from it's childs

I'm trying to make a default modal box that must be accessible from any part of the application, and need to be called whenever I want from inside any page. (must be called from code-behind).
So I came up with the idea of a Panel + modalPopupExtender placed in the MasterPage, and calling it from child pages via code-behind.
How can I do that? Or perhaps you guys have a better idea to solve this.
Since the modal is to be called from the code behind, you can achieve it like this
Add a method to your Master Page
public class MyMaster : MasterPage
{
public void ShowModal(string someParameter)
{
// Do your logic here
// Show the modal
}
}
Then add a method to your page, or page base like this...
public void ShowModal(string someParameter)
{
MyMaster masterPage = this.Master as MyMaster;
masterPage.ShowModal(someParameter);
}
I recommend using a base class for your pages so that you don't have to replicate the above method.
Add a method to your Master page. For example:
public void ShowMpSignup4free()
{
mpSignup4free.Show();
}
Then call this method from the code behind page like this:
protected void lbSignin_Click(object sender, EventArgs e)
{
MasterPages_WebMasterPage wm = (MasterPages_WebMasterPage)(this.Master);
wm.ShowMpSignup4free();
}
Here mpSignup4free is ID of ModelPopupExtender and MasterPages_WebMasterPage is name of master page (WebMasterPage is name of master page placed in folder MasterPages. That is why the complete name of master page is MasterPages_WebMasterPage).
and lbSignin is Link button on the page whose master page is WebMasterPage to whose click event will show the model popup.
For avoiding post back place the lbSignin link button in UpdatePanel...

ASP.net C# Silverlight server component and ajax modal inside firefox

I have a modal popup inside of an update panel with a silverlight control.
The video displays fine in IE 7/8 but in firefox all I get is a white screen
I am using the following video skin from link text
<div style="height:360px;">
<asp:Silverlight
ID="myVideoPlayer"
runat="server"
Source="~/Videos/VideoPlayer.xap"
Width="640px"
Height="360px"
InitParameters="m=Efficiency.wmv"
Windowless="true" />
</div>
I know it works when I use the normal <object> method but that will not work as I need to set the Initparams from the code behind depending on what video category they choose.
I have consulted the google gods and they have been not so helpful. Hope you guys can help me with this problem. Thank you!
I use Object tag, and init params injected to aspx like this :
<%= initparams %>
initparams is protected variable of my Page class:
public partial class _Default : System.Web.UI.Page
{
protected string initparams;
protected void Page_Load(object sender, EventArgs e)
{
initparams = "m=video.asx";
}
you can also change that variable in any event, it will be injected to rendered HTML.

Categories

Resources