Ext.net:PreviousPage.FindControl issue - asp.net

I have main page something like this.
<ext:ResourceManager ID="ResourceManager1" runat="server" Theme="Gray" />
<form id="form2" runat="server">
<ext:Panel runat="server" ID="anaPanel" Title="OSO" Icon="Car">
<TopBar>
<ext:Toolbar runat="server" Layout="FitLayout">
<Items>
<ext:Menu ID="Menu1" runat="server" Floating="false" Layout="HBoxLayout" ShowSeparator="false"
AnimCollapse="true" Cls="horizontal-menu">
<Defaults>
<ext:Parameter Name="MenuAlign" Value="tl-bl?" Mode="Value" />
</Defaults>
<Items>
<ext:MenuItem ID="MenuItem1" runat="server" Text="" Icon="Group">
<Menu>
<ext:Menu ID="Menu2" runat="server">
<Items>
<ext:MenuItem Text="new card" Icon="GroupAdd">
<DirectEvents>
<Click OnEvent="AddNewCart_Click"></Click>
</DirectEvents>
</ext:MenuItem>
...............
...............
</ext:Panel>
<ext:Window runat="server" ID="MyWindow" Hidden="true"></ext:Window>
</form>
My maninpage codebehind something like this.
protected void AddNewCart_Click(object sender, DirectEventArgs e)
{
string path = "Pages/Kart.aspx";
Window win = CreateWindows(MyWindow,Icon.Group,path,"new card", 420, 500);
//private Window CreateWindows(Window Mywindow , Icon ic,string path,string Title, int Heigh, int With){......};
//I get the MyWindow and pass some values and turn it back.
win.Render(this.Form);
win.Show();
}
inside the Kart.aspx I have some buttons,my first question is ,how can I close this window .I put a button to close the this window(window which loaded Kart.aspx) ,here are the some way how I did.but none of them didnt work.
1-
if (PreviousPage != null)//previouspage come as a null.
{
Window wnd = PreviousPage.FindControl("MyWindow") as Window;
wnd.Close();
}
2-
Window wnd = Parent.Page.FindControl("MyWindow") as Window;
wnd.Close();
3-I also tried to make MyWindow as public inside mainpage and try to reach it from Kart.aspx,but also didnt work
my second question is ,how can I pass the parameters between the those pages.

you can do it in the client side by calling
window.parent.Ext.getCmp('MyWindow').close();
Edit
the closest to a code behind call is to use
btnClose.AddScript("window.parent.Ext.getCmp('MyWindow').close();");
Edit 2
if you code is more complex like reloading a grid or modifying forms, you do the following:
Code-Behind
X.Call("ReloadGrid");
JavaScript
function ReloadGrid() {
var grid = window.parent.Ext.getCmp('GridId');
//grid reloading code
}

Related

RegisterStartupScript from code behind not working when Update Panel is used

