loading an ASPX page into a jQuery UI Tab with AJAX - asp.net

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>

Related

ASP.NET Gridview with JQuery Mobile Dialog

I've been struggling trying to link an ASP.NET Gridview to a JQuery Mobile dialog, which would be used to edit data in the Gridview.
My goal was to use the GridView to display the data. The user would click on a row, and a dialog would open a dialog with a FormView, which the user could edit the data for the selected row. I got this to work fine with a JQuery UI dialog, but when I switched to Jquery Mobile, things fell apart.
Right now the dialog flashes on the screen for a second if I run it on an iOS device, or a Blackberry. It works okay if I run it in Windows. I'm not sure what I'm doing wrong.
Here is my code for the aspx page:
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="MyTest.aspx.cs" Inherits="MySite.MyTest" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Test</title>
<style type="text/css">
.hover
{
background-color: Gray;
}
</style>
<script type="text/javascript">
function clickRow() {
//Had to put in $(document).ready or I got PostBack errors.
$(document).ready(function () {
$.mobile.changePage("#dialogPage", 'pop', true, true);
});
}
</script>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<div data-role="page" id="mainpage">
<div data-role="content">
...GridView goes here...
Click Me
</div>
</div>
<div data-role="dialog" id="dialogPage">
<div data-role="content">
... FormView goes here....
</div>
</form>
</body>
</html>
And here is some of the code behind:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
//Allows user to click/tap anywhere on gridview row to trigger SelectIndexChange
e.Row.Attributes["onmouseover"] = "this.oldClass = this.className;this.className='hover';";
e.Row.Attributes["onmouseout"] = "this.className=this.oldClass;";
e.Row.Attributes["onclick"] = Page.ClientScript.GetPostBackClientHyperlink(GridView1, "Select$" + e.Row.RowIndex.ToString());
}
}
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
//This should open dialog
ClientScript.RegisterStartupScript(typeof(Page), "test", "clickRow()",true);
}
I think the problem is with the way I wrapped the $.mobile.changePage() function in the $(document).ready() function. If I didn't do that, I got postback errors. I'm not sure the right way to do this.
If I try to open the dialog using a <a data-rel="dialog"></a> link, it works fine on all devices.
Thanks for any advice.
I remember encountering a similar situation. The thing to remember is that when dealing with jQuery mobile, the script tags located within the head are not loaded on subsequent pages. Try moving your script block to within the tag that serves as the page.

how to call function from html file in asp.net

i'm new to asp.net
i want to call function from my html file
I have a html file containing a list of menu
Menu 1
and i have a file name home.aspx, i want to call a function in home.aspx.cs with that hyperlink
I have tried this but still can not
Call a C# function in ASP.NET when clicking on a HTML link
sorry for my English, English is not my native language
please help me
is my first code name home.aspx
<%# Page Language="C#" AutoEventWireup="true" CodeFile="home.aspx.cs" Inherits="user_home" %>
<% Response.WriteFile("tools/menu.htm"); %>
and this is home.aspx.cs
public partial class user_home : System.Web.UI.Page
protected void Page_Load(object sender, EventArgs e){
}
and this is my menu.htm
<div id="menu">
<ul>
<li>Menu1</li>
</ul>
</div>
sorry for trouble
Menu 1
You should use runat="server" attribute.
Are you sure you are not looking for a LinkButton???
This control renders in the client as:
Click me...
ASPX
<asp:LinkButton Text="Click me..." runat="server" ID="myLink" Click="myLink_Click" />
Code behind
protected void myLink_click(object sender, EventArgs e)
{
// write here your cool stuff as a response of the click event
}
If you insist to use an HtmlControl, then:
In ASPX
Menu 1
IN code behind
protected void myLink2_Click(object sender, EventArgs e)
{
// write here your cool stuff as a response of the click event
}

ASP.NET Web Forms multiple views and the lifecycle of controls that are not Visible

