笔记0509

1. SQL :select isnull(sum(qty),0) as qty from table where record = 150

isnull

ISNULL ( check_expression , replacement_value )

备注

如果 check_expression 不为 NULL,则返回它的值;否则,在将 replacement_value 隐式转换为 check_expression 的类型(如果这两个类型不同)后,则返回前者。

参数

check_expression

将被检查是否为 NULL 的表达式。check_expression 可以为任何类型。

replacement_value

当 check_expression 为 NULL 时要返回的表达式。replacement_value 必须是可以隐式转换为 check_expresssion 类型的类型。

2.

<Columns>
          <asp:BoundField  DataField="" HeaderText="" ReadOnly="true" />
          </Columns>
DataField 与 HeaderText 区别

3.

ASP.NET Eval四种绑定方式:

1、1.x中的数据绑定语法

<asp:Literal id="litEval2" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "userName")%>' />

2、 2.x简化Eval数据绑定语法

<asp:Literal id="litEval1" runat="server" Text='<%Eval("userName")%>' />

3、第二种方法的方法重载

<a href='<%# Eval("userId","Default.aspx?id={0}")%>'><%# Eval("userName") %></a>

4、eval同时绑定两个值

<a href='<%# string.Format("Default.aspx?id={0}&role={1}", Eval("userId"),Eval("userRole"))%>'><%# Eval("userName") %></a>

eval_r()方法在运行时使用反射执行后期绑定计算,因此与标准的ASP.NET数据绑定方法bind相比,会导致性能明显下降。它一般用在绑定时需要格式化字符串的情况下。多数情况尽量少用此方法


Eval 方法是静态(只读)方法,该方法采用数据字段的值作为参数并将其作为字符串返回。Bind 方法支持读/写功能,可以检索数据绑定控件的值并将任何更改提交回数据库。

使用 Eval 方法
Eval 方法可计算数据绑定控件(如 GridView、DetailsView 和 FormView 控件)的模板中的后期绑定数据表达式。在运行时,Eval 方法调用 DataBinder 对象的 Eval 方法,同时引用命名容器的当前数据项。命名容器通常是包含完整记录的数据绑定控件的最小组成部分,如 GridView 控件中的一行。因此,只能对数据绑定控件的模板内的绑定使用 Eval 方法。

Eval 方法以数据字段的名称作为参数,从数据源的当前记录返回一个包含该字段值的字符串。可以提供第二个参数来指定返回字符串的格式,该参数为可选参数。字符串格式参数使用为 String 类的 Format 方法定义的语法。

使用 Bind 方法
Bind 方法与 Eval 方法有一些相似之处,但也存在很大的差异。虽然可以像使用 Eval 方法一样使用 Bind 方法来检索数据绑定字段的值,但当数据可以被修改时,还是要使用 Bind 方法。

在 ASP.NET 中,数据绑定控件(如 GridView、DetailsView 和 FormView 控件)可自动使用数据源控件的更新、删除和插入操作。例如,如果已为数据源控件定义了 SQL Select、Insert、Delete 和 Update 语句,则通过使用 GridView、DetailsView 或 FormView 控件模板中的 Bind 方法,就可以使控件从模板中的子控件中提取值,并将这些值传递给数据源控件。然后数据源控件将执行适当的数据库命令。出于这个原因,在数据绑定控件的 EditItemTemplate 或 InsertItemTemplate 中要使用 Bind 函数。

Bind 方法通常与输入控件一起使用,例如由编辑模式中的 GridView 行所呈现的 TextBox 控件。当数据绑定控件将这些输入控件作为自身呈现的一部分创建时,该方法便可提取输入值。

Bind 方法采用数据字段的名称作为参数,从而与绑定属性关联,如下面的示例所示:

<EditItemTemplate>
<table>
<tr>
<td align=right>
<b>Employee ID:</b>
</td>
<td>
<%# eval_r("EmployeeID") %>
</td>
</tr>
<tr>
<td align=right>
<b>First Name:</b>
</td>
<td>
<asp:TextBox ID="EditFirstNameTextBox" RunAt="Server"
Text='<%# Bind("FirstName") %>' />
</td>
</tr>
<tr>
<td align=right>
<b>Last Name:</b>
</td>
<td>
<asp:TextBox ID="EditLastNameTextBox" RunAt="Server"
Text='<%# Bind("LastName") %>' />

</td>
</tr>
<tr>
<td colspan="2">
<asp:LinkButton ID="UpdateButton" RunAt="server"
Text="Update" CommandName="Update" />

<asp:LinkButton ID="CancelUpdateButton" RunAt="server"
Text="Cancel" CommandName="Cancel" />
</td>
</tr>
</table>
</EditItemTemplate>

