Ajax with Dotnet

首先http://ajax.asp.net/ 去下载一个VS2005的插件, 安装完成后。

新建一个网站的时候,会看到一个新的模版 asp.net ajax-enable web site

建立新的项目后,项目会自动引用ajax的元素,然后只需要配置就可以使用了

在这里我写了一个二级联动的菜单,数据库使用的是Mysql.

以下是代码

default.aspx

<% @ Page Language = " C# "  AutoEventWireup = " true "  CodeFile = " Default.aspx.cs "  Inherits = " _Default "   %>

<! DOCTYPE html PUBLIC  " -//W3C//DTD XHTML 1.1//EN "   " http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd " >
< html xmlns = " http://www.w3.org/1999/xhtml " >
< head runat = " server " >
    
< title > Ajax test </ title >
</ head >
< body >
    
< form id = " form1 "  runat = " server " >
        
< asp:ScriptManager ID = " ScriptManager1 "  runat = " server "   />
     
< asp:UpdatePanel ID = " UpdatePanel1 "  runat = " server " >
          
< ContentTemplate >
                
< asp:DropDownList ID = " reslist "  runat = " server " >
                  
                
</ asp:DropDownList >
                 
            
</ ContentTemplate >
            
< Triggers >
                
< asp:AsyncPostBackTrigger ControlID = " typelist "  EventName = " SelectedIndexChanged "   />
            
            
</ Triggers >
        
</ asp:UpdatePanel >
        
< div >
        
< asp:DropDownList ID = " typelist "  runat = " server "  OnSelectedIndexChanged = " typelist_SelectedIndexChanged "  AutoPostBack = " True " ></ asp:DropDownList >< br  />
        
& nbsp; < br  />
     
< asp:GridView ID = " viewlist "   runat = " server " ></ asp:GridView >
        
</ div >
    
</ form >
</ body >
</ html >

default.aspx.cs

 

using  System;
using  System.Data;
using  System.Configuration;
using  System.Web;
using  System.Web.Security;
using  System.Web.UI;
using  System.Web.UI.WebControls;
using  System.Web.UI.WebControls.WebParts;
using  System.Web.UI.HtmlControls;
using  MySql.Data.MySqlClient;

public   partial   class  _Default : System.Web.UI.Page 
{
    
protected void Page_Load(object sender, EventArgs e)
    
{
        
if (!IsPostBack)
        
{
            DataTable ds 
= new DataTable();

            
string conn = ConfigurationManager.ConnectionStrings["mysql"].ConnectionString;
            MySqlConnection connection 
= new MySqlConnection(conn);
            
string type = "select tid,name from resource_type";
            MySqlDataAdapter adapter 
= new MySqlDataAdapter();
            MySqlCommand command 
= new MySqlCommand(type, connection);
            adapter.SelectCommand 
= command;

            connection.Open();
            command.ExecuteNonQuery();
            adapter.Fill(ds);
            connection.Close();
            viewlist.DataSource 
= ds.DefaultView;
            viewlist.DataBind();
            typelist.DataSource 
= ds.DefaultView;
            typelist.DataTextField 
= "name";
            typelist.DataValueField 
= "tid";
            typelist.DataBind();
        }

    }

    
protected void typelist_SelectedIndexChanged(object sender, EventArgs e)
    
{
      
//  Response.Write(typelist.SelectedValue);
     
//   Response.Write("javascript(alert("+typelist.SelectedItem.Value+"))");
       
// Response.Write(typelist.SelectedValue);

        
int id = Convert.ToInt32(this.typelist.SelectedValue);
        
//id = 17;
        
//Response.Write("alert("+id.ToString()+")");
        DataSet ds = new DataSet();

        
string conn = ConfigurationManager.ConnectionStrings["mysql"].ConnectionString;
        MySqlConnection connection 
= new MySqlConnection(conn);
        
string type = "select rid,name from resource where type=" + id;
        MySqlDataAdapter adapter 
= new MySqlDataAdapter();
        MySqlCommand command 
= new MySqlCommand(type, connection);
        adapter.SelectCommand 
= command;

        connection.Open();
        command.ExecuteNonQuery();
        adapter.Fill(ds);
        connection.Close();
        
//viewlist.DataSource = ds.Tables[0];
        
//viewlist.DataBind();
        reslist.DataSource = ds.Tables[0];
        reslist.DataTextField 
= "name";
        reslist.DataValueField 
= "rid";
        reslist.DataBind();
    }

   
}

后台代码没什么变化,有变化的就是前台代码,然后多加一段新的代码

    <asp:ScriptManager ID="ScriptManager1" runat="server" />   //这个是引用dotnet ajax的说明
     <asp:UpdatePanel ID="UpdatePanel1" runat="server">     //具体使用ajax的一个方法 我这里使用的是update,当然还有其他的方法
          <ContentTemplate>                                                            //模版列
                <asp:DropDownList ID="reslist" runat="server">  //添加了一个dropdownlist
                  
                </asp:DropDownList>
                
            </ContentTemplate>
            <Triggers>                                                        //这里很重要了   触发的事件的详细设置
                <asp:AsyncPostBackTrigger ControlID="typelist" EventName="SelectedIndexChanged" />
               //我这里用的是一个同步的触发,ContorlID是第一个dropdownlist,然后事件是在选后触发
            </Triggers>
        </asp:UpdatePanel>

所有的代码全在这里了,希望对大家使用ajax有所帮助

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值