DataView 使用技巧

1.GridView中使用超链接的技巧
  GridView中的超级链接,可以设置一个模版列,放入超级链接的控件,设置绑定参数即可。
数据绑定方式有两种,如下示例:
◆Eval方式     <%# Eval("id") %>
◆Bind方式    <%# Bind("id","~/info.aspx?id={0}") %>
推荐使用第一种方式,可以在一个<%# %>里放入多个绑定,而第二种只能如此绑定一个值
<%# Eval("id") + ":" + Eval("name")%>

做超级链接的控件,我们也有多种选择:
◆asp:LinkButton
示例
<asp:LinkButton ID="LinkButton2" OnClientClick=<%# "window.open('info.aspx?id=" + Eval("id") + "&name=" + Eval("name") + "')" %> runat="server"><%# "LinkButton方式绑定:" + Eval("id") %></asp:LinkButton>
◆asp:HyperLink
示例
<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%# "~/info.aspx?id=" + Eval("id") + "&name=" + Eval("name") %>' Text='<%#"HyperLink控件:" + Eval("id") %>'></asp:HyperLink>
◆a标签
示例
<a href='<%# "info.aspx?id=" + Eval("id") + "&name=" + Eval("name") %>'>a标签:<%# Eval("id") + ":" + Eval("name")%></a>
<a href='Javascript:alert("<%# "~/info.aspx?id=" + Eval("id") + "&name=" + Eval("name") %>")'>链接js事件:<%# Eval("id") %></a>

LinkButton 不好设置,推荐使用HyperLink或者a的方式,简单实用。特别是a标签,使用Javascript的方法也比较方便。
 补充:
<asp:LinkButton ID="LinkButton3" runat="server" PostBackUrl='<%# "~/Default.aspx?id=" + Eval("FUserID") %>'
CausesValidation="False" Text="跳转编辑"></asp:LinkButton>

2.gridview模板列加htmlinputcheckbox的取值
①aspx文件:
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AutoGenerateColumns="False"
            DataKeyNames="OrderID,ProductID" DataSourceID="SqlDataSource1" EmptyDataText="111">
            <Columns>
                <asp:BoundField DataField="OrderID" HeaderText="OrderID" ReadOnly="True" SortExpression="OrderID" />
                <asp:BoundField DataField="ProductID" HeaderText="ProductID" ReadOnly="True" SortExpression="ProductID" />
                <asp:BoundField DataField="UnitPrice" HeaderText="UnitPrice" SortExpression="UnitPrice" DataFormatString="{0:C}" />
                <asp:BoundField DataField="Quantity" HeaderText="Quantity" SortExpression="Quantity" />
                <asp:BoundField DataField="Discount" HeaderText="Discount" SortExpression="Discount" />
                <asp:ImageField DataImageUrlField="OrderID" DataImageUrlFormatString="/{0}.gif" HeaderText="pic">
                </asp:ImageField>
                <asp:TemplateField FooterText="id">
                    <ItemTemplate>
                        &nbsp;<input id="Checkbox1" runat="server" type="checkbox" value='<%# Eval("OrderID") %>' />                      
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
        <%=gridview1.PageIndex +1%>of<%=GridView1.PageCount%>
②cs文件:
string str1;
HtmlInputCheckBox cb;
str1="";
for(int i=0;i<GridView1.Rows.Count-1;i++)
{
  cb=GridView1.Rows(i).Cells(6).FindControl("Checkbox1");
  if(cb.Checked)
  {
    if(str1=="")
    {
      str1=cb.Value;
    }
    else
    {
      str1=str1+","+cb.Value;
    }
  }
}
Label1.Text=str1;

3.gridview模板列绑定gridview
①aspx文件:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="ProductID"
            DataSourceID="SqlDataSource1">
            <Columns>
                <asp:BoundField DataField="ProductID" HeaderText="ProductID" InsertVisible="False"
                    ReadOnly="True" SortExpression="ProductID" />
                <asp:BoundField DataField="ProductName" HeaderText="ProductName" SortExpression="ProductName" />
                <asp:TemplateField HeaderText="detail">
                    <ItemTemplate>
                        &nbsp;&nbsp;&nbsp;
                        <asp:GridView ID="GridView2" runat="server">
                        </asp:GridView>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
②cs文件:
protected void GridView1_RowDataBound(object sender,System.Web.UI.WebControls.GridViewRowEventArgs e)
{
  if(e.Row.RowType==DataControlRowType.DataRow)
  {
    SqlDataSource2.SelectParameters.Clear();
    SqlDataSource2.SelectParameters.Add("id",e.Row.Cells(0).Text);
    GridView gv;
    gv=e.Row.Cells(2).FindControl("GridView2");
    gv.DataSource=this.SqlDataSource2;
    gv.DataBind();
  }
}

