accessing content page controls - asp.net

i'm using following code to access controls inside content page from master page
Button btn = (Button)ContentPlaceHolder2.FindControl("btnProceed");
btn.Text="test";
and it does finds the control inside content page and runs with out exception.but the button text doesn't change.in the content page btnProceed Text field is set to "Proceed".what i need is when i click on a imageButton on the master page content page btnProceed button text should be changed to "test" which is currently not happening.what's the reason for this issue?

you could try like this...
Button btn= Master.FindControl("ContentPlaceHolder2").FindControl("btnProceed") as Button;
btn.Text ="test";

the button on content page is created by markup or at run time?
if its in markup, the following code is working fine..
Its the image button click handler on Master page
protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
{
Button btn = ContentPlaceHolder1.FindControl("Button1") as Button;
btn.Text = "Proceed";
}
if we have in content page.aspx something like:
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<asp:Button ID="Button1" runat="server" Text="Button" />
</asp:Content>

Related

Opening Page In New Tab Updates Both Tabs

I am working with two files, Default.aspx and Beta.aspx.
In Default.aspx I have a button that navigates to a Beta.aspx
<telerik:RadImageButton ID="btn26" runat="server" Skin="Material" Text="26" OnClick="btnConfirm_Click26">
</telerik:RadImageButton>
protected void btnConfirm_Click26(object sender, EventArgs e)
{
string url = string.Format("Beta.aspx?year={0}&track={1}&event={2}&car=26&session{3}", hidYear.Value, hidTrack.Value, hidEvent.Value, hidSession.Value);
Response.Redirect(url);
}
I have the Beta.aspx form tag set up like so in order to force the page to load as a new tab:
<form id="form1" runat="server" class="SmallFont" target="_blank">
The button works, and loads the page in Beta.aspx as a new tab like I had hoped for, however the Default.aspx page that the button resides on also navigates to the same URL. I want Default.aspx to always stay on the same page, what am I missing here?
To open a new tab, there is no need to do a server round-trip. You need to move your button onto a separate html form, skip the aspx code and handle the click event using (client-side) JavaScript, like this:
<form id="form2" class="SmallFont" action="Beta.aspx" target="_blank">
<input type="submit" ID="btn26" Skin="Material" Text="26" />
<input type="hidden" name="year" value="{0}" />
<input type="hidden" name="track" value="{1}" />
</form>
or this: (which might not work as-well because of popup blockers)
<input type="button" ID="btn26" Skin="Material" Text="26"
OnClick="window.open('Beta.aspx?year={0}&track={1}&event={2}&car=26&session{3}', '_blank');" />
If you don't like the old-school JavaScript, there are more-modern ways, with jQuery or other frameworks, etc. HTML button onclick event
This should be a pretty easy problem, let's put few more details out here.
There is a form on default.aspx
<form id="form1" runat="server" target="_blank">
<asp:Button ID="btn26" runat="server" OnClick="btnConfirm_Click26" Text="26" />
</form>
The target is _blank, i.e. the new window, or tab will be opened.
The code of btnConfirm_Click26() of default.aspx has redirect
protected void btnConfirm_Click26(object sender, EventArgs e)
{
string url = string.Format("Beta.aspx?year={0}&track={1}&event={2}&car=26&session{3}", 0, 1, 2, 3);
Response.Redirect(url);
}
i.e. the form of default.aspx in first tab will be submitted to a new window to the same default.aspx and then redirected (in same second tab) to beta.aspx.
default.aspx (in first tab)
--> default.aspx (in new tab) --> redirects to beta.aspx
If you have no code in Page_Load() of default.aspx, it should "stay" as-is in the first tab and will not "navigate to beta.aspx". If you have code in Page_Load(), make sure you use if (!Page.IsPostBack) to execute code only once when default.aspx is loaded (and not when it is redirected to beta.aspx).
As mentioned earlier, redirect does not require to be executed on server and can be done via client script too.

Load specific part in webpage?

