We have an asp.net (2.5) application where we use one master page and a few hundred forms that utilize that master. We're creating a new set of forms and we want to submit the form on a button so that we can use all of the form values on the next page.
However, the form tag is in our master page and asp.net seems to only allow 1 form tag per page. Is there a way for me to change the target of the form post of the master from the child page or is there a way to add a second post?
You can change the target. In Masterpage add an id if it's not there already:
<form runat="server" id="form1" >
Now in the content page access the form and change it's target:
protected void Page_Load(object sender, EventArgs e)
{
HtmlForm myForm = Page.Master.FindControl("form1") as HtmlForm;
if (myForm != null)
{
myForm.Target = "_blank"; // _parent, _self or _top
}
}
I don't know how much it will help.
You might look here, I found it very interesting: Form Elements in ASP.NET Master Pages and Content Pages.
Do you need to access this second form from the code-behind on your new forms? If not there is a little hack you can do just add an end form tag and start a new tag that has the new target details
</form>
<form method="POST" action="blah.svc" >
//non-asp.net form
However to change the actual postback url you would change the form action via the PostBackUrl property on your buttons.
There is also Server.Transfer, kinda depends on what you are doing on your new forms.
Related
The following is the code I am using to display popup window of Telerik(Radwindow) in aspx page. It successfully displays the window with the below current code.
How can I display popup window from ASP.NET Usercontrol?
RadWindowManager windowManager = new RadWindowManager();
RadWindow window1 = new RadWindow();
window1.NavigateUrl = "Window1.aspx";
window1.ID = "RadWindow1";
window1.VisibleOnPageLoad = true; // Set this property to True for showing window from code
windowManager.Windows.Add(window1);
this.form1.Controls.Add(window1);
You can, of course, but there are two issues with this approach:
the user control will have to know about the window manager and window on the master page, traverse the controls hierarchy and find them
if you add the entire snippet to the user control you will end up with several window manager instance and this can play a few tricks on you (see here).
So, think about the following ideas:
add a RadWindow instance to the user control (not a RadWindowManager) and use that alone. Read this article to register a script from the server in order to open it and this article on making the JS functions you may need unique per user control.
open the RadWindow purely from the client-side as shown here. You can register a JS function from the server that will pass the parameters you need (URL, modality, whatever)
Here is a sample implementation of one of the ideas (that I would go with) based on your comment:
Master page
<telerik:RadWindowManager ID="RadWindowManager1" runat="server"></telerik:RadWindowManager>
<script>
function openDialog(url, modal, width, height) {
if (radopen) { //if not, there is no RadWindowManager on the page, add an else{} block to use window.open() or other logic
var wnd = radopen(url, null);
wnd.set_destroyOnClose(true);
//add checks here in case parameters have not been passed
wnd.setSize(width, height);
wnd.center();
wnd.set_modal(modal);
}
}
</script>
User control markup
<asp:Button ID="Button1" Text="open RW" OnClick="Button1_Click" runat="server" />
Use control code-behind
protected void Button1_Click(object sender, EventArgs e)
{
bool flag = true;
if(flag)
{
string script = string.Format("function f(){{openDialog('{0}', {1}, {2}, {3});Sys.Application.remove_load(f);}}Sys.Application.add_load(f);",
"the-page.aspx",
"true",
600,
400);
ScriptManager.RegisterStartupScript(Page, Page.GetType(), "someKey", script, true);
}
}
I have a master page which is being inherited to a lot of content page. I have one particular page in which I do NOT want one of the user controls present in the master page. I do NOT want to create a separate master page for this. I want to know if there's any way to prevent the user control present in master page being inherited to child / content page. Appreciate your help. thanks !
In the user control,
protected void Page_Init(object sender, EventArgs e)
{
Css.Register(this, "/css/reset.css", BrowserTarget.All, true);
}
In the master page, the user control is added
The part (css in reset.css) I'm trying to remove / prevent being inherited to
the content page
ol, ul {
list-style: none;
}
You can use in the child control:
((UserControl)this.Master.FindControl("userControlName")).Visible = false;
In order for the control to never exist at all with what you're saying and your feedback that hidden doesn't fit your needs, I'm guessing removing the control won't either.
The only other option here is to change the master page to have a flag that loads the control. This changes the approach from "remove this if" to "include this when." Then, in your calling pages, you'll need to set that flag. To make this new change less invasive, set the default the true and in this page set it to false.
Try this
MasterPage master = this.Master;
UserControl userControl = (UserControl)master.FindControl("userControlName");
if(userControl != null)
{
master.Controls.Remove(userControl );
}
I have a user control which requires Javascript/Jquery per control. It is actually a control to represent data graphically using some javascript library. As a norm all my javascript links are located at the bottom of the page in the Master Page. This implies I cannot write any javascript in the control because it will appear before any of its dependencies. Also, Even if the script is located at the bottom of the page, it only works for the first control. Has anyone encountered similar challenges? I'm looking for a way out. Thanks
You can register client scripts from code-behind.
var clientScriptManager = Page.ClientScript;
if(!clientScriptManager.IsStartupScriptRegistered("a_key")) { //If multiple control instances are present on the page, register scripts only once
RegisterStartupScript(Page.GetType(), "a_key", "<script src=\"/js/a_library.js\"></script>"));
}
RegisterStartupScript will add <script> tag at the very bottom of the page, before </body> closing tag.
I had a similar issue "updating" a legacy site that had tons of inline JS... so I also ran into issues when I moved jQuery and other scripts to the bottom of the page. I solved it by adding a new ContentPlaceHolder to the master page, underneath the other script references:
<asp:ContentPlaceHolder ID="ScriptsPlace" runat="server"></asp:ContentPlaceHolder>
</body>
Then went through the pages and moved all the inline JS into a content block for ScriptsPlace.
<asp:Content ID="Content5" ContentPlaceHolderID="ScriptsPlace" runat="server">
<script>
//some awesome JS
</script>
</asp:Content>
Although this doesn't work for user controls... so I made a somewhat hacky solution that essentially involved putting all of the user controls JS into a div named _jsDiv and then from the code-behind moving that div into the placeholder (I'm not fond of RegisterStartupScript and it's not practical for a lot of code).
Since I did this in multiple controls, I did these steps:
Step 1 - Make a custom user control, ScriptMoverUserControl.cs:
public class ScriptMoverUserControl : System.Web.UI.UserControl
{
protected void Page_PreRender(object sender, EventArgs e)
{
ContentPlaceHolder c = Page.Master.FindControl("ScriptsPlace") as ContentPlaceHolder;
HtmlGenericControl jsDiv = this.FindControl("_jsDiv") as HtmlGenericControl;
if (c != null && jsDiv != null)
{
jsDiv.ID = Guid.NewGuid().ToString(); //change the ID to avoid ID conflicts if more than one control on page is using this.
c.Controls.Add(jsDiv);
}
}
}
Step 2 - Make your user control use this new control class:
It will inherit ScriptMoverUserControl instead of System.Web.UI.UserControl
public partial class SomeGreatControl : ScriptMoverUserControl
Step 3 - Dump your user control scripts in a div named _jsDiv:
<div id="_jsDiv" runat="server">
<script>
//some awesome JS
</script>
</div>
It's been working fine for me, but if anyone knows a better/cleaner way, I'd like to know!
I have a Update Panel inside a User Control i use on 2 pages in my website
Both pages use the same MasterPage, ScriptManger is declared in the MasterPage.
Both pages call the UC the same way:
<uc:SearchCube runat="server" ID="searchCube" />
in the Update panel i have many RadioButtons that on change generate a server side event that fill dropdown in the update panel and update the panel
protected void SearchCategoryChanged(object sender, EventArgs e)
{
FillDropdowns();
SearchOptions.Update();
}
Update Panel is set like this:
<asp:UpdatePanel ID="SearchOptions" runat="server" UpdateMode="Conditional"
hildrenAsTriggers="true"/>
Each RadioButton is set like this:
<asp:RadioButton ID="RadioButton1" GroupName="SearchCategory" runat="server"
AutoPostBack="true" OnCheckedChanged="SearchCategoryChanged" Text="Text"/>
I also have an AsyncPostBackTrigger on each Radio Button Controller
The problem i have is that on one page when i call the Update() function the panel is updated and Page_Load is triggered which causes the UC to refresh and reload the default settings of the UC
I can see in DEBUG mode that on the working page Update() does not generate Page_Load.
Can anyone explain to me why this is happening?
Everytime a request goes to the server, it executes the Page_Load event.
What you need to do is make sure you have a PostBack validation on all your pages:
protectec void Page_Load(object sender, EventArgs e)
{
if(!Page.IsPostBack)
{
//Not a postBack: Normal page load
//Init your page here
}
else
{
//It's a PostBack (from a command).
//Do nothing or init stuff your all your commands.
}
}
If you put some breakpoints in your Page Load and your SearchCategoryChanged method, you'll be able to see what the pipeline looks like.
Fixed my problem.
the problematic page is an index page that takes a few parameters in.
I have a Response.Redirect() on the page to avoid duplication of pages.
Apparently when the PostBack() is made it calls the page without any parameters and i was forcing it to be redirected into a default view since no parameters were sent to the page.
i found a lead to my problem in a Microsoft help forum that stated:
By calling Response.Write() directly you are bypassing the normal
rendering mechanism of ASP.NET controls. The bits you write are going
straight out to the client without further processing (well,
mostly...). This means that UpdatePanel can't encode the data in its
special format.
Anyway the page was reloading every time which caused it to reload the User Control with it's default values.
What would be the best way to call a method in the code-behind of parent page from the code behind of the child page in the ASP.NET 2.0 Web Site model?
Scenario: User clicks a link in the parent page to view a data record's detail in child page (The parent and child are tow seperate pages). The user will modified the data in the client page. Once the child page has processed the update, the child page will close and I need to rebind the parent page to reflect the update.
I did something similar, but it was calling the parent page from a user control that was included in the parent page
if(Page.GetType().ToString().Contains("ASP.Default_aspx"))
{
MethodInfo MyMethod = this.Parent.TemplateControl.GetType().GetMethod("MyMethod");
MyMethod.Invoke(this.Page, null);
MyMethod = null;
}
UPDATE: I have to do this from the code behind because the child page closes after the data has been updated.
Easiest way would be to do the whole thing on one page using a multiview or some such thing.
Other then that, with javascript you can call
document.opener.location.href = "url";
to change the url on the parent page. You could just keep it the same, or stick stuff in the query string and muck with those values on Page_Load
If you're opening the child page in a modal pop-up, you can access window.returnValue on the parent page (via JavaScript) and then invoke a page refresh or an Ajaxy rebind call.
Check this page out for how to return value from modal pop-up and do something based on that value on parent page.
However, if you have the option, I'd get away from opening the edit form in a separate page. I'd put the edit form in a user control and show/hide it a la lightbox style.
Not sure if this is exactly what you want but you could have used cross page posting for this. This allows you to post back to the parent page from the child page with the advantage of having access to the child page's ViewState. To do this you set the PostBackUrl property of a button to the child page. In the parent page you can then access the PreviousPage property to retrieve values from the child page. So in the child page have a button such as:
<asp:Button ID="Button1" runat="server" Text="Update data"
PostBackUrl="~/Parent.aspx" onclick="Button1_Click" />
Then in the Parent.aspx Page_Load event:
protected void Page_Load(object sender, EventArgs e) {
if (IsCrossPagePostBack) {
Page prevPage = this.PreviousPage;
string myVal = prevPage.FindControl("myTextBox1").ToString();
string myVal2 = prevPage.FindControl("myTextBox2").ToString();
//etc
}
}