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

asp.net2.0乱记(27)两个低级错误

2013年12月07日 ⁄ 综合 ⁄ 共 2927字 ⁄ 字号 评论关闭

大脑变得很不灵光,经常卡在一些小问题上 。想起来还有很多东西没有时间研究,昨天想转向C/S部分的开发,C#还是蛮有趣的。web控件如果也有那么丰富就好了。
 
  第一,导出excel时在表中总是有一个input的控件,使用的是网上找来的一段代码,以前用过没有什么问题,现在用用,页面中的viewstate的隐藏控件总是会显示在excel文件第一行第一列上
  

    public void OutPutExcel()
    
{
        
//定义文档类型、字符编码
        this.EnableViewState = false;
        Response.Clear();
        Response.Buffer 
= true;
        Response.Charset 
= "GB2312";
        
//下面这行很重要, attachment 参数表示作为附件下载,您可以改成 online在线打开
        
//filename=FileFlow.xls 指定输出文件的名称,注意其扩展名和指定文件类型相符,可以为:.doc    .xls    .txt   .htm

        Response.AppendHeader(
"Content-Disposition""attachment;filename=FileFlow.xls");
        Response.ContentEncoding 
= System.Text.Encoding.GetEncoding("GB2312");

        
//Response.ContentType指定文件类型 可以为application/ms-excel、application/ms-word、application/ms-txt、application/ms-html 或其他浏览器可直接支持文档
        Response.ContentType = "application/ms-excel";

        
// 定义一个输入流
        System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
        System.Web.UI.HtmlTextWriter oHtmlTextWriter 
= new System.Web.UI.HtmlTextWriter(oStringWriter);

        
this.RenderControl(oHtmlTextWriter);
        
//this 表示输出本页,你也可以绑定datagrid,或其他支持obj.RenderControl()属性的控件
        Response.Write(oStringWriter.ToString());


        Response.End();
    }

    问题在于        this.RenderControl(oHtmlTextWriter);这句上
    this在这个过程里的默认代表的是ThisPage,打印了整个页面,后来改成GridView1就可以通过了,呼。。。
   
   
第二,页面中部分代码,使用GridView来显示数据,允许进行Edit和Update行:

    protected void mydatabind()
    
{
         UserDB.CommonSelect(
"select * from T_user",GridView1 );
    }

    
protected void Page_Load(object sender, EventArgs e)
    
{
            mydatabind();
    }

    
protected void gd_OnRowEditing(object sender, GridViewEditEventArgs e)
    
{
        GridView1.EditIndex 
= e.NewEditIndex;
        mydatabind();

    }

    
protected void gd_OnRowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    
{
        GridView1.EditIndex 
= -1;
        mydatabind();
    }

    
protected void gd_OnRowUpdating(object sender, GridViewUpdateEventArgs e)
    
{
        Label lb 
=(Label ) GridView1.Rows[e.RowIndex].FindControl("Label1");
        TextBox tb
=(TextBox )GridView1 .Rows [e.RowIndex ].FindControl("TextBox2");
        
if (UserDB.CommonDelete("update T_user set password='"+tb.Text .Trim ()+"' where username='" + lb.Text.Trim() + "'"))
        
{
            Label1.Text 
= "Modify Successfully.";
            GridView1.EditIndex 
= -1;
        }

        
else
        
{
            Label1.Text 
= "Modify Failed.";
        }

        mydatabind();
    }

    出现的问题是:每一次进行编辑时,可以让一行处于编辑状态,但是无论怎么update,数据都不会改变,画面也不产生变化,我怀疑是事件问题,又用了OnRowUpdated事件,打印测试数据,同样无法输出测试,下断点也断不下来,郁闷至极。
    后来发现问题十分幼稚,原来是在page_load里应该这样写,只在第一次加载时绑定数据:

    protected void Page_Load(object sender, EventArgs e)
    
{
        
if (!IsPostBack)
        
{
            mydatabind();
        }

    }

    原来的代码在每一次提交时重新绑定了gridview的数据,就算是有修改,也是改成了以前的数据,不过我在事件中下断点都没有断下来看,由于gridview的重新绑定,OnRowUpdating的事件根本没有执行。事件也可以进行覆盖?不了不了。。。 

 

抱歉!评论已关闭.