I have created a radiobutton list followed by one button. I can select one of the item from the radiobutton list and then click the button to execute something. And I want the page to pop up a window if the button is clicked without selecting any of the options from the radiobutton list. My codebehide is like this:
protected void ButtonPP_Click(object sender, EventArgs e)
{
if (radioBtnPP.SelectedIndex < 0)
{
String csname1 = "PopupScript";
Type cstype = this.GetType();
// Get a ClientScriptManager reference from the Page class.
ClientScriptManager cs = Page.ClientScript;
// Check to see if the startup script is already registered.
if (!cs.IsStartupScriptRegistered(cstype, csname1))
{
StringBuilder cstext1 = new StringBuilder();
cstext1.Append("<script type=text/javascript> alert('Please Select Criteria!') </");
cstext1.Append("script>");
cs.RegisterStartupScript(cstype, csname1, cstext1.ToString());
}
}
}
and my html part is:
<asp:RadioButtonList class ="radioBtn" ID="radioBtnACO" runat="server">
<asp:ListItem Text="All Dim" />
<asp:ListItem Text="Select Dim" />
</asp:RadioButtonList>
<asp:Button id ="ButtonPP" class ="buttonRight" runat="server" Text="Run PP" OnClick="ButtonPP_Click" />
This works fine. However, now I don't want to refresh the whole page when i click the button, so I use updatepanel. And I changed my html code to the following:
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="ControlUpdatePanel" runat="server" Visible="True">
<ContentTemplate>
<asp:RadioButtonList class ="radioBtn" ID="radioBtnACO" runat="server">
<asp:ListItem Text="All Dim" />
<asp:ListItem Text="Select Dim" />
</asp:RadioButtonList>
<asp:Button id ="ButtonPP" class ="buttonRight" runat="server" Text="Run PP" OnClick="ButtonPP_Click" />
</ContentTemplate>
</asp:UpdatePanel>
But now my code doesn't work anymore... If I don't select any option from the radiobutton list and just click the button, no window would pop up :( I am sure my codebehind is still executed but seems like it just doesn't passed to my .aspx page...
Anyone please help me out? I don't know what to do now... Millions of thanks!
You need to use ScriptManager.RegisterStartupScript for Ajax.
protected void ButtonPP_Click(object sender, EventArgs e)
{
if (radioBtnACO.SelectedIndex < 0)
{
string csname1 = "PopupScript";
var cstext1 = new StringBuilder();
cstext1.Append("alert('Please Select Criteria!')");
ScriptManager.RegisterStartupScript(this, GetType(), csname1,
cstext1.ToString(), true);
}
}

Ext.Net,loading 2 child page ,and reach from one child to another

My main.aspx
<ext:ResourceManager ID="ResourceManager1" runat="server" Theme="Gray" />
<form id="form2" runat="server">
<ext:Panel runat="server" ID="anaPanel" Title="OSO" Icon="Car">
<TopBar>
<ext:Toolbar runat="server" Layout="FitLayout">
<Items>
<ext:Menu ID="Menu1" runat="server" Floating="false" Layout="HBoxLayout" ShowSeparator="false"
AnimCollapse="true" Cls="horizontal-menu">
<Defaults>
<ext:Parameter Name="MenuAlign" Value="tl-bl?" Mode="Value" />
</Defaults>
<Items>
<ext:MenuItem ID="MenuItem1" runat="server" Text="" Icon="Group">
<Menu>
<ext:Menu ID="Menu2" runat="server">
<Items>
<ext:MenuItem Text="new card" Icon="GroupAdd">
<DirectEvents>
<Click OnEvent="AddNewCart_Click"></Click>
</DirectEvents>
</ext:MenuItem>
...............
...............
</ext:Panel>
</form>
My maninpage codebehind something like this.
protected void AddNewCart_Click(object sender, DirectEventArgs e)
{
string path = "Pages/Kart.aspx";
Window win = CreateWindows(MyWindow,Icon.Group,path,"new card", 420, 500);
// private Window CreateWindow(string Id, Icon ic, string path, string Title, int Heigh, int width){......};
//I get the new Window and pass some values and turn it back.
win.Render(this.Form);
win.Show();
}
inside Kart.aspx there is also a button and when the clicked button , load second childpage just the same above code
but changed path (lets say loading B.aspx).I am doing this like that window.parent.App.direct.MethodName(). *[MethodName() descibed in the main.aspx] but I need to refresh a grid inside the Kart.aspx, when and new item from B.aspx,
*I have tried to reach Kart.aspx methon like this window.parent.App.direct.MethodName() but seems doesnt work.it works only if it is described in the main.aspx codebehind.
a friend told me that ,only way to do that is use javascript" like this
Code-Behind
X.Call("ReloadGrid");
function ReloadGrid() {
var grid = window.parent.Ext.getCmp('GridId');
//grid reloading code
}
but I have no idea how I reload grid via this script.or are there anaother way to do this.thank you
my Kart.aspx ;
<ext:GridPanel runat="server" ID="grid1" Title="" Height="460"
Flex="1" Scroll="Vertical">
<Store>
<ext:Store ID="strKart" runat="server">
<Model>
<ext:Model ID="Model1" runat="server" IDProperty="Id">
<Fields>
<ext:ModelField Name="Id" />
<ext:ModelField Name="name" />
<ext:ModelField Name="surname" />
</Fields>
</ext:Model>
</Model>
<Listeners>
<Exception Handler="Ext.Msg.alert('Products - Load failed', operation.getError());" />
</Listeners>
</ext:Store>
</Store>
and code behind of it
[DirectMethod]
public void ReloadKart()
{
this.strKart.DataSource = cari_bll.GetAll();
this.strKart.DataBind();
}
**trying to run ReloadKart () function from B.aspx which is clicked the button
Now you have 3 pages, the parent lets call it Parent.aspx, and the children Kart.aspx, and b.aspx
And you want to load the grid in Kart.aspx based on an event in b.aspx
Since you load the grid by calling the direct method
[DirectMethod]
public void ReloadKart()
{
this.strKart.DataSource = cari_bll.GetAll();
this.strKart.DataBind();
}
its boils down to calling this method inside Kart.aspx
To achieve this you need to do the following:
Define a JavaScript method in Kart.aspx that calls the direct method ReloadKart, lets name it ReloadGrid
function ReloadGrid()
{
App.direct.ReloadKart();
}
Define a delegate to this method in Parent.aspx, lets call it ReloadGridDelegate, a method to call that delegate CallKartReloadGrid, and a method to set it SetReloadGridDelegate
var ReloadGridDelegate;
function CallKartReloadGrid()
{
ReloadGridDelegate();
}
function SetReloadGridDelegate(delegate)
{
ReloadGridDelegate = delegate;
}
In Kart.aspx assign call the SetReloadGridDelegate
window.parent.SetReloadGridDelegate(ReloadGrid);
Finally in b.aspx call the parent method
window.parent.CallKartReloadGrid();

How to avoid Page_Load() on button click?

I have two buttons, preview and Save. With help of preview button user can view the data based on the format and then can save.
But when preview is clicked, one textbox attached to ajaxcontrol (Calender) becomes empty and user have to fill the date before saving. How to handle this? On preview click i get the details to show the data in layout.
<asp:TextBox ID="txtDate" ReadOnly="true" runat="server"></asp:TextBox>
<div style="float: right;">
<asp:ImageButton ID="imgcalender1" runat="server" ImageUrl="~/images/calendar.png"
ImageAlign="Bottom" />
<asp:CalendarExtender ID="ajCal" TargetControlID="txtpublishDate" PopupButtonID="imgcalender1"
runat="server">
</asp:CalendarExtender>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" ValidationGroup="group1" runat="server" ControlToValidate="txtDate"
ForeColor="Red" Font-Bold="true" ErrorMessage="*"></asp:RequiredFieldValidator>
</div>
<asp:Button ID="btnPreview" runat="server" Text="Preview" OnClick="btnPreview_Click" />
<asp:Button ID="btnsubmit" runat="server" ValidationGroup="group1" Text="Save" OnClick="btnsubmit_Click" />
Use Page.IsPostback() in your aspx code (server-side). Like this:
private void Page_Load()
{
if (!IsPostBack)
{
// the code that only needs to run once goes here
}
}
This code will only run the first time the page is loaded and avoids stepping on user-entered changes to the form.
From what I am understanding the preview button is causing a postback and you do not want that, try this on your preview button:
<asp:button runat="server".... OnClientClick="return false;" />
similarly this also works:
YourButton.Attributes.Add("onclick", return false");
Edit:
it seems the answer to the user's question was simple change in the HTML mark up of the preview button
CausesValidation="False"
you can put your code in
Page_Init()
{
//put your code here
}
instead of
Page_Load()
{
//code
}
I had the same problem and the solution above of "CausesValidation="False"" and even adding "UseSubmitBehavior="False"" DID NOT work - it still called "Page_Load" method.
What worked for me was adding the following line up front in Page_Load method.
if (IsPostBack) return;
I am mentioning this if it helps someone (I meant to comment above but StackOverflow did not allow me to comment because I am a new user - hence a new reply).
Try adding this to the buttons properties in the aspx page.
OnClientClick="return false;"
For my the #tgolisch answer worked better, maybe it's because i'm still a rookie.
I was trying to load a simple captcha in my WebForm and end up using a Reference Type in the Page_Load event and in a Button Click event (for a code snippet).
In the end i only have to edit some things and it's done:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
var captchaText = generateCaptchaCode(5);
lblCaptcha.Text = captchaText;
}
}
protected void btnCheckCaptcha_Click(object sender, EventArgs e)
{
if (txtCaptchaCode.Text == lblCaptcha.Text)
lblMessage.Text = "Right input characters";
else
lblMessage.Text = "Error wrong characters";
}
form1.Action = Request.RawUrl;
Write this code on page load then page is not post back on button click

