C#项目积累经验二

这篇博客介绍了ASP.NET中的一些实用技巧,包括为GridView添加全选功能,遍历并检查Gridview中Checkbox的值,MySql事务处理的开始、提交和回滚操作,文件下载的实现方法,以及在关闭窗口时执行其他窗口事件的处理。此外,还提到了GridView的分页功能实现。

一、         ASP.NETGridView加入全选--- 1

二、         如何遍历Gridview中的Checkbox呢?检查被选中的checkbox的值呢?--- 2

三、         MySql事务处理--- 2

四、         事务实例--- 4

五、         ASP.ENT实现文件下载--- 4

六、         ASP.NET中如何在关闭一个窗口时执行另一个窗口中的某个事件???--- 5

七、         GridView分页功能--- 5

 

一、ASP.NETGridView加入全选

http://www.diybl.com/course/4_webprogram/asp.net/asp_netshl/2008324/106435.html

http://tech.sina.com.cn/s/2008-06-24/1050705942.shtml

 

Gridview中加入下列模板:

<asp:TemplateField HeaderText="全选">
        <HeaderTemplate>
                <asp:CheckBox ID="checkall" runat="server" Text="
全选"
                        AutoPostBack="true" OnCheckedChanged="checkAll" />
         </HeaderTemplate>
          <ItemTemplate>
                   <asp:CheckBox ID="check" runat="server" Text="
选择" />
            </ItemTemplate>    
</asp:TemplateField>