单击行的 Update 按钮时,使用 Bind 语法绑定的每个控件属性值都会被提取出来,并传递给数据源控件以执行更新操作。


使用 DataBinder.Eval
ASP.NET 提供了一个名为 DataBinder.Eval 的静态方法,该方法计算后期绑定的数据绑定表达式,并将结果格式化为字符串(可选)。利用此方法,可以避免许多在将值强制为所需数据类型时必须执行的显式强制转换操作。

例如,在下面的代码片段中,一个整数显示为货币字符串。使用标准的 ASP.NET 数据绑定语法,必须首先强制转换数据行的类型以便检索数据字段 IntegerValue。然后,这将作为参数传递到 String.Format 方法:

<%# String.Format("{0:c}", ((DataRowView)Container.DataItem)["IntegerValue"]) %>


将此语法与 DataBinder.Eval 的语法进行比较,后者只有三个参数:数据项的命名容器、数据字段名称和格式字符串。在模板化列表中(如 DataList 类、DataGrid 类或 Repeater 类),命名容器始终是 Container.DataItem。

<%# DataBinder.eval_r(Container.DataItem, "IntegerValue", "{0:c}") %>


格式字符串参数是可选的。如果它被忽略,DataBinder.Eval 将返回类型对象的值,如下面的示例所示:
<%# (bool)DataBinder.eval_r(Container.DataItem, "BoolValue") %>
当对模板化列表中的控件进行数据绑定时,DataBinder.Eval 特别有用,因为数据行和数据字段通常都必须强制转换。

</td>
</tr>
<tr>
<td colspan="2">
<asp:LinkButton ID="UpdateButton" RunAt="server"
Text="Update" CommandName="Update" />

<asp:LinkButton ID="CancelUpdateButton" RunAt="server"
Text="Cancel" CommandName="Cancel" />
</td>
</tr>
</table>
</EditItemTemplate>

单击行的 Update 按钮时,使用 Bind 语法绑定的每个控件属性值都会被提取出来,并传递给数据源控件以执行更新操作。


使用 DataBinder.Eval
ASP.NET 提供了一个名为 DataBinder.Eval 的静态方法,该方法计算后期绑定的数据绑定表达式,并将结果格式化为字符串(可选)。利用此方法,可以避免许多在将值强制为所需数据类型时必须执行的显式强制转换操作。

例如,在下面的代码片段中,一个整数显示为货币字符串。使用标准的 ASP.NET 数据绑定语法,必须首先强制转换数据行的类型以便检索数据字段 IntegerValue。然后,这将作为参数传递到 String.Format 方法:

<%# String.Format("{0:c}", ((DataRowView)Container.DataItem)["IntegerValue"]) %>


将此语法与 DataBinder.Eval 的语法进行比较,后者只有三个参数:数据项的命名容器、数据字段名称和格式字符串。在模板化列表中(如 DataList 类、DataGrid 类或 Repeater 类),命名容器始终是 Container.DataItem。

<%# DataBinder.eval_r(Container.DataItem, "IntegerValue", "{0:c}") %>


格式字符串参数是可选的。如果它被忽略,DataBinder.Eval 将返回类型对象的值,如下面的示例所示:
<%# (bool)DataBinder.eval_r(Container.DataItem, "BoolValue") %>
当对模板化列表中的控件进行数据绑定时,DataBinder.Eval 特别有用,因为数据行和数据字段通常都必须强制转换。

4.

在GridView中使用FindControl

最近项目中用到FindControl和GridView,整理了一下几种使用方法,留作资料。  
  
1、在选择(SelectedIndexChanged)事件中使用  
  
  
     //获得被选择行的TextBox1  
    protected void gv1_SelectedIndexChanged(object sender, EventArgs e)  
    {  
        //Control c = this.gv1.Rows[this.gv1.SelectedIndex].FindControl("TextBox1");  
        //TextBox tb = (TextBox)c;  
        //tb.Text = "TextBox";  
  
        TextBox tb = (TextBox)this.gv1.Rows[this.gv1.SelectedIndex].FindControl("TextBox1");  
        tb.Text = "hello";  
    }  
  
2、在编辑行(RowEditing)事件中使用  
  
     
    //编辑行时,找到TextBox1  
    protected void gv1_RowEditing(object sender, GridViewEditEventArgs e)  
    {  
        //设置要编辑行的索引  
        gv1.EditIndex = e.NewEditIndex;  
        GridViewBind();  
  
        TextBox tb = (TextBox)this.gv1.Rows[e.NewEditIndex].FindControl("TextBox1");  
        Response.Write(tb.Text);  
    }  
  
  
