.Net中用ajax控制元件實現功能(例)

iDotNetSpace發表於2009-07-14

隨筆簡介:積累技術,免得遺忘

環境:VS2008,sql 2005,系統自帶資料庫

1.功能,在dropdownlist中選擇查詢條件,點選按鈕查詢,無重新整理gridview限制各條件下的資料

2.實現方式,在body中新增一個scriptmannager, 接著新增一個updatpannel,然後在裡面新增操作

3.原理不解釋,我也解釋不清楚,菜鳥先學會操作吧,程式碼如下

 

.Net中用ajax控制元件實現功能(例)
//前臺
@ Page Language="C#" AutoEventWireup="true" CodeBehind="deafault.aspx.cs" Inherits="WebApplication1.deafault" %>

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>
    
        利用ajx技術實現頁面無重新整理
<br />
        請選擇僱員ID:
<br />
        
<asp:ScriptManager ID="ScriptManager1" runat="server">
    
asp:ScriptManager>
        
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
            
<ContentTemplate>
                
<asp:DropDownList ID="DropDownList1" runat="server">
                    
<asp:ListItem Value="7">編號7asp:ListItem>
                    
<asp:ListItem Value="5">編號5asp:ListItem>
                    
<asp:ListItem Value="3">編號3asp:ListItem>
                
asp:DropDownList>
                
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="查詢" />
                
<br />
                
<asp:GridView ID="GridView1" runat="server" BackColor="White" 
                    BorderColor
="#E7E7FF" BorderStyle="None" BorderWidth="1px" CellPadding="3" 
                    GridLines
="Horizontal">
                    
<RowStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" />
                    
<FooterStyle BackColor="#B5C7DE" ForeColor="#4A3C8C" />
                    
<PagerStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" HorizontalAlign="Right" />
                    
<SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="#F7F7F7" />
                    
<HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#F7F7F7" />
                    
<AlternatingRowStyle BackColor="#F7F7F7" />
                
asp:GridView>
                
<br />
            
ContentTemplate>
        
asp:UpdatePanel>
    
    
div>
    
    
form>
body>
html>

 

.Net中用ajax控制元件實現功能(例)
//後臺
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.Sql;
using System.Data.SqlClient;
namespace WebApplication1
{
    
public partial class deafault : System.Web.UI.Page
    {
        
protected void Page_Load(object sender, EventArgs e)
        {

        }

        
protected void Button1_Click(object sender, EventArgs e)
        {
            SqlConnection con 
= new SqlConnection();
            
string conStr = "Data Source=GMSOFT-0ZEME6EL;Initial Catalog=Northwind;Persist Security Info=True;User ID=sa;Password=sa";
            con.ConnectionString 
= conStr;
            con.Open();

            
string id = this.DropDownList1.SelectedValue;
            
string sql = "select * from orders where shipvia=3 and employeeid=" + id;
            SqlDataAdapter da 
= new SqlDataAdapter(sql, con);
            DataSet ds 
= new DataSet();
            da.Fill(ds);
            con.Close();
            GridView1.DataSource 
= ds;
            GridView1.DataBind();
        }
    }
}

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/12639172/viewspace-609078/,如需轉載,請註明出處,否則將追究法律責任。

相關文章