net 2.0中的GridView与FormView控件

来源:互联网 发布:部落冲突7本满防数据 编辑:程序博客网 时间:2024/04/29 00:38
net 2.0中的GridView与FormView控件 2006-3-29 15:20:00  [asp.net 2.0]

 

在.net 1.1中,我们为DataGrid控件而喝彩,它的排序功能、分页功能、内置的编辑、修改、删除功能,曾让无数的asp.net程序员为之振奋。但不论是排序还是分页或是编辑功能都需要我们去后台页面编写相当多的代码,这点让很多刚入门的asp.net爱好者感到无所适从。
现在,在2.0中,不必再.cs页面中编写大段的代码了。在这里,我用GridView显示数据,用FormView控件显示选中的详细记录。
首先,打开vs2005,新建一个.aspx页面,拖入一个GridView控件,在源代码区写好数据源,或按向导来创建。



 <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:lemongtreeConnectionString %>"
            SelectCommand="SELECT * FROM [LINKS]"
            ></asp:SqlDataSource>
   这里新建了一个SqlDataSource,它得到的结果是在links表中查询出所有的记录,这个SqlDataSource我们将绑定在GridView中。
   我们来看看GridView的代码:
   <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" BackColor="White"
            BorderColor="#336666" BorderStyle="Double" BorderWidth="3px" CellPadding="4"
            DataSourceID="SqlDataSource1" GridLines="Horizontal" OnSelectedIndexChanged="GridView1_SelectedIndexChanged" DataKeyNames="id">
            <FooterStyle BackColor="White" ForeColor="#333333" />
            <Columns>
                <asp:CommandField ShowSelectButton="True" />
                <asp:BoundField DataField="id" HeaderText="id" InsertVisible="False" ReadOnly="True"
                    SortExpression="id" />
                <asp:HyperLinkField DataNavigateUrlFields="url" DataTextField="Title" Text="鍚嶇О" />
            </Columns>
            <RowStyle BackColor="White" ForeColor="#333333" Font-Size="9pt"/>
            <SelectedRowStyle BackColor="#339966" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="#336666" ForeColor="White" HorizontalAlign="Center" />
            <HeaderStyle BackColor="#336666" Font-Bold="True" ForeColor="White" />
        </asp:GridView>
  这里我将AutoGenerateColumns设为了False,不允许自动生成列,因为我只想在GrieView中显示ID和一列超级链接。
  注意这里的DataSourceID属性,要将GridView的"启用选定内容"设为True,这样就可以在GridView中选定一行了。DatakeyNames属性设为links表的主键。这里还有一个重要的OnSelectedIndexChanged事件,当我们选中一行时,将执行.aspx.cs中的GridView1_SelectedIndexChanged方法。这个方法到底有何用,呆会就会介绍到。
    完成了GridView的操作后,从工具条中拖一个FormView控件,这个控件用于显示详细记录,更新、删除、新增记录。
    这里,我重建了一个SqlDataSource,
   <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:lemongtreeConnectionString %>" SelectCommand="select * from [links] where
id=@id" UpdateCommand="update [links] set title=@title,url=@url,logo=@logo,isindex=@isindex,description=@description,islogo=@islogo where id=@id" DeleteCommand="delete from [links] where id=@id" InsertCommand="insert into [links](title,url,logo,isindex,description,islogo) values (@title,@url,@logo,@isindex,@description,@islogo)">
   然后,设定一个SelectParameters,这里的SelectParameters是与GridView的SelectedIndexChanged事件相关联的
   <SelectParameters>
    <asp:Parameter Name="id" Type="int16" Direction="input" />
    </SelectParameters>
 完整的就是
  <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:lemongtreeConnectionString %>" SelectCommand="select * from [links] where
id=@id" UpdateCommand="update [links] set title=@title,url=@url,logo=@logo,isindex=@isindex,description=@description,islogo=@islogo where id=@id" DeleteCommand="delete from [links] where id=@id" InsertCommand="insert into [links](title,url,logo,isindex,description,islogo) values (@title,@url,@logo,@isindex,@description,@islogo)">
    <SelectParameters>
    <asp:Parameter Name="id" Type="int16" Direction="input" />
    </SelectParameters>
    </asp:SqlDataSource>
 FormView的代码:
  <asp:FormView ID="FormView1" runat="server" BackColor="White" BorderColor="#336666"
            BorderStyle="Double" BorderWidth="3px" CellPadding="4" DataSourceID="SqlDataSource2"
            GridLines="Horizontal" DataKeyNames="id" OnItemUpdated="FormView1_ItemUpdated" style="font-size: 9pt; font-family: 'Courier New'" Width="395px" OnItemDeleted="FormView1_ItemDeleted" AllowPaging="True" OnItemInserted="FormView1_ItemInserted">
   ........
    <ItemTemplate>
               .....
                  <asp:LinkButton ID="EditButton"
                                        Text="Edit"
                                        CommandName="Edit"
                                        RunAt="server"/>
                        &nbsp;
                        <asp:LinkButton ID="NewButton"
                                        Text="New"
                                        CommandName="New"
                                        RunAt="server"/>
                     
            </ItemTemplate>
   注意ItemTemplate中的二个LinkButton,这里是用来触发编辑与新增命令的
   前台介绍完了,再来看看.cs文件
   protected void Page_Load(object sender, EventArgs e)
    {
      
    }
    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
        this.SqlDataSource2.SelectParameters["id"].DefaultValue = GridView1.SelectedValue.ToString();
        this.FormView1.DataBind();
    }
    protected void FormView1_ItemUpdated(object sender, FormViewUpdatedEventArgs e)
    {
        this.GridView1.DataBind();
        this.GridView1.SelectedIndex = -1;
    }
    protected void FormView1_ItemDeleted(object sender, FormViewDeletedEventArgs e)
    {
        this.GridView1.DataBind();
        this.GridView1.SelectedIndex = -1;
    }
    protected void FormView1_ItemInserted(object sender, FormViewInsertedEventArgs e)
    {
        this.GridView1.DataBind();
        this.GridView1.SelectedIndex = -1;
    }
 就这么一些代码,就可以完成增、删、改操作,是不是很简单?
 看看最终效果:
显示记录
 选择一条记录

编辑一条记录
 

新增一条记录
 
是不是变得异常简单?

 

原创粉丝点击