3、在取消编辑行(RowCancelingEdit)事件中使用  
  
    //取消编辑时,找到TextBox1  
    protected void gv1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)  
    {  
        TextBox tb = (TextBox)this.gv1.Rows[e.RowIndex].FindControl("TextBox1");  
        Response.Write(tb.Text);  
  
        gv1.EditIndex = -1;  
        GridViewBind();  
    }  
  
4、在行绑定(RowDataBound)事件中使用  
  
      
  
    //获得行数据绑定中的TextBox1  
    protected void gv1_RowDataBound(object sender, GridViewRowEventArgs e)  
    {  
        // 对于在RowDataBound中Find,可以用if (e.Row.RowType == DataControlRowType.DataRow)来限制Find的范围,因为Find默认是在HeaderTemplate中找,如果不限定范围,在HeaderTemplate中找不到,自然就返回null,然后就出错了,DataControlRowType枚举中的DataRow确定是数据行.  
        //if (e.Row.RowType == DataControlRowType.DataRow)  
        //{  
        //    TextBox tb = (TextBox)e.Row.FindControl("TextBox1");  
        //    tb.Text = "databind";  
        //}  
  
  
        //如果在DataGrid的页眉和页脚:  
  
        //if (e.Row.RowType == DataControlRowType.Header)  
        //{  
        //    TextBox tbheader = (TextBox)e.Row.FindControl("txtHeader");  
        //    tbheader.Text = "Head";  
        //}  
        ((TextBox)this.gv1.Controls[0].Controls[0].FindControl("txtHeader")).Text = "Head";  
  
        if (e.Row.RowType == DataControlRowType.Footer)  
        {  
            TextBox tbfooter = (TextBox)e.Row.FindControl("txtFooter");  
            tbfooter.Text = "Footer";  
        }  
    }  
  
5、在行命令(RowCommand)事件中使用  
  
       
  
    //行命令时间中找到TextBox1  
    //如果使用GridView默认的模式,e.CommandArgument自动棒定为该行的Index,这时候只要指定gridview1.Rows[Convert.ToInt32(e.CommandArgument)].FindControl("xxx")就可以了,但是如果转化为Template,e.CommandArgument并不会自动绑定任何值,需要手动绑定,可以在<ItemTemplate></ItemTemplate>手动写CommandArgument="<%# ((GridViewRow) Container).RowIndex %>",把这个行的 Index绑定绑定到该e.CommandArgument就可以了.  
    protected void gv1_RowCommand(object sender, GridViewCommandEventArgs e)  
    {  
        if (e.CommandName.ToLower() == "change")  
        {  
            TextBox tb = (TextBox)this.gv1.Rows[Convert.ToInt32(e.CommandArgument)].FindControl("TextBox1");  
              
            Response.Write(tb.Text);  
        }  
    }  
  
其他事件中的使用,和上面列举的类似  
  
附上,前台代码:UseTest3.aspx  
  
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="UseTest3.aspx.cs" Inherits="UseTest3" %>  
  
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  
<html xmlns="http://www.w3.org/1999/xhtml" >  
<head runat="server">  
    <title>无标题页</title>  
</head>  
<body>  
    <form id="form1" runat="server">  
    <div>  
        <asp:GridView ID="gv1" runat="server" AutoGenerateColumns="False" OnSelectedIndexChanged="gv1_SelectedIndexChanged" OnRowEditing="gv1_RowEditing" OnRowDataBound="gv1_RowDataBound" ShowFooter="True" OnRowCommand="gv1_RowCommand" DataKeyNames="employeeid" OnRowCancelingEdit="gv1_RowCancelingEdit">  
        <Columns>  
        <asp:TemplateField>  
            <HeaderTemplate>  
                <asp:TextBox ID="txtHeader" runat="Server"></asp:TextBox>  
            </HeaderTemplate>  
            <ItemTemplate>  
                <asp:TextBox ID="TextBox1" Text='<%# Bind("employeeid")%>' runat="server"></asp:TextBox>               
            </ItemTemplate>  
            <FooterTemplate>  
                <asp:TextBox ID="txtFooter" runat="server"></asp:TextBox>  
            </FooterTemplate>  
        </asp:TemplateField>  
        <asp:BoundField DataField="lastname" HeaderText="LastName" />  
        <asp:BoundField DataField="firstname" HeaderText="FirstName" />  
        <asp:ButtonField CommandName="select" Text="选择" />       
        <asp:ButtonField CommandName="change" Text="change" />  
            <asp:CommandField ShowEditButton="True" />  
        </Columns>  
        </asp:GridView>  
         </div>  
    </form>  
</body>  
</html>  

 5.gridview

原文地址:https://www.cnblogs.com/jonson1126/p/3069418.html