See below image first.
we have one sidebar navigation(ajax accordion control asp.net)
now when ever user click on link inside side bar related page(content) should display in Content goes here region.
As per given instruction entire page should not be refreshed or in other word in Back Button should not work(In Internet Explorer).
what should be the way to achieve this functionality?
what should be the best suggestion for that?
EDIT: navigation tree is inside MasterPage and Content goes region is in side content page of master page
please suggest me.....
thank you so much....
The Easiest way is to Wrap your side navigation & the Content placeholder in an UpdatePanel. Set the TreeView in the side bar as the UpdateTrigger for the update Panel. But, this approach is a little inefficient.
A slightly better way is ti just wrap the Content Placeholder in an Update Panel, along with a HiddenField in it. Upon a selection in the sidebar, update the HiddenField Value with JavaScript and then refresh the update Panel.
According to:
As per given instruction entire page should not be refreshed or in other word in Back Button should not work(In Internet Explorer).
And
sidebar tree view is in master page and Content goes here region is content page
If my understanding is correct, I think you do not need to place your TreeView control in your master page because you only want one page loading dynamically the content based on the selection of your tree view. So...Why is this important? Well if you place your tree view in your page you can use an UpdatePanel to avoid full posts.
Output of the following code
The following code covers the next points:
A TreeView control is embedded in a UserControl and placed in an ASPX page (left side)
The menu contorl exposes an event that is raised whenever the selected node changes, this event is handled in the ASPX page to dynamically load user controls depending on the user selection on the right side of the page, only one content is loaded at a time.
The controls are embedded in an UpdatePanel therefore you won't change your page and your back button in your browser won't be affected
Note: the user controls keep their state across post backs
(I'm not sure if this is the best way to do it, perhaps you could try to find a solution using only ajax, and avoid the use of the evil updata panels, but certainly this is a way to do it)
I'll try to simplify the code to reduce the size of the post, I will just post the code of one user control, the other one is exactly the same I just changed its title to difference them on the page
ASCX Menu
<asp:TreeView ID="TreeView1" runat="server" onselectednodechanged="Unnamed2_SelectedNodeChanged">
<Nodes>
<asp:TreeNode Text="link1" />
<asp:TreeNode Text="link2" />
</Nodes>
<SelectedNodeStyle Font-Bold="True" Font-Italic="True" />
</asp:TreeView>
ASCX Menu code behind
public event Action<string> MenuChanged = delegate { };
protected void Unnamed2_SelectedNodeChanged(object sender, EventArgs e)
{
this.MenuChanged(this.TreeView1.SelectedNode.Text);
}
ASPX
<asp:ScriptManager runat="server" ID="sm" />
<asp:UpdatePanel runat="server" ChildrenAsTriggers="true">
<ContentTemplate>
<asp:HiddenField runat="server" ID="currentControl" />
<table border="0" cellpadding="0" cellspacing="0" width="90%" align="center">
<tr>
<td style="width:50%; background-color: Silver">
<menu:TreeViewMenu runat="server" ID="myTreeViewMenu" OnMenuChanged="myTreeViewMenu_MenuChanged" />
</td>
<td style="width:50%; background-color: Aqua">
<p>Result:</p>
<asp:Panel runat="server" ID="myPanel">
</asp:Panel>
<asp:Label ID="lblMessage" runat="server" />
</td>
</tr>
</table>
</ContentTemplate>
</asp:UpdatePanel>
ASPX code behind
protected void Page_Init(object sender, EventArgs e)
{
if (this.IsPostBack)
{
var cc = this.Request.Form["currentControl"];
if (!string.IsNullOrWhiteSpace(cc))
{
var uc = this.LoadControl(this.Server.HtmlDecode(cc));
this.myPanel.Controls.Add(uc);
}
}
}
protected void myTreeViewMenu_MenuChanged(string e)
{
this.myPanel.Controls.Clear();
switch (e)
{
case "link1":
var cc1 = "~/Content1.ascx";
this.currentControl.Value = this.Server.HtmlEncode(cc1);
var uc1 = this.LoadControl(cc1);
this.myPanel.Controls.Add(uc1);
this.lblMessage.Text = "Updated from: link1";
break;
case "link2":
var cc2 = "~/Content2.ascx";
this.currentControl.Value = this.Server.HtmlEncode(cc2);
var uc2 = this.LoadControl(cc2);
this.myPanel.Controls.Add(uc2);
this.lblMessage.Text = "Updated from: link2";
break;
default:
this.lblMessage.Text = "Updated from default: " + e;
break;
}
}
ASCX
<h1>Content 1</h1>
<asp:TextBox runat="server" ID="txt" />
<asp:Button Text="Process data..." runat="server" OnClick="button_Click" />
<asp:Button Text="Just post" runat="server" />
<asp:Label ID="lblMessage" runat="server" />
ASCX Code Behind
protected void button_Click(object sender, EventArgs e)
{
this.lblMessage.Text = this.txt.Text;
}
You can simply copy-paste this code to test it yourself, this should work
Jupaol's answer works fine but 1 thing need to mention, I came across the problem after implemented Jupaol's idea, the first time I called the user control immediately after I click menu, the button with in the ascx works fine, but if I switch to 2nd one, first click of the button on the 2nd control will not fire on first click, this is because we do not have a "static" ID of the control. It took me almost 3 days to finally figure out why this is happening. so here's part of my code to make. I'm leaving this message in hope that anyone who read this afterwards will make the use of it.
if (!string.IsNullOrEmpty(controlPath))
{
PlaceHolder1.Controls.Clear();
UserControl uc = (UserControl)LoadControl(controlPath);
/**note below LastLoadedControl is anything that could
* be unique to the called control so every time when call back
* it will not confuse the back end so the first fire of eg. a button
* on that loaded control will work
*/
uc.ID = LastLoadedControl;
PlaceHolder1.Controls.Add(uc);
}
I'll also need to thank Jupaol's great contribution so that I can get my site running.

loading an ASPX page into a jQuery UI Tab with AJAX

I have some experience with jquery and regular html pages but only just started learning .net. I have searched everywhere and I cannot find a good tutorial or example of how to load aspx pages into jquery ui tabs with ajax. I have a Main.aspx page which has some jquery ui tabs for navigation. I would like to load the content for each tab with ajax. I have tried to use the Ajax mode of jquery ui tabs but it seems like when the aspx page contains certain web controls it does not load for some reason. The aspx file that I want to load into the tab only has a button control that, when clicked, changes it's text to say "hello".
Here is the tab section of Main.aspx:
<div id="tabs">
<ul>
<li><span>Test</span></li>
<li>Second</li>
<li>Third</li>
</ul>
<div id="tabs-2">Second Tab</div>
<div id="tabs-3">Third Tab</div>
</div>
Here is the the test.aspx code:
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="test.aspx.cs"
Inherits="TropicalServerGUI.test" %>
<div>
<asp:button ID="btn" runat="server" text="Button" />
</div>
and its code-behind:
namespace TropicalServerGUI {
public partial class test : System.Web.UI.Page {
protected void Page_Load(object sender, EventArgs e) {
}
protected void btn_Click(object sender, EventArgs e) {
//btn.Text = "hello";
}
}
}
the second and third tabs which are static work fine but nothing gets loaded into the first tab. If I were to remove the button control and put, for example, <h1>Hello World</h1> then it loads the page correctly. I know I am doing something completely wrong and I cannot find any website that addresses this topic, so any help with this would be greatly appreciated!
Are you initialising the JQueryUI tabs?
<script>
$(function() {
$( "#tabs" ).tabs();
});
</script>

Disabling Update panel from content page

Hello I have a ToolkitScriptManager and update panel on master page. I want to disable update panel functionality on a content page but it should work fine for other pages.
Master Page Code:
<ajax:ToolkitScriptManager runat="server" ID="sm1" EnableScriptGlobalization="true"
EnableScriptLocalization="true" ScriptMode="Release" CompositeScript-
ScriptMode="Release" />
<asp:UpdatePanel ID="udpEmail" runat="server">
<ContentTemplate>
<asp:ContentPlaceHolder ID="cphMain" runat="server">
</asp:ContentPlaceHolder>
</ContentTemplate>
</asp:UpdatePanel>
Content Page Code:(e.g. Page1.aspx)
<asp:Content ID="Content3" ContentPlaceHolderID="cphMain" runat="Server">
Code Here
</asp:Content>
So Now I want to that Update panel functionality should not work on Page1.aspx but it should work on other content pages of same master page. Please help
what about on the code behind to check for the name of the page and set the visibility of cphMain to false? http://forums.asp.net/t/1163743.aspx
Have you explored Razor + MVC yet?
On the page where it should be disabled, try something like this:
UpdatePanel panel = Page.Form.FindControl("UpdatePanel1") as UpdatePanel;
if (panel != null)
{
panel.Enabled = false;
}
Depending on where the UpdatePanel is located in the master page, you may need to search the page recursively, like this:
private void DisableControl(Control parentCtrl, string controlID)
{
foreach (Control ctrl in parentCtrl.Controls)
{
if (ctrl.ID == controlID)
{
((WebControl)ctrl).Enabled = false;
continue;
}
DisableControl(ctrl, controlID);
}
}
You can either cast Master property on content page to correct masterpage type, or use
<%# MasterType %>
to have typed master page.
Then you will have access to controls on it and can disable update panel.
I haven't found a way yet better than the below, To override the onInit and add that code
protected override void OnInit(EventArgs e)
{
//disables the ajax effect, and allows postback
ScriptManager.GetCurrent(Page).EnablePartialRendering = false;
base.OnInit(e);
}

Page Validation, Submit Button is not part of the usercontrol

I have some fields to be validated on the server (CustomValidator) which is in a separate user control and my Submit button is in the page not in the user control.
What I have done is, I placed all my Validation Methods in the code behind of my usercontrol (ascx.cs) and the page validation is in the code behind of the page (aspx.cs). I put them in the same ValidationGroupName (fields and Submit button). usercontrol (ascx) is a child of my page (aspx). I have already placed CauseValidation="true" to my Button and UseSubmitBehavior="true". The problem is it does not validate. What could be the problem in this?
Note: I cannot put the the Button to be part of the usercontrol.
Edit:
in my aspx page, click event of the button has this.Page.Validate(ValidationGroupName) and all of my validators are on the fields on a separate control (ascx) which is a child of the aspx page.
protected void Button1_Command(object sender, CommandEventArgs e)
{
if(e.CommandName.Equals("Validate", StringComparison.Ordinal))
{
this.Page.Validate("MyValidationGroup");
if(this.Page.IsValid)
{
// I'll change my View here.
}
}
}
My Button looks like
<asp:Button ID="Button1" runat="server" UseSubmitBehavior="true" CommandName="Validate"
Text="Submit" OnCommand="Button1_Command" CausesValidation="true" ValidationGroup="MyValidationGroup" />
The above snippet is located in my aspx page.
I've tried to put the same button on the ascx page, it works fine. My thought is since the ascx page is under the aspx page. When the button event on aspx page is fired (Button1), the rest of the event in ascx page is not fired. I've tried to put breakpoints on the button event and validator events, if the button of the page (aspx) is the one I click, it will not stop on my validator events, if the button on the control (ascx) I click it will stop to the validator events. What could be the remedy for this?
Have you tried calling ucName.Page.Validate(); in the button click event?
(where ucName is whatever you called the user control in the markup)
Edit: OK this simple code below works fine for me, the only way I broke the validation was setting my user control to visible = false; Can you post more of your code?
Parent page markup:
<uc:ucTest id="ucName" runat="server">
<asp:Button ID="btnTest" runat="server" Text="Test validation" onclick="btnTest_Click" />
code behind:
protected void btnTest_Click(object sender, EventArgs e)
{
if (Page.IsValid)
{
Response.Write("Hi!");
}
}
user control markup:
<asp:TextBox ID="txtDummy" runat="server"></asp:TextBox>
<asp:CustomValidator ID="CustomValidator1" runat="server"
ErrorMessage="*"
ControlToValidate="txtDummy"
onservervalidate="CustomValidator1_ServerValidate">
</asp:CustomValidator>
code behind:
protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
{
if (txtDummy.Text.Length > 0)
{
args.IsValid = false;
}
}

Resources