C# onClick Event on ASP.NET Button not initially fired but postsbacks... and eventually fired after multiple clicks

I'm having a weird issue and i'm not sure where to look or how to debug.
I have a Parent Page A which has a Combo box control with 7 provinces; Each Province has a User Control ( View ). By default , the selected index is province "SK" which loads in the same page a user control View dynamically.
Below is my HTML and Code behind for the Parent Page
<telerik:RadComboBox ID="cmbProvince" runat="server" OnSelectedIndexChanged="cmbProvince_SelectedIndexChanged" AutoPostBack="true">
<Items>
<telerik:RadComboBoxItem runat="server" Text="Quebec" Value="QC" />
<telerik:RadComboBoxItem runat="server" Text="Ontario" Value="ON" />
<telerik:RadComboBoxItem runat="server" Text="Saskatchewan" Value="SK" />
<telerik:RadComboBoxItem runat="server" Text="Manitoba" Value="MB" />
<telerik:RadComboBoxItem runat="server" Text="Yukon" Value="YT" />
<telerik:RadComboBoxItem runat="server" Text="Nuvanut" Value="NU" />
</Items>
</telerik:RadComboBox>
protected void Page_PreInit(object sender, EventArgs e)
{
MasterPage master = this.Master;
_currentProvince = GetCurrentOperatingProvince(IsPostBack);
cmbProvince.SelectedValue = _currentProvince;
txtReferenceNo.Text = "TECHLOS" + DateTime.Now.ToString("MMddyyyy-HHMMss");
//Reload Dynamic Control on Every Page Creation
LoadDetailControl();
}
private void LoadDetailControl()
{
UserControl ucPPSAControl = (UserControl)LoadControl(GetUserControlPath(_currentProvince));
IPPSAView provView = ucPPSAControl as IPPSAView;
ApplicationContext.TargetProvince = _currentProvince;
PPSAPresenter presenter = new PPSAPresenter(provView, ApplicationContext);
provView.AttachPresenter(presenter, cmbProvince.SelectedValue, txtReferenceNo.Text);
phPPSAControl.Controls.Add(ucPPSAControl);
}
protected void cmbProvince_SelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
{
phPPSAControl.Controls.Clear();
_currentProvince = e.Value;
LoadDetailControl();
}
In each UC by province, it has a Search button with an OnClick Event. The first time I land on the default province, the Search button works as expected and fires the OnClick and the Event Handler Search_Click is ran.
Below is that of a typical UC for each province
HTML button.
<telerik:RadButton ID="btnSearch" runat="server" Text="Search" OnClick="Search_OnClick">
</telerik:RadButton>
and Code behind
protected void Search_OnClick(object sender, EventArgs e)
{
if (isValidSearchRequest())
{
DisplayResultsFromResponse(presenter.Search());
}
else
{
phResponse.Controls.Add(new LiteralControl(string.Format("<span style='color:red'>Invalid Search Request</span>")));
}
}
Now when I change the province to "ON" ( or any other province has the same behaviour), When I click on the Search Button on the newly generated UC , it only posts back without triggering the OnClick Event. Clicking Twice , does the same thing... and on the 3rd time, It does trigger the OnClick... and ran as normal!!!
This behaviour occurs whether I have "ON" View the first time it loads and "SK" View ( or any other View for that matter ) the sencond time, where i can assume something with the initial creation of the View makes the button work correctly... but subsequent reloading of a new control will cause the OnClick to fail.
Another point to note, Once i change the province, even if i come back to the original province, the OnClick won't work...Also I'm using Telerik controls with RadAjaxManager
How I can debug this to find out where my problem is... even better how can i fix this?
After 3 days spent on this... it looks like the problem was related to the dynamic control . I created it without assigning an ID...so on recreation, somehow it posted back a few times probably because it was using the ASP's generated ID and each time it wasn't the same, so my onCLick on the button couldnt find the button that originally called it.. i guess on the 3rd time, the id is the same and thats when it called it properly.
Anyways, i know this fixed the problem i had
after the ScriptManager place this code
<telerik:RadAjaxManager ID="RadAjaxManager1" runat="server">
<AjaxSettings>
<telerik:AjaxSetting AjaxControlID="RadAjaxManager1">
<UpdatedControls>
<telerik:AjaxUpdatedControl ControlID="cmbProvince" />
<telerik:AjaxUpdatedControl ControlID="btnSearch" />
</UpdatedControls>
</telerik:AjaxSetting>
</AjaxSettings>
may help you

