随笔简介:积累技术,免得遗忘
环境:VS2008,sql 2005,系统自带数据库
1.功能,在dropdownlist中选择查询条件,点击按钮查询,无刷新gridview限制各条件下的数据
2.实现方式,在body中添加一个scriptmannager, 接着添加一个updatpannel,然后在里面添加操作
3.原理不解释,我也解释不清楚,菜鸟先学会操作吧,代码如下
//前台
<%@ 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>
//后台
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();
}
}
}