4.GridView更新和删除显示提示信息
①aspx文件:
<asp:GridView ID="GridView1" runat="server"
            DataSourceID="SqlDataSource1" AutoGenerateColumns="False" DataKeyNames="au_id" OnRowCommand="GridView1_RowCommand" OnRowUpdating="GridView1_RowUpdating" OnRowDeleting="GridView1_RowDeleting">
            <Columns>
                <asp:TemplateField>
                    <ItemTemplate>
                        <asp:LinkButton ID="Edit" runat="server" CommandName="Edit">编辑</asp:LinkButton>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField Visible="False">
                    <ItemTemplate>
                        <asp:LinkButton ID="Update" runat="server" CommandName="Update" OnClientClick="return confirm('确定要更新?');">更新</asp:LinkButton>&nbsp;&nbsp;
                        <asp:LinkButton ID="Delete" runat="server" CommandName="Delete" OnClientClick="return confirm('确定要删除');">删除</asp:LinkButton>&nbsp;&nbsp;
                        <asp:LinkButton ID="Cancel" runat="server" CommandName="Cancel">取消</asp:LinkButton>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:templatefield headertext="Last Name">
                    <itemtemplate>
                        <%#Eval("au_lname") %>
                    </itemtemplate>
                    <edititemtemplate>
                        <asp:textbox id="LastNameTextBox" text='<%#Eval("au_lname") %>'
                            width="175" runat="server"/>
                        <br/>
                        <asp:requiredfieldvalidator id="LastNameRequiredValidator" controltovalidate="LastNameTextBox"
                            errormessage="Please enter a last name." validationgroup="NameGroup" runat="server"/>
                    </edititemtemplate>
                </asp:templatefield>
               
                <asp:templatefield headertext="First Name">
                    <itemtemplate>
                        <%#Eval("au_fname") %>
                    </itemtemplate>
                    <edititemtemplate>
                          <asp:textbox id="FirstNameTextBox" text='<%#Eval("au_fname") %>'
                            width="175" runat="server"/>
                          <br/>
                          <asp:requiredfieldvalidator id="FirstNameRequiredValidator" controltovalidate="FirstNameTextBox"
                                errormessage="Please enter a first name." validationgroup="NameGroup" runat="server"/>
                     </edititemtemplate>
                </asp:templatefield>
            </Columns>
        </asp:GridView>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:Pubs2000 %>"
            ProviderName="<%$ ConnectionStrings:Pubs2000.ProviderName %>" SelectCommand="SELECT au_id, au_lname, au_fname FROM authors"
            UpdateCommand="UPDATE authors SET au_lname = @au_lname, au_fname = @au_fname WHERE (au_id = @au_id)"
            DeleteCommand="DELETE FROM authors WHERE (au_id = @au_id)">
        </asp:SqlDataSource>
②cs文件:
void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        switch (e.CommandName.ToLower())
        {
            case "edit":
                GridView1.Columns[0].Visible = false;
                GridView1.Columns[1].Visible = true;
                break;
            case "update":
            case "delete":
            case "cancel":
                GridView1.Columns[0].Visible = true;
                GridView1.Columns[1].Visible = false;
                break;
            default:
                // Do nothing.
                break;
        }
    }

    void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        int index = GridView1.EditIndex;
        GridViewRow row = GridView1.Rows[index];

        TextBox firstNameTextBox = (TextBox)row.Cells[1].FindControl("FirstNameTextBox");

        String firstName = "";
        if (firstNameTextBox != null)
        {
            firstName = firstNameTextBox.Text;
        }

        TextBox lastNameTextBox = (TextBox)row.Cells[2].FindControl("LastNameTextBox");

        String lastName = "";
        if (lastNameTextBox != null)
        {
            lastName = lastNameTextBox.Text;
        }

        Parameter lastNameParameter = new Parameter("au_lname", TypeCode.String, lastName);
        Parameter firstNameParameter = new Parameter("au_fname", TypeCode.String, firstName);

        SqlDataSource1.UpdateParameters.Clear();
        SqlDataSource1.UpdateParameters.Add(lastNameParameter);
        SqlDataSource1.UpdateParameters.Add(firstNameParameter);
    }

    void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        SqlDataSource1.DeleteParameters.Clear();
    }

你可能感兴趣的:(view)