I learned several ASP.NET's AJAX ability today! It is so interesting and so easy to use AJAX in ASP.NET.
2006-12-02 11:28
671 查看
The first example is about Gridview Control. The most important Property here is EnableSortingAndPagingCallbacks Property. From the word you can see that, if you set it true, the control will get AJAX ability and it can sort and page without flashing the page.
The code is here:
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True"
AutoGenerateColumns="False" DataKeyNames="CustomerID" DataSourceID="SqlDataSource1"
EnableSortingAndPagingCallbacks="True" PageSize="5">
<Columns>
<asp:BoundField DataField="CustomerID" HeaderText="CustomerID" ReadOnly="True" SortExpression="CustomerID" />
<asp:BoundField DataField="CompanyName" HeaderText="CompanyName" SortExpression="CompanyName" />
<asp:BoundField DataField="ContactName" HeaderText="ContactName" SortExpression="ContactName" />
<asp:BoundField DataField="ContactTitle" HeaderText="ContactTitle" SortExpression="ContactTitle" />
<asp:BoundField DataField="Address" HeaderText="Address" SortExpression="Address" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
SelectCommand="SELECT [CustomerID], [CompanyName], [ContactName], [ContactTitle], [Address] FROM [Customers]">
</asp:SqlDataSource>
The second example is about TreeView.
<asp:TreeView ID="TreeView1" runat="server" Height="174px" ImageSet="XPFileExplorer" Width="169px" OnTreeNodePopulate="TreeView1_TreeNodePopulate">
<Nodes>
<asp:TreeNode Text="Visual Stdio 2005" Value="C:/Documents and Settings/v-soguo/My Documents/Visual Studio 2005" Expanded="False" PopulateOnDemand="True"/>
</Nodes>
</asp:TreeView>
It is the same as the gridview. PopulateNodesFromClient is the key property here. If you set it true, it will asynchronously populate the children when it is expended. On the client side, you will find it is no refreshing. By the way this property 's default value is true.
The Server side code is here:
protected void TreeView1_TreeNodePopulate(object sender, TreeNodeEventArgs e)
{
DirectoryInfo info = new DirectoryInfo(e.Node.Value);
foreach (DirectoryInfo directory in info.GetDirectories())
{
TreeNode newNode = new TreeNode();
newNode.PopulateOnDemand = true;
newNode.Text = directory.Name;
newNode.Value = directory.FullName;
e.Node.ChildNodes.Add(newNode);
}
foreach (FileInfo file in info.GetFiles())
{
TreeNode newNode = new TreeNode();
newNode.Text = file.Name;
newNode.Value = file.FullName;
e.Node.ChildNodes.Add(newNode);
}
}
![](http://p.blog.csdn.net/images/p_blog_csdn_net/diandian82/New Bitmap Image.bmp)
The third example is implement the AJAX by ICallbackEventHandler.
At first, the page must inherit the ICallbackEventHandler as below:
public partial class _Default : System.Web.UI.Page ,ICallbackEventHandler
protected void Page_Load(object sender, EventArgs e)
{
string cbReference = this.ClientScript.GetCallbackEventReference(
this, "this.value", "OnSuccessCallback", null, "OnFailureCallback", true);
this.DropDownList1.Attributes["onchange"] = cbReference;
/* For the server button control, if you don't want it to be postbacked, you have to add "return false" at the end of the event. In fact, ClientScript.GetCallbackEventReference created a complete string which is be binded to the client event, such as onclick of the button.
string btnReference = ClientScript.GetCallbackEventReference(
this, "this.name", "OnSuccessCallback", null, "OnFailureCallback", true);
this.Button1.Attributes["onclick"] = btnReference + ";return false"; */
}
second you have to implement the two function of the ICallbackEventHandler,one is RaiseCallbackEvent, the other is GetCallbackResult. RaiseCallbackEvent is to do some process of the call beginning. GetCallbackResult returns the result by means of string.
private int indexer=0; // the indicator of the value.
string[] strs = new string[] {"a","b","c","d","e" }; // the value want to return .
void ICallbackEventHandler.RaiseCallbackEvent(string argumnet)
{
indexer = int.Parse(argumnet);
}
string ICallbackEventHandler.GetCallbackResult()
{
return strs[indexer];
}
<input id="Text1" type="text" />
<asp:DropDownList ID="DropDownList1" runat="server" Width="193px">
<asp:ListItem>1</asp:ListItem>
<asp:ListItem>2</asp:ListItem>
<asp:ListItem>3</asp:ListItem>
<asp:ListItem>4</asp:ListItem>
<asp:ListItem>5</asp:ListItem>
<asp:ListItem>6</asp:ListItem>
</asp:DropDownList>
function OnSuccessCallback(result,context)
{
document.getElementById("Text1").innerText = result;
}
function OnFailureCallback(errmessage)
{
document.getElementById("Text1").innerText = "Error:" + errmessage;
}
The code is here:
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True"
AutoGenerateColumns="False" DataKeyNames="CustomerID" DataSourceID="SqlDataSource1"
EnableSortingAndPagingCallbacks="True" PageSize="5">
<Columns>
<asp:BoundField DataField="CustomerID" HeaderText="CustomerID" ReadOnly="True" SortExpression="CustomerID" />
<asp:BoundField DataField="CompanyName" HeaderText="CompanyName" SortExpression="CompanyName" />
<asp:BoundField DataField="ContactName" HeaderText="ContactName" SortExpression="ContactName" />
<asp:BoundField DataField="ContactTitle" HeaderText="ContactTitle" SortExpression="ContactTitle" />
<asp:BoundField DataField="Address" HeaderText="Address" SortExpression="Address" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
SelectCommand="SELECT [CustomerID], [CompanyName], [ContactName], [ContactTitle], [Address] FROM [Customers]">
</asp:SqlDataSource>
The second example is about TreeView.
<asp:TreeView ID="TreeView1" runat="server" Height="174px" ImageSet="XPFileExplorer" Width="169px" OnTreeNodePopulate="TreeView1_TreeNodePopulate">
<Nodes>
<asp:TreeNode Text="Visual Stdio 2005" Value="C:/Documents and Settings/v-soguo/My Documents/Visual Studio 2005" Expanded="False" PopulateOnDemand="True"/>
</Nodes>
</asp:TreeView>
It is the same as the gridview. PopulateNodesFromClient is the key property here. If you set it true, it will asynchronously populate the children when it is expended. On the client side, you will find it is no refreshing. By the way this property 's default value is true.
The Server side code is here:
protected void TreeView1_TreeNodePopulate(object sender, TreeNodeEventArgs e)
{
DirectoryInfo info = new DirectoryInfo(e.Node.Value);
foreach (DirectoryInfo directory in info.GetDirectories())
{
TreeNode newNode = new TreeNode();
newNode.PopulateOnDemand = true;
newNode.Text = directory.Name;
newNode.Value = directory.FullName;
e.Node.ChildNodes.Add(newNode);
}
foreach (FileInfo file in info.GetFiles())
{
TreeNode newNode = new TreeNode();
newNode.Text = file.Name;
newNode.Value = file.FullName;
e.Node.ChildNodes.Add(newNode);
}
}
![](http://p.blog.csdn.net/images/p_blog_csdn_net/diandian82/New Bitmap Image.bmp)
The third example is implement the AJAX by ICallbackEventHandler.
At first, the page must inherit the ICallbackEventHandler as below:
public partial class _Default : System.Web.UI.Page ,ICallbackEventHandler
protected void Page_Load(object sender, EventArgs e)
{
string cbReference = this.ClientScript.GetCallbackEventReference(
this, "this.value", "OnSuccessCallback", null, "OnFailureCallback", true);
this.DropDownList1.Attributes["onchange"] = cbReference;
/* For the server button control, if you don't want it to be postbacked, you have to add "return false" at the end of the event. In fact, ClientScript.GetCallbackEventReference created a complete string which is be binded to the client event, such as onclick of the button.
string btnReference = ClientScript.GetCallbackEventReference(
this, "this.name", "OnSuccessCallback", null, "OnFailureCallback", true);
this.Button1.Attributes["onclick"] = btnReference + ";return false"; */
}
second you have to implement the two function of the ICallbackEventHandler,one is RaiseCallbackEvent, the other is GetCallbackResult. RaiseCallbackEvent is to do some process of the call beginning. GetCallbackResult returns the result by means of string.
private int indexer=0; // the indicator of the value.
string[] strs = new string[] {"a","b","c","d","e" }; // the value want to return .
void ICallbackEventHandler.RaiseCallbackEvent(string argumnet)
{
indexer = int.Parse(argumnet);
}
string ICallbackEventHandler.GetCallbackResult()
{
return strs[indexer];
}
<input id="Text1" type="text" />
<asp:DropDownList ID="DropDownList1" runat="server" Width="193px">
<asp:ListItem>1</asp:ListItem>
<asp:ListItem>2</asp:ListItem>
<asp:ListItem>3</asp:ListItem>
<asp:ListItem>4</asp:ListItem>
<asp:ListItem>5</asp:ListItem>
<asp:ListItem>6</asp:ListItem>
</asp:DropDownList>
function OnSuccessCallback(result,context)
{
document.getElementById("Text1").innerText = result;
}
function OnFailureCallback(errmessage)
{
document.getElementById("Text1").innerText = "Error:" + errmessage;
}
相关文章推荐
- A ScriptManager is required on the page to use ASP.NET AJAX Script Components
- Send and Receive JSON objects to Web Service Methods using jQuery AJAX in ASP.Net
- It is not possible to run two different versions of ASP.NET in the same IIS process
- What is reflection and how to use it in CSharp ?
- It is not possible to run two different versions of ASP.NET in the same IIS process:IIS
- The service cannot be activated because it does not support ASP.NET compatibility. ASP.NET compatibility is enabled for this application. Turn off ASP.NET compatibility mode in the web.config or add the AspNetCompatibilityRequirements attribute to the ser
- How to use Bundle&Minifier and bundleconfig.json in ASP.NET Core
- Use jQuery and ASP.NET AJAX to build a client side Repeater
- Use Office Web Components to Load Excel and perform calculations in ASP.NET
- How to use jquery or ajax to update razor partial view in c#/asp.net for a MVC project
- This function has none of Deterministic,no sql,or reads sql data in its declaration and binary logging is enabled(you *might* want to use the less safe log_bin_trust_function_creators variable
- It is not possible to run two different versions of ASP.NET in the same IIS process.问题的解决
- Is it possible to use a checkbox in a split button list instead of the gear icon?
- Session Data Is Lost When You Use ASP.NET InProc Session State Mode
- How To: Use Impersonation and Delegation in ASP.NET 2.0
- Use C# to get JSON Data from the Web and Map it to .NET Class => Made Easy! 转
- Custom Client Side Drag and Drop Behavior in ASP.NET AJAX
- WHAT IS ISO? A CAMERA’S SENSITIVITY TO LIGHT EXPLAINED, AND HOW TO USE IT
- Preview Word files (docx) in HTML using ASP.NET, OpenXML and LINQ to XML
- (zhuan) Attention in Neural Networks and How to Use It