CS文件中加入下列方法:

   protected void checkAll(object sender, EventArgs e)
  {
       CheckBox cb = (CheckBox) sender;
       if (cb.Text == "
全选")
       {
           foreach (GridViewRow gvr in gvLessons.Rows)
           {
               CheckBox cb1 = (CheckBox)gvr.FindControl("check");
               cb1.Checked = cb.Checked;
           }
       }

 

二、       如何遍历Gridview中的Checkbox呢?检查被选中的checkbox的值呢?

 

http://topic.csdn.net/u/20070224/16/456dc753-84de-4e56-8d96-c80777fcf2f8.html

 

protected   void   btnDel_Click(object   sender,   EventArgs   e)
        {
                int   intRowCount   =   this.GridView1.Rows.Count;
                string   strInfo   =   string.Empty;

                for   (int   i   =   0;   i   <   intRowCount;   i++)
                {
                        CheckBox   chk   =   (CheckBox)   this.GridView1.Rows[i].Cells[0].FindControl( "chkDelete ");
                        if   (chk.Checked)
                        {
                                this.GridView1.Rows[i].Visible   =   false;
                                strInfo   +=   this.GridView1.Rows[i].Cells[1].Text   +   "
被选中
. "   +   " <br> ";
                        }
                }

                this.lblInfo.Text   =   strInfo;
        }

 

三、       MySql事务处理

       //////////////////////////////////////////////////////////////////////////自己添加的两个关于事务的方法

        /// <summary>

        /// 开始事务

        /// </summary>

        public static MySqlTransaction BeginTran()

        {

           

            MySqlConnection conn = new MySqlConnection(MySqlConnectionString);

 

            if (conn.State != ConnectionState.Open)

                conn.Open();

 

            MySqlTransaction tran =conn.BeginTransaction();

           

            return tran;

        }

        /// <summary>

        /// 提交事务

        /// </summary>

        /// <param name="tran"></param>

        /// <returns></returns>

        public static void SubmitTran(MySqlTransaction tran)

        {

            tran.Commit();

        }

        /// <summary>

        /// 回滚事务

        /// </summary>

        /// <param name="tran"></param>

        public static void RollBackTran(MySqlTransaction tran)

        {

            tran.Rollback();

        }

 

  public bool SendLetter(MailInfo mailInfo, IList<AccessoryInfo> AccessoryList, string[] UName, string UID)

        {

            MySqlTransaction tran = MySQLUtility.MySQLADO.BeginTran();

            try

            {

                //得到邮件编号

                int sl_ID = SendLetterSender(tran, mailInfo);

 

                ///有附件的时候才对附件表进行操作

                if (AccessoryList.Count > 0)

                {

                    SendLetterAccessory(tran, AccessoryList, sl_ID, UID);

                }

 

                SendLetterReceive(tran, sl_ID, UName, false);

 

                MySQLADO.SubmitTran(tran);

                return true;

            }

            catch (System.Exception e)

            {

                Console.WriteLine("The process failed: {0}", e.ToString());

                MySQLADO.RollBackTran(tran);

                return false;

            }

        }

 

四、       事务实例

        if (username.Text != "" && username.Text != null)

        {

            staticUserInfo = userLogin.isValid(username.Text.ToString());

            if (staticUserInfo != null)

            {

                Session.Add("user", staticUserInfo);

                Response.Redirect("Main.aspx");

            }

            else

                //Response.Write("用户名或密码错误!");

            Response.Write("<script type=text/javascript>alert(/"用户名或密码错误/");</script>");

        }

五、       ASP.ENT实现文件下载

http://hi.baidu.com/junval/blog/item/0a04930ad469cc3eb0351d53.html

 

StreamWriter sr;
string path=Server.MapPath("
文件的相对路径
");
if(File.Exists(path))
{
File.Delete(path);
}
sr=File.CreateText(path);
sr.Close();
sr=new StreamWriter(path,false,Encoding.GetEncoding("GB2312"));
sr.WriteLine("
姓名,昵称
");
string a="Mike",b="Michael";
sr.WriteLine(a+","+b);
sr.Close();
FileStream fileStream=new FileStream(path,FileMode.Open);
long fileSize = fileStream.Length;
Response.ContentType="application/octet-stream";
Response.AddHeader("Content-Disposition","attachment; filename=" + HttpUtility.UrlEncode("
保存后的文件名
",Encoding.UTF8));
Response.AddHeader("Content-Length",fileSize.ToString());
byte[] fileBuffer=new byte[fileSize];
fileStream.Read(fileBuffer,0,(int)fileSize);
fileStream.Close();
Response.BinaryWrite(fileBuffer);
Response.End();

 

实例二

 string file_name = "文件名字";
   string FilePath = "
文件路径
";
   Page.Response.Clear();
   Response.ContentType="application/octet-stream";
   Response.AddHeader("Content-Disposition","attachment; filename=" + HttpUtility.UrlEncode(file_name,System.Text.Encoding.UTF8));
   Page.Response.WriteFile(FilePath);
   Page.Response.End(); 

 

六、       ASP.NET中如何在关闭一个窗口时执行另一个窗口中的某个事件???

在子窗体的某个事件中增加如下的代码来进行返回  
  string   strScript="<script>"  
  +   "   window.parent.returnValue='"   +   yourValue   +   "';"  
  +   "   window.parent.close();"  
  +"</script>";  
  if(   !   IsClientScriptBlockRegistered("clientScript")   )  
  RegisterClientScriptBlock(   "clientScript",   strScript   );  

 

 

七、       GridView的分页功能

1.    拖控件

一个gridview控件,一个SqlDataSource控件,

2.首先在CS的代码 Page_Load函数中绑定数据

   

    protected void Page_Load(object sender, EventArgs e)

    {

        GetSession();

   

        if (!IsPostBack)

        {

            BindGridView();

            btnDelMail.Attributes.Add("onclick", "return confirm('确定删除邮件?');");

 

        }

    }

 

    private void BindGridView()

    {

 

        if (Loginer != null)

        {

            mails = mail.GetMailByUserNameRead(Loginer.Name, false);

            gdvMail.DataSource = mails;

            gdvMail.DataBind();

            Session["mails" + Loginer.Name] = mails;

        }

}

 

3.Gridview添加PageIndexChanging事件

    protected void gdvMail_PageIndexChanging(object sender, GridViewPageEventArgs e)

    {

        gdvMail.PageIndex = e.NewPageIndex;

        BindGridView();

    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值