现在的位置: 首页 > 综合 > 正文

[zt]ASP.NET中Session与DATAGRID的综合应用

2013年09月11日 ⁄ 综合 ⁄ 共 10737字 ⁄ 字号 评论关闭

// ********************************************************************************
//Name:SysResearchA_Z
//Function:问卷发布
//Author:zql
//Create time:2004/09/02
//Version:1.0
//Related Table:InfResearch,InfResProject,InfResOption
//Related Class:
// ********************************************************************************
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Text;
using AAAAAA.COM;

namespace AAAAA.InfoCenter.Sys
{
 /// <summary>
 /// SysResearchA_Z 的摘要说明。
 /// </summary>
 public class SysResearchA_Z : System.Web.UI.Page
 {
  protected System.Web.UI.WebControls.TextBox txtSubject;
  protected System.Web.UI.WebControls.DataGrid DataGrid1;
  protected System.Web.UI.HtmlControls.HtmlForm Form1;
  protected System.Web.UI.WebControls.Label Label1;
  protected System.Web.UI.WebControls.Label Label2;
  protected System.Web.UI.WebControls.Label Label3;
  protected System.Web.UI.WebControls.Button btnPrjAdd;
  protected System.Web.UI.WebControls.TextBox txtShow1;
  protected System.Web.UI.WebControls.DropDownList drpType;
  protected System.Web.UI.WebControls.TextBox txtProject;
  protected System.Web.UI.WebControls.Label Label4;
  protected System.Web.UI.WebControls.Label Label6;
  protected System.Web.UI.WebControls.DropDownList drpShow;
  protected System.Web.UI.WebControls.Button btnContent;
  protected System.Web.UI.WebControls.DataGrid DataGrid2;
  protected System.Web.UI.WebControls.TextBox txtContent;

  DataTable Research1;
  DataView ResearchView1;
  DataTable Content;
  protected System.Web.UI.WebControls.Button btSend;
  protected System.Web.UI.WebControls.Button btCancel;
  DataView ContentView;
  DateTime dTime;
 
  private void Page_Load(object sender, System.EventArgs e)
  {
   // 在此处放置用户代码以初始化页面
   //************** *temp code fro test ******************
   Session["userid"] = "61844785-B2D1-4B2A-9D01-E671A56C6386";
   Session["usertype"] = "S";
   Session["userright"] = "m,012";
   Application["elibid"] = "027B9961-F00A-4594-8775-775D9E5F8E4E";
   //************** *temp code fro test ******************

//   for(i=0;i<10;i++)
//   {
//    row = tb.NewRow();
//    tb.Rows.Add(row);
//   }

   if (Session["Research1"]==null)
   {
    Research1 = new DataTable();
    Research1.Columns.Add(new DataColumn("ShowNo",typeof(string)));
    Research1.Columns.Add(new DataColumn("Type",typeof(string)));
    Research1.Columns.Add(new DataColumn("Project",typeof(string)));
//    DataRow row;
//    for (int i=0;i<10;i++)
//    {
//     row = Research1.NewRow();
//     Research1.Rows.Add(row);
//    }
    Session["Research1"] = Research1;
   }
   else
   {
    Research1 = (DataTable)Session["Research1"];
   }
   ResearchView1 = new DataView(Research1);
   DataGrid1.DataSource = ResearchView1;
   ResearchView1.Sort="ShowNo";
   DataGrid1.DataBind();

   if (Session["Content"]==null)
   {
    Content = new DataTable();
    Content.Columns.Add(new DataColumn("ShowNo",typeof(string)));
    Content.Columns.Add(new DataColumn("Content",typeof(string)));
    Session["Content"] = Content;
   }
   else
   {
    Content = (DataTable)Session["Content"];
   }
   ContentView = new DataView(Content);
   DataGrid2.DataSource = ContentView;
   ContentView.Sort="ShowNo";
   DataGrid2.DataBind();
  }

  #region Web 窗体设计器生成的代码
  override protected void OnInit(EventArgs e)
  {
   //
   // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
   //
   InitializeComponent();
   base.OnInit(e);
  }
 
