2011/8/31

自我修練 – ASP.net專題實務

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DBAccessLib;
using System.Data.Odbc;
using System.Text.RegularExpressions;

public partial class WebPart_wkh_TopsTicket_Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {       
        const int offset = -3;
        Label1.Text = "";

        if (!Page.IsPostBack)
        {

//抓入當日日期的方法, 但注意要放在if (!Page.IsPostBack)中,以免新設定的值被洗掉。
            tbFromYear.Text = DateTime.Today.AddDays(offset).Year.ToString();
            tbFromMonth.Text = DateTime.Today.AddDays(offset).Month.ToString();
            tbFromDay.Text = DateTime.Today.AddDays(offset).Day.ToString();
            tbToYear.Text = DateTime.Today.Year.ToString();
            tbToMonth.Text = DateTime.Today.Month.ToString();
            tbToDay.Text = DateTime.Today.Day.ToString();
        }
    }

    protected void Calendar1_SelectionChanged(object sender, EventArgs e)
    {

//以button呼叫日曆控制項的方法, 看起來真的很笨, 只是把非隱藏設成隱藏

        tbFromYear.Text = Calendar1.SelectedDate.Year.ToString();
        tbFromMonth.Text = Calendar1.SelectedDate.Month.ToString();
        tbFromDay.Text = Calendar1.SelectedDate.Day.ToString();
        Calendar1.Visible = false;
        Button2.Enabled = true;
    }

    protected void Button1_Click(object sender, EventArgs e)
    {

//以button呼叫日曆控制項的方法, 看起來真的很笨, 只是把隱藏設成非隱藏
        Calendar1.Visible = true;
        Button2.Enabled = false;
        Calendar1.Focus();
    }

    protected void Calendar2_SelectionChanged(object sender, EventArgs e)
    {
        tbToYear.Text = Calendar2.SelectedDate.Year.ToString();
        tbToMonth.Text = Calendar2.SelectedDate.Month.ToString();
        tbToDay.Text = Calendar2.SelectedDate.Day.ToString();
        Calendar2.Visible = false;
        Button1.Enabled = true;
    }

    protected void Button2_Click(object sender, EventArgs e)
    {
        Calendar2.Visible = true;
        Button1.Enabled = false;
        Calendar2.Focus();
    }
    protected void btSubmit_Click(object sender, EventArgs e)
    {
        string DSN_RCA = Common.ToGetDsnByNCSR("R");
        String SQLStr = "";

        if(tbValue.Text.Length == 0) {           
        }

        if (rblFeature.SelectedValue == "dispatch")
        {
            SQLStr = "select orderid as 聯單號碼, exgname as 交換機, dispatchuserid as 操作員代碼,";
            SQLStr += " dispatchtime as 時間 from tkdispatch where dispatchtime >= \'";
            SQLStr += tbFromYear.Text + "-" + tbFromMonth.Text + "-" + tbFromDay.Text + " 00:00:00\' ";
            SQLStr += " and dispatchtime <= \'" + tbToYear.Text + "-" + tbToMonth.Text + "-" + tbToDay.Text;
            SQLStr += " 23:59:59.99999\'";
            if (rblCondition.SelectedValue == "tel")
            {
                SQLStr += "";
            }
            if (rblCondition.SelectedValue == "number")
            {
                if (Regex.IsMatch(tbValue.Text, @"^[1-9][0-9]*"))
                {
                    SQLStr += " and orderid =" + tbValue.Text;
                }
                else
                {
                    Label1.Text = "聯單號碼格非數字,改為搜尋全部條件。";
                }
            }
            if (rblCondition.SelectedValue == "op")
            {
                SQLStr += " and dispatchuserid like \'%" + tbValue.Text + "%\'";
            }
        }

        if (rblFeature.SelectedValue == "reply")
        {
            SQLStr = "select orderid as 聯單號碼, replyvalue as 回報代碼, replyuserid as 操作員代碼,";
            SQLStr += " replytime as 時間 from tkreply where replytime >= \'";
            SQLStr += tbFromYear.Text + "-" + tbFromMonth.Text + "-" + tbFromDay.Text + " 00:00:00\' ";
            SQLStr += " and replytime <= \'" + tbToYear.Text + "-" + tbToMonth.Text + "-" + tbToDay.Text;
            SQLStr += " 23:59:59.99999\'";
            if (rblCondition.SelectedValue == "tel")
            {
                SQLStr += "";
            }
            if (rblCondition.SelectedValue == "number")
            {
                if (Regex.IsMatch(tbValue.Text, @"^[1-9][0-9]*"))
                {
                    SQLStr += " and orderid =" + tbValue.Text;
                }
                else
                {
                    Label1.Text = "聯單號碼格非數字,改為搜尋全部條件。";
                }
            }
            if (rblCondition.SelectedValue == "op")
            {
                SQLStr += " and replyuserid like \'%" + tbValue.Text + "%\'";
            }
        }

        //Label1.Text = SQLStr;

 

//好用的GridView設定方法, 要把DataReader餵過去就ok了
        DBAccess db1 = new DBAccess();
        OdbcDataReader dr = db1.DataReader(….);
        GridView1.DataSource = dr;
        GridView1.DataBind();
        GridView1.Visible=true;

        dr.Close();
    }
}

沒有留言:

張貼留言