asp.net一些常用功能

程序人生   2008-04-03 10:30   阅读1152   评论0  
字号:    

 

 

38、分页:

分页事件:PageIndexChanging

    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)

{

GridView1.PageIndex = e.NewPageIndex;

bind();

}

 

 

//分页序号递增的自动编号:(测试,这个其实不要没关系)

if (e.Row.RowIndex== -1)

{

int indexID= GridView1.PageIndex * GridView1.PageSize + e.Row.RowIndex + 1;

e.Row.Cells[0].Text = indexID.ToString();

}

}

 

 

37、判断显示的问题:

方法一:

Text='<%# Eval("r_audited").ToString()=="0"?"未审核":"已审核" %>'

方法二:

SELECT  

        CASE   sex  

              WHEN   '1'   THEN   '男'  

              WHEN   '0'   THEN   '女'  

            ELSE   '不男不女?哈哈'  

        END   AS   sex  

  from   表  

   

方法三:

      if(e.Row.Cells[列的索引].Text=="0")  

          {  

              e.Row.Cells[列的索引].Text="男";  

          }  

      else  

          {

 

            e.Row.Cells[列的索引].Text="女";  

          }  

 

 

36、GridView根据条件改变行颜色

public void productsGridView_RowDataBound(object sender,

GridViewRowEventArgs e)

{

 if (e.Row.RowType == DataControlRowType.DataRow)

 {

  int unitsInStock = Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "UnitsInStock"));

  if (unitsInStock == 0)

   e.Row.BackColor = Color.Yellow;

 }

}

 

 

35、审核

  

1、GridView1中创建按纽列,按纽列的CommandName = OpenRow                    //确定按纽名称,用于区别有多个按纽列;

2、GridView1的属性: OnRowCommand = "btnOpenClick"  DataKeyNames="ps_id,ps_name"  //绑定行方法

3、双击 OnRowCommand 属性产生:

    protected void btnOpenClick(object sender, GridViewCommandEventArgs e)

    {

                

    }

4、写入:

    protected void btnOpenClick(object sender, GridViewCommandEventArgs e)

    {

        if (e.CommandName == "OpenRow")  //如果是OpenRow按纽

        {

            int RowIndex = Convert.ToInt32(e.CommandArgument); //行INDEX

            DataKey keys = GridView1.DataKeys[RowIndex];       //行中的数据

            int id = (int)keys.Values["ps_id"];

            string name = (string)keys.Values["ps_name"];

            Response.Write(id.ToString() + "-" + name);

        }

        

    }

 

 

34、删除

 

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)

{

//如果是绑定数据行

if (e.Row.RowType == DataControlRowType.DataRow)

{

if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)

{

((LinkButton)e.Row.Cells[22].Controls[0]).Attributes.Add("onclick", "javascript:return confirm('你确认要删除:\"" + e.Row.Cells[1].Text + "\"吗?')");

}

}

 

if (e.Row.RowType == DataControlRowType.DataRow)

{

//鼠标经过时,行背景色变

e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='#E6F5FA'");

//鼠标移出时,行背景色变

e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='#FFFFFF'");

}

 

 

 

}

 

33、ridview如何把dropdownlist选中的值付到UpdateParameters中?

      ObjectDataSource1.UpdateParameters("abc").DefaultValue = dropdownlist1.SelectedValue

 

 

 

32、获取选择列的主键值

 

      protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)

{

GridViewRow row = GridView1.SelectedRow;

int id = Convert.ToInt32(GridView1.SelectedDataKey.Value);

}

 

 

31、已有打开的与此命令相关联的 DataReader,必须首先将它关闭

同时打开建立两个DataReader,而采用同一个SqlConnection;

解决方法: 两个DataReader用不同的SqlConnection;

注意: DataReader依托于SqlConnection,如果SqlConnection关闭了,DataReader也就自动消亡了~

 

 

 

30、Excel导出的问题:

 

<%@ Page Language="C#"  EnableEventValidation = "false" ... %>

using System.IO;

using System.Text;

 

protected void Button1_Click(object sender, EventArgs e)

{

Export("application/ms-excel", "门店日佣金表.xls");

}

 

private void Export(string FileType, string FileName)

{

Response.Charset = "GB2312";

Response.ContentEncoding = System.Text.Encoding.UTF7;

Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, Encoding.UTF8).ToString());

Response.ContentType = FileType;

this.EnableViewState = false;

StringWriter tw = new StringWriter();

HtmlTextWriter hw = new HtmlTextWriter(tw);

GridView1.RenderControl(hw);

Response.Write(tw.ToString());

Response.End();

}

 

public override void VerifyRenderingInServerForm(Control control)

{

}

 

 

 

29、Server Application Unavailable

The web application you are attempting to access on this web server is currently unavailable.  Please hit the "Refresh" button in your web browser to retry your request.

Administrator Note: An error message detailing the cause of this specific request failure can be found in the application event log of the web server. Please review this log entry to discover what caused this error to occur.

 

 

 

Compiler Error Message: CS0016: Could not write to output file 'd:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\root\3aba5b02\e9f70ae8\App_Web_home.aspx.cdcab7d2.qkocgonl.dll' -- 'Access is denied. '

 

C:\WINDOWS\temp-->右键属性

 

 

 

 

28、DataFormatString设置了但不起作用:

注意:在ASP.NET 2.0中应设置HtmlEncode为false,否则DataFormatString不起作用

 

27、合并单元格:

e.Row.Cells[34].ColumnSpan = 3;

 

26、隐藏列的方法:

          GridView1.Columns[0].Visible = false;   //这种隐藏放在绑定后,被隐藏的列依然是绑定的,如果在前台中直接设置visable=false就不绑定。

评论(?)
阅读(?)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
网易公司版权所有 ©1997-2009