  /// <summary>
  /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  /// 此方法的内容。
  /// </summary>
  private void InitializeComponent()
  {   
   this.btnPrjAdd.Click += new System.EventHandler(this.btnPrjAdd_Click);
   this.DataGrid1.EditCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_EditCommand);
   this.DataGrid1.DeleteCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_DeleteCommand);
   this.btnContent.Click += new System.EventHandler(this.btnContent_Click);
   this.DataGrid2.EditCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid2_EditCommand);
   this.DataGrid2.DeleteCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid2_DeleteCommand);
   this.btSend.Click += new System.EventHandler(this.btSend_Click);
   this.btCancel.Click += new System.EventHandler(this.btCancel_Click);
   this.Load += new System.EventHandler(this.Page_Load);

  }
  #endregion

  private void btnPrjAdd_Click(object sender, System.EventArgs e)
  {
   //将新增加的数据加入到GRID中
   if (txtShow1.Text=="")
   {
    Response.Write("<script>alert('显示顺序不能为空.');</script>");
    return;
   }
   if (txtProject.Text=="")
   {
    Response.Write("<script>alert('项目不能为空.');</script>");
    return;
   }
   if (btnPrjAdd.Text == "保存")
   {
    btnPrjAdd.Text = "增加";  //将按钮重命名
    //    ResearchView1.Delete(delflg);  //先删除该记录
    ResearchView1.Delete((int)Session["delflg"]);  //先删除该记录
    //    delflg = -1;//置标志为-1
   }
   DataRow dr = Research1.NewRow();
      dr[0] = txtShow1.Text.ToString();
   dr[1] = drpType.SelectedItem.Value.ToString();
   dr[2] = txtProject.Text.ToString();
   Research1.Rows.Add(dr);  //新增加一行数据
   DataGrid1.DataBind();
   drpShow.DataSource =ResearchView1;
   drpShow.DataValueField = "ShowNo";
   drpShow.DataBind();
//   for (int i=0;i<(10-Convert.ToInt32(Research1.Rows.Count.ToString())/10);i++)
//   {
//    dr = Research1.NewRow();
//    Research1.Rows.Add(dr);
//   }

   //清空输入
   txtShow1.Text ="";
   txtProject.Text ="";
  }

  private void DataGrid1_DeleteCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
  {
   //删除行
   ResearchView1.Delete(e.Item.ItemIndex);//本行删除当前选定的行
   DataGrid1.DataBind();
   drpShow.DataBind();
   //如果按了修改后又按删除,需要将增加按钮重命名
   btnPrjAdd.Text = "增加";
  }

  private void DataGrid1_EditCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
  {
   //delflg = DataGrid1.TabIndex;//获取当前选择的行号
   Session["delflg"] = e.Item.ItemIndex;
   txtShow1.Text=e.Item.Cells[0].Text.ToString();//取得行值
   drpType.SelectedValue = e.Item.Cells[1].Text.ToString();
   txtProject.Text = e.Item.Cells[2].Text.ToString();
   btnPrjAdd.Text = "保存";//更改增加按钮名称
  }

  private void DataGrid2_DeleteCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
  {
   //删除行
   ContentView.Delete(e.Item.ItemIndex);//本行删除当前选定的行
   DataGrid2.DataBind();
   //如果按了修改后又按删除,需要将增加按钮重命名
   btnContent.Text = "增加";
  }

  private void DataGrid2_EditCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
  {
   Session["cdelflg"] = e.Item.ItemIndex;//获取当前选择的行号
   drpShow.SelectedValue  = e.Item.Cells[0].Text.ToString();
   txtContent.Text = e.Item.Cells[1].Text.ToString();
   btnContent.Text = "保存";//更改增加按钮名称
  }

  private void btnContent_Click(object sender, System.EventArgs e)
  {
   //将新增加的数据加入到GRID中
   if (txtContent.Text=="")
   {
    Response.Write("<script>alert('内容不能为空.');</script>");
    return;
   }
   if (btnContent.Text == "保存")
   {
    btnContent.Text = "增加";  //将按钮重命名
    ContentView.Delete((int)Session["cdelflg"]);  //先删除该记录
   }
   DataRow dr = Content.NewRow();
   dr[0] = drpShow.SelectedItem.Value.ToString();
   dr[1] = txtContent.Text.ToString();
   Content.Rows.Add(dr);  //新增加一行数据
   DataGrid2.DataBind();
   //清空输入
   txtContent.Text ="";
  }

  private void delResearch(DateTime dTime)
  {
   //将所有的数据删除
   string strSQL;
   strSQL = "delete from InfResearch where IssuanceTime='"+dTime+"'";
   LocalDBDealer iv = new LocalDBDealer();
   bool success = iv.ExeCommandBool(strSQL);
   if(!success)
   {
    Response.Write("<script>alert('删除数据库表InfResearch失败');</script>");
    //return;
   }
   strSQL = "delete from InfResProject where CreateTime='"+dTime+"'";
   success = iv.ExeCommandBool(strSQL);
   if(!success)
   {
    Response.Write("<script>alert('删除数据库表InfResProject失败');</script>");
    //return;
   }
   strSQL = "delete from InfResOption where CreateTime='"+dTime+"'";
   success = iv.ExeCommandBool(strSQL);
   if(!success)
   {
    Response.Write("<script>alert('删除数据库表InfResOption失败');</script>");
    //return;
   }
  }

  private void btSend_Click(object sender, System.EventArgs e)
  {
   //提交前要检查
   string RtnStr;
  
   if((RtnStr=Verify.CheckInput("主题",this.txtSubject.Text,100,false))!="")
   {
    Response.Write("<script>alert('" + RtnStr + "');</script>");
    return;
   }
   if (DataGrid1.Items.Count.ToString()=="0")
   {
    Response.Write("<script>alert('没有项目数据,不能提交.');</script>");
    return;
   }

   if (DataGrid2.Items.Count.ToString()=="0")
   {
    Response.Write("<script>alert('没有内容数据,不能提交.');</script>");
    return;
   }
   //先将数据保存到INFRESEARCH表中(Research1)
   string strSQL;
   dTime = DateTime.Now;
   strSQL = "insert into InfResearch (Subject,state,IssuanceTime,Ballot,Creater,CreateTime) values ( '";
    strSQL = strSQL+ txtSubject.Text.Trim() + "','1','" ;//1为编辑状态
    strSQL = strSQL+ dTime + "',0,'";
    strSQL = strSQL+ Session["userid"].ToString() + "','";
    strSQL = strSQL+ dTime+ "')";
   LocalDBDealer iv = new LocalDBDealer();
   bool success = iv.ExeCommandBool(strSQL);
   if(!success)
   {
    Response.Write("<script>alert('写数据库失败');</script>");
    delResearch(dTime);
   }
   //获取GUID
   strSQL = "select ResearchId from InfResearch where Subject = '" + txtSubject.Text.Trim() + "' and IssuanceTime='"+dTime+"'";
   DataSet ds = iv.ExeSelectDS(strSQL);
   string getGuid ="0";
   foreach(DataRow myRow in ds.Tables[0].Rows)
   {
     getGuid= myRow["ResearchId"].ToString();
   }
   //检查Research1中有多少行数据,确定循环
   //int ss = Convert.ToInt32(Research1.Rows.Count.ToString());
   //读取数据
   //string aa = Research1.Rows[0][0].ToString();
   //写到数据库InfResProject
   for (int i=0;i<Convert.ToInt32(Research1.Rows.Count.ToString());i++)
   {
    //aa=Research1.Rows[i][0].ToString();
    strSQL = "insert into InfResProject (ResearchId,ShowOrder,ButtType,Project,Creater,CreateTime) values ( '";
    strSQL = strSQL+ getGuid + "','"+Research1.Rows[i][0].ToString()+"','";
    strSQL = strSQL+ Research1.Rows[i][1].ToString()+"','";
    strSQL = strSQL+ Research1.Rows[i][2].ToString()+"','";
    strSQL = strSQL+ Session["userid"].ToString() + "','";
    strSQL = strSQL+ dTime+ "')";
    success = iv.ExeCommandBool(strSQL);
    if(!success)
    {
     Response.Write("<script>alert('写数据库失败');</script>");
     delResearch(dTime);
    }
   }
   //写到数据库表InfResOption
   for (int i=0;i<Convert.ToInt32(Content.Rows.Count.ToString());i++)
   {
    //先取得GUID

    strSQL = "select ResProjectId from InfResProject where ShowOrder = '" + Content.Rows[i][0].ToString() + "' and CreateTime='"+dTime+"'";
    ds = iv.ExeSelectDS(strSQL);
    foreach(DataRow myRow in ds.Tables[0].Rows)
    {
     getGuid= myRow["ResProjectId"].ToString();
    }
    strSQL = "insert into InfResOption (ResProjectId,Content,Creater,CreateTime) values ( '";
    strSQL = strSQL+ getGuid + "','"+Content.Rows[i][1].ToString()+"','";
    strSQL = strSQL+ Session["userid"].ToString() + "','";
    strSQL = strSQL+ dTime+ "')";
    success = iv.ExeCommandBool(strSQL);
    if(!success)
    {
     Response.Write("<script>alert('写数据库失败');</script>");
     delResearch(dTime);
    }
   }
   Response.Write("<script>alert('调查表提交成功.');</script>");
   btSend.Enabled = false;  
  }

  private void btCancel_Click(object sender, System.EventArgs e)
  {
   //清空所有的数据
   Session["Research1"] = null;
   Session["Content"]= null;
   DataGrid1.DataSource = null;
   DataGrid2.DataSource = null;
   DataGrid1.DataBind();
   DataGrid2.DataBind();
   this.drpShow.DataSource = null;
//   drpShow.DataValueField = "";
//   drpShow.DataBind();
   drpShow.Items.Clear();
   Page_Load(sender,e);
   btSend.Enabled = true;
  }
 }
}

抱歉!评论已关闭.