GridView

<appSettings>

<add key="ConnectionString" value="server=(local);uid=sa;pwd=;database=News"/>

</appSettings>

string strConn = System.Configuration.ConfigurationManager.AppSettings["ConnectionString"];

 

<connectionStrings>

<add name="Conn" connectionString="server=192.168.1.3;database=edubase;uid=sa;pwd=^Y&amp;U*I(O;"/>

</connectionStrings>

string strConn = System.Configuration.ConfigurationManager.connectionString["ConnectionString"];

 

截取字符串: Text='<%# (Eval("NewsBody")).ToString().Substring(0,10) %>'

 

货币格式:DataFormatString{0:c}  HtmlEncode False

 

没有时以默认值填充:编辑列->属性NullDisplayText

 

编辑时只读:把该列变为模板列,设置其控件属性为ReadOnly.

 

改变编辑状态下textbox的宽度:编辑列->样式CointrolStyle  Width

 

判断当前行是不是数据行:

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

DataList中if(e.item.itemtype==)

 

用Literral显示HTML:属性Mode设置为Encode

 

使用FindControl找控件时,所找的控件必须放在模板列中。

 

 

光盘笔记:

GridView中"编辑列"->ButtonField,指定CommandName的属性以增加删除、取消等功能。

 

可以通过SelectedValue 直接取得所选中行的关键字

this.Label2.Text=this.GridView1.SelectedValue.ToString();

 

快速监视的使用。

 

列的排序:

1.编辑列->sortExperssion

2.属性AllowSorting

3.事件sorting  

if(this.sortExpression != null && this.sortExpression.Contains(e.SortExpression))

{

  if{this.sortExpression.Contains("desc")}

  {

this.sortExpression=e.SortExpression;

  }

 else

  {

this.sortExpression=e.SortExpression+"desc";

  }

}

else

{

this.sortExpression=e.SortExpression;

}

 

BindData()方法里面:

if(this.sortExpression==null)

{

this.GridView1.DataSource=ds;

}

else

{

DataView dv=ds.table1.DefaultView;

dv.Sort=sortExpression;

this.GridView1.DataSource=dv;

}

 

          

 

视图状态:

string sortExpression

{

      get{

string result=this.ViewState["SortExperssion"] as string;

return result;

}

      set{

this.ViewState["SortExpression"]=value;

}

}

 

分页增加跳转:

GridView1_RowCreated事件中

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

{

TextBox TxtNewPage=new TextBox();

TxtNewPage.ID="TxtNewPage";

 

LinkButton goPage=new LinkButton();

goPage.Text="Go";

goPage.ID="BtnGo";

goPage.Click+=new EventHandler(goPage_Click);

 

Table pager=e.Row.Cells[0].Controls[0] as Table;

 

TableCell goCell=new TableCell();

goCell.Controls.Add(TxtNewPage);

goCell.Controls.Add(goPage);

 

pager.Row[0].Cells.Add(goCell);

 

 

void goPage_Click(object sender,EventArgs e)

{

LinkButton btnGo=sender as LinkButton;

TextBox txtNewPage=btnGo.Parent.FindControl("TxtNewPage") as TextBox;

int newPage=Convert.ToInt32(txtNewPage.Text);

this.GridView1.PageIndex=newpage-1;

this.BindPageData();

}

}

 

下拉列表框选页:

GridView1_RowCreated事件中

DropDownList ddlPage=new DropDownList();

ddlPage.ID="DdlPage";

ddlPage.AutoPostBack=true;

for(int i=0;i<this.GridView1.PageCount;i++)

{

ListItem li=new ListItem();

li.Text=String.Format("第{0}页",,i+1);

li.Value=i.ToString();

 

ddlPage.Items.Add(li);

}

ddlPage.SelectedIndex=this.Gridview1.PageIndex;

ddlPage.SelectedIndexChanged+=new EventHandler(ddlPage_SelectIndexChanged);

TableCell ddlCell=new TableCell();

ddlCell.Controls.Add(ddlPage);

 

page.Rows[0].Cells.Add(ddlCell);

 

void ddlPage_SelectedIndexChanged(object sender,EventArgs e)

{

DropDownList ddl=sender as DropDownList;

int index=Convert.ToInt32(ddl.SelectedValue);

this.GridView1.PageIndex=index;

this.BindPageData();

}

 

 

把删除列变成模板列,然后添加OnClientClick="return window.confirm('你真的要删除吗?');"属性。

 

动态添加js脚本:

string script="<script> function deleteconfirm() { return window.confirm(\"你真的要删除吗?\");} </script>";

if(!this.ClientScript.IsClientScriptBlockRegistered("deleteconfirm"))

{

this.ClientScript.RegisterClientScriptBlock(this.GetType(),"deleteconfirm",script);

}

 

 

GridView增加HyperLink类型的列 如:

DataNavigateUrlFields设为NewsID,DataNavigateUrlFormatString设为UpdateNews.aspx?id={0}

 

 

GridView中特定的行变色:

protected void GridView1_RowDataBound(object sender,GridViewRowEventArgs e)

{

    for (int i=0;i<GridView1.Rows.Count;i++)

    {

       string lbl=Convert.ToString(DataBinder.Eval(e.Row.DataItem,"state"));

        if(lbl=="60")

        {

        e.Row.BackColor=System.Drawing.Color.LimeGreen;

        }

    }

}

 

 

 

 

 

 

aspx页面使用<asp:GridView:

protected void GVviewMasterChildren_RowDataBound(object sender,

System.Web.UI.WebControls.GridViewRowEventArgs e)

{

            //e.Row.Cells[8].Visible = true;

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

            {

                HyperLink downloadHL = (HyperLink)e.Row.FindControl("HLdownload");

         string lang = DataBinder.Eval(e.Row.DataItem, "Language").ToString();//不用找隐藏控件,直接绑定已有的字段

 

 

 

 

GridView中遍历行:

protected void GridView1_DataBound(object sender, EventArgs e)

    {

        foreach (GridViewRow gr in GridView1.Rows)

        {//显示是否有回复

            CheckBox chk = (CheckBox)gr.Cells[3].FindControl("CheckBox1");

            string lid = ((Label)gr.Cells[3].FindControl("Label4")).Text.ToString();

            if (Convert.ToInt32(odb.scr("select count(*) from [read] where [lid]=" + lid + "")) > 0)

            {

                chk.Checked = true;

                chk.Text = "已回复";

            }

            else

            {

                chk.Checked = false;

                chk.Text = "无回复";

            }

        }

    }

 

 

鼠标经过变色:

protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)

    {//鼠标行为

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

        e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c;");

    }

posted on 2012-08-29 16:18  cw_volcano  阅读(195)  评论(0)    收藏  举报