使用基本的ASP.NET服务器端控件的综合实例

 zonghe.aspx页面包括Label1,CheckBoxList1,DropDownList1,DropDownList2,TextBox1,RadioButton1,RadioButton2,Button1,Button2,

Button3,ListBox1

public partial class zonghe : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //查询Northwind数据库中的数据表Order Details(id=325576198)中的数据
        Label1.Text = "一个数据库查询的例子";
        if (!IsPostBack)
        {
            try
            {
                DataSet ds = new DataSet();
                SqlConnection myConnection = new SqlConnection("server=localhost;uid=sa;pwd=860712;database=Northwind;");
                string strSQL = "select name from sysColumns where id=325576198 order by colid";
                SqlDataAdapter myCommand = new SqlDataAdapter(strSQL, myConnection);
                myCommand.Fill(ds, "OrderDetails");
                CheckBoxList1.DataSource = ds.Tables["OrderDetails"].DefaultView;
                CheckBoxList1.DataValueField = ds.Tables["OrderDetails"].Columns[0].ColumnName;
                CheckBoxList1.DataTextField = ds.Tables["OrderDetails"].Columns[0].ColumnName;
                CheckBoxList1.DataBind();
                CheckBoxList1.RepeatDirection = System.Web.UI.WebControls.RepeatDirection.Horizontal;

                DropDownList1.DataSource = ds.Tables["OrderDetails"].DefaultView;
                DropDownList1.DataValueField = ds.Tables["OrderDetails"].Columns[0].ColumnName;
                DropDownList1.DataTextField = ds.Tables["OrderDetails"].Columns[0].ColumnName;
                DropDownList1.DataBind();
            }
            catch
            {
                Response.Write("<script language='javascript'>alert('连接失败!')</script>");
            }
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        //添加一个查询条件到列表框中
        string query;
        string query1="";
        string query2;
        if(RadioButton1.Checked==true)
        {
            query1=" "+RadioButton1.Text.ToString()+" ";
        }
        if(RadioButton2.Checked==true)
        {
            query1=" "+RadioButton2.Text.ToString()+" ";
        }
        query2=DropDownList1.SelectedItem.Text.ToString()+DropDownList2.SelectedItem.Text.ToString()+TextBox1.Text.ToString();
        if(ListBox1.Items.Count==0)
        {
            query=query2;
        }
        else
        {
            query=query1+query2;
        }
        ListBox1.Items.Add(query);
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        //删除某个选中的列表项
        if(ListBox1.Items.Count!=0)
        {
            if(ListBox1.SelectedIndex>-1)
            {
                ListBox1.Items.Remove(ListBox1.SelectedItem);
            }
            else
            {
                Response.Write("<script language='javascript'>alert('你没有选中列表项!')</script>");
            }
        }
    }
    protected void Button3_Click(object sender, EventArgs e)
    {
        string totalQuery="";//存放所有的查询条件
        string showCols="";//存放要显示的所有字段
        //获取列表框中的所有查询条件
        if(ListBox1.Items.Count==0)
        {
            Response.Write("<script language='javascript'>alert('请设定查询条件!')</script>");
        }
        for(int i=0;i<ListBox1.Items.Count;i++)
        {
            if(ListBox1.Items[i].Text.StartsWith(" and "))
            {
                ListBox1.Items[i].Text=ListBox1.Items[0].Text.Substring(5);
            }
            if(ListBox1.Items[i].Text.StartsWith(" or "))
            {
                ListBox1.Items[i].Text=ListBox1.Items[0].Text.Substring(4);
            }
            totalQuery+=ListBox1.Items[i].Text.ToString();
        }
        //获取复选框列表的所有被选择的字段
        if(CheckBoxList1.SelectedIndex>-1)
        {
            for(int j=0;j<CheckBoxList1.Items.Count;j++)
            {
                if(CheckBoxList1.Items[j].Selected==true)
                {
                    showCols+=CheckBoxList1.Items[j].Text.ToString()+",";
                }
            }
        }
        else
        {
            Response.Write("<script language='javascript'>alert('请选择要显示字段的名称!')</script>");
        }
        showCols=" "+showCols.TrimEnd(',')+" ";
        //将查询结果通过另一个文件显示
        Response.Redirect("zonghe1.aspx?showCols="+showCols+"&totalQuery="+totalQuery);
    }
}

zonghe1.aspx包括Label1,GridView1(我的教材书是.net2003,里面用的是DataGrid,但是我现在用的是.net2005)

public partial class zonghe1 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string showCols = Request["showCols"];
        string totalQuery = Request["totalQuery"];
        Label1.Text = "查询结果 " + totalQuery;
        //通过DataView控件显示符合查询条件的数据
        DataSet ds=new DataSet();
        SqlConnection myConnection = new SqlConnection("server=localhost;uid=sa;pwd=860712;database=Northwind");
        string strSQL = "select " + showCols + " from [Order Details] where " + totalQuery;
        SqlDataAdapter myCommand = new SqlDataAdapter(strSQL, myConnection);
        myCommand.Fill(ds,"OrderDetails1");
        if (ds.Tables["OrderDetails1"].Rows.Count != 0)
        {
            GridView1.DataSource = ds.Tables["OrderDetails1"].DefaultView;
            GridView1.DataBind();
        }
        else
        {
            Label1.Text = "没有符合查询条件的数据!";
        }
    }
}
原来是这里出错:
string strSQL ="select_"+showCols+"_from [Order Details] where_"+totalQuery;
少了三个空格(用_表示的地方)

现在可以完全运行,不过还是有蛮多不足的地方:

1、第一次运行对添加、删除选项单击没有反应;

2、文本框内容为空的时候,单击添加也会添加,不能进行判断;

3、添加,删除操作进行几次以后,ListBox为空的时候,点删除按钮也没反应;

4、运行以后,再返回首页,则提交按钮不能进行判断。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值