Given a scenario where a page/control should display different views (like tabs) in different circumstances (query string argument, postback from a control, user setting retrieved from the database) I would normally put a control like MultiView or Placeholder and switch the active view index or the Visible property like this:
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm2.aspx.cs" Inherits="TestWebApplication.WebForm2" %>
<%# Register Src="SomeControl.ascx" TagName="SomeControl" TagPrefix="uc1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:PlaceHolder runat="server" ID="phControl" Visible="false">
<uc1:SomeControl ID="SomeControl1" runat="server" />
</asp:PlaceHolder>
<asp:PlaceHolder runat="server" ID="phNotControl" Visible="false">
Some other content
</asp:PlaceHolder>
</div>
</form>
</body>
</html>
And I would switch the views in a code behind depending on the logic like this:
using System;
namespace TestWebApplication
{
public partial class WebForm2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
phControl.Visible = false;
phNotControl.Visible = true;
}
}
}
This is the control:
<%# Control Language="C#" AutoEventWireup="true" CodeBehind="SomeControl.ascx.cs" Inherits="TestWebApplication.SomeControl" EnableViewState="false" %>
I am the control
And the code behind of the control:
using System;
namespace TestWebApplication
{
public partial class SomeControl : System.Web.UI.UserControl
{
protected void Page_Init(object sender, EventArgs e)
{
Response.Write("I am the control's Init and I am executing heavy operations <br />");
}
protected void Page_Load(object sender, EventArgs e)
{
Response.Write("I am the control's Load <br />");
}
protected void Page_PreRender(object sender, EventArgs e)
{
Response.Write("I am the control's PreRender and I am only called if the control is Visible <br />");
}
}
}
However what if the control in question has some relatively heavy work to do (calling the database) in its init and load events? They get executed even if the control is not Visible and will slow down the page. One possible solution I see is moving all the work in the PreRender method of the child control because it is not executed for controls which are not visible. However this will not work if the control accepts user input from some dynamically populated control (think of dropdownlist) because the logic that populates the dropdown with options will not be executed.
I've come up with the following solutions:
Move the heavy work in the PreRender method and wrap it in an if(!IsPostBack). Then let the ViewState hold the possible values for the dropdown and ASP.NET will restore it. The downside is that it is using ViewState and bumping up the size of the page.
Create the control dynamically with LoadControl and add it in the placeholder from code behind. This will trigger catch up events and the code that populates the dropdown can be put in the Init method where it belongs. The downside is that the layout is defined in the code behind. Looking at the markup one cannot see what controls are on the page and looking at the code behind it is not clear where the control will appear. It also makes styling and other "design" operations more difficult.
Leave it like it is. The downside is that there will be additional database queries and if there are a lot of views these would add up.
My actual question is how I solve this problem without putting stuff in the ViewState, without multiple queries and without dragging my layout into the code behind.
Could it be a solution to make an AJAX call for your heavy database work?
Then you could display the View and in the background load the data you need and display it. The page would load really fast and you could display some indication that data is currently being loaded.
How about creating a public method on in your user controls, such as InitControl(). Do all the heavy work within InitControl() instead on page load. Then, on parent page, when page is being shown, call InitControl.
using System;
namespace TestWebApplication
{
public partial class WebForm2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
phControl.Visible = false;
phNotControl.Visible = true;
SomeControl1.InitControl();
}
}
}
Don't do any work within OnLoad event of your user controls.

accessing content page controls

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>

how to invoke server side and client side events in asp web forms

within an asp.net webform I have the following code
<asp:UpdatePanel ID="udpNames" runat="server">
<ContentTemplate>
<div class="expanderheaders">
<asp:Image ID="epImgNames" runat="server" ImageAlign="Middle" CssClass="expanderimage" />
<asp:LinkButton ToolTip="Expand Names" ID="lbtnNames" runat="server" OnClick="lbName_Click"
Text="Names" CssClass="detaillinks" />
</div>
<div class="detailsectionBorders">
<ajax:CollapsiblePanelExtender ID="epNames" runat="server" ExpandControlID="lbtnNames"
CollapseControlID="lbtnNames" Collapsed="true" ExpandedSize="420" ScrollContents="true"
ImageControlID="epImgNames" CollapsedImage="~/images/expandwn.png" ExpandedImage="~/images/expanup.png"
TargetControlID="namePanel" CollapsedSize="0" CollapsedText="Names" AutoExpand="false" />
<asp:Panel ID="namePanel" runat="server">
<asp:PlaceHolder runat="server" ID="PlaceHolderNames" />
</asp:Panel>
</div>
</ContentTemplate>
</asp:UpdatePanel>
DIV tag expanderheaders is a used as a header to the section. It contains a link button an image similar to a expander panel bar.
CollapsiblePanelExtnder is an ajax toolkit control that expands when a asp.net control is clicked (LinkButton) a user control is then loaded into the PlaceHolder to display a new section of data.
This all works fine but I am currently only able to click on the link button to expand the section (as expected). What I would like to do is have the ability to click on the entire div section (expanderHeaders) and have it serve as the control to expand the section.
I have looked at using jQuery and I have been able to duplicate the panel expansion as well as set the DIV layer to function as desired in accepting a client event and not just on an server side control. However, I have been unsuccessful in being able to invoke a server side method to load the user control when using jQuery.
Can anyone provide some guidance on how to either set the existing control up to where the link button could span the entire content of the div layer or use client side script/ jQuery to allow me to call a server side method to load a user control in the page?
Thanks in advance
update to James answer
I tried something similar to this
jquery
$(function () {
$("#panel").hide();
});
$(document).ready(function () {
$(".slide").click(function () {
$("#panel").show("slow");
});
});
and aspx
<div>
<div id="panel" >
<p>stuff here</p>
</div>
<div class="slide" id="div1" runat="server">
<p class="btn-slide">Expand Panel</p>
</div>
</div>
I'll omit the CSS as it is not that important for now
Using this approach clicking on the div layer seems to causes a postback each time clicked so the codebhind is never accessed.
protected void Page_Load (object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
div1.Attributes["onclick"] = ClientScript.GetPostBackEventReference(this, "ClickDiv");
}
}
protected override void RaisePostBackEvent (IPostBackEventHandler source, string eventArgument)
{
//call the RaisePostBack event
base.RaisePostBackEvent(source, eventArgument);
if (eventArgument.ToUpper() == "CLICKDIV")
{
}
}
still no dice.
It would probably be easier to do this with jQuery:
//obviously, adjust this to your needs
$(document).ready(function(){
$(".expanderheaders").click(function(){
$(".detailsectionBorders").hide("slow");
}
});
To do it server-side, if you give the div an ID and can specify runat="server", you can do something like this:
<div id="div1" runat="server">
Expand Me
</div>
Code-behind:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
div1.Attributes["onclick"] = ClientScript.GetPostBackEventReference(this, "ClickDiv");
}
}
protected override void RaisePostBackEvent(IPostBackEventHandler source, string eventArgument)
{
//call the RaisePostBack event
base.RaisePostBackEvent(source, eventArgument);
if (eventArgument.ToUpper() == "CLICKDIV")
{
//logic here
}
}

Resources