ASP.Net Master Page Sidebar display after Login (keeping it displayed whilst logged in)

I have the following sidebar on my master page. It is not a part of any ContentPlaceHolder.
<div runat="server" visible="false" id="menuAccountMembersDiv" class="leftCol">
<asp:Menu ID="menuAccountMembers" runat="server" StaticSubMenuIndent="16px" Visible="false">
<Items>
<asp:MenuItem ImageUrl="~/Resources/x.png"
NavigateUrl="~/About.aspx" Text="x" ToolTip="x"
Value="b647ce4e-5c7f-400c-a921-ec7902494f26"></asp:MenuItem>
<asp:MenuItem ImageUrl="~/Resources/y.png"
NavigateUrl="~/About.aspx" Text="y" ToolTip="y"
Value="y"></asp:MenuItem>
<asp:MenuItem ImageUrl="~/Resources/sarahhunkin.png" NavigateUrl="~/About.aspx"
PopOutImageUrl="~/Resources/z.png" Text="z"
ToolTip="z" Value="z"></asp:MenuItem>
<asp:MenuItem ImageUrl="~/Resources/a.png"
NavigateUrl="~/About.aspx"
PopOutImageUrl="~/Resources/apop.png" Text="a"
ToolTip="a" Value="a"></asp:MenuItem>
</Items>
</asp:Menu>
</div>
I initially hide it. But I would like to display it and keep it displayed after logging in. Using the standard web application login page. I tried the following:
protected void LoginUser_LoggedIn(object sender, EventArgs e)
{
Menu MenuAccountMembers = (Menu)Master.FindControl("menuAccountMembers");
MenuAccountMembers.Visible = true;
Control menuAccountMembersDiv = (Control)Master.FindControl("menuAccountMembersDiv");
menuAccountMembersDiv.Visible = true;
}
I am not sure to to interact with the div tag, as there is no Div object. In any event, this does not display the sidebar with the menu
EDIT:
I ended up adding the following code to the master page itself.
public partial class SiteMaster : System.Web.UI.MasterPage
{
protected void Page_Load(object sender, EventArgs e)
{
if (HttpContext.Current.Request.IsAuthenticated)
{
Control MenuDiv = this.FindControl("menuAccountMembersDiv");
MenuDiv.Visible = true;
Menu AccountMenu = (Menu)MenuDiv.FindControl("menuAccountMembers");
AccountMenu.Visible = true;
}
}
}
I would go for setting the visibiliy directly on your div based on authentication status
<div runat="server" visible="<%# Page.User.IsAuthenticated %>" id="menuAccountMembersDiv" class="leftCol">
that way you don't need your LoginUser_LoggedIn method and the menu will show/hide on every load depending on the user is logged in or not
And remember to remove the Visible="false" from your <asp:Menu> control, if the outer div is hidden, nothing inside it will be shown anyway.
Since you have the runat="server" tag in the menu's Div tag, you can reference it directly in code...
menuAccountMembersDiv.Style.Item("Display") = "none";
or
menuAccountMembersDiv.Visible = False;
A div tag is a HtmlGenericControl class. To get access to this class import namespace System.Web.UI.HtmlControls; and use something like this:
HtmlGenericControl div = Master.FindControl("menuAccountMembersDiv") as HtmlGenericControl;
if(div != null)
{
div.Visible = true;
}
Or you can move your menu to UserControl and hide or show just use ID of your UserControl.
Hope it will help you with your question.

Resources