Gridview,Formview的事件驱动

来源:互联网 发布:spotlight 翻译 软件 编辑:程序博客网 时间:2024/05/16 06:40

最近发现一件怪事:执行formview的insert的时候,girdview自动刷新了一次。奇怪啊。。。我没有写这个重新绑定的方法啊

  <asp:ObjectDataSource ID="ObjectDataSource1" runat="server"  DeleteMethod="Delete" InsertMethod="Add" SelectMethod="GetMaxModel" TypeName="BLL.test" UpdateMethod="Update" OldValuesParameterFormatString="{0}" ConflictDetection="OverwriteChanges"  DataObjectTypeName="Model.test">
         </asp:ObjectDataSource>

             <asp:FormView ID="FormView1" runat="server" DataSourceID="ObjectDataSource1" OnDataBound="FormView1_DataBound" DefaultMode="Insert">
            <EditItemTemplate>
                sex:
                <asp:TextBox ID="sexTextBox" runat="server" Text='<%# Bind("sex") %>'>
                </asp:TextBox><br />
                name:
                <asp:TextBox ID="nameTextBox" runat="server" Text='<%# Bind("name") %>'>
                </asp:TextBox><br />
                id:
                <asp:TextBox ID="idTextBox" runat="server" Text='<%# Bind("id") %>'>
                </asp:TextBox><br />
                birthday:
                <asp:TextBox ID="birthdayTextBox" runat="server" Text='<%# Bind("birthday") %>'>
                </asp:TextBox><br />
                <asp:LinkButton ID="UpdateButton" runat="server" CausesValidation="True" CommandName="Update"
                    Text="Update">
                </asp:LinkButton>
                <asp:LinkButton ID="UpdateCancelButton" runat="server" CausesValidation="False" CommandName="Cancel"
                    Text="Cancel">
                </asp:LinkButton>
            </EditItemTemplate>
            <InsertItemTemplate>
                sex:
                <asp:TextBox ID="sexTextBox" runat="server" Text='<%# Bind("sex") %>'>
                </asp:TextBox><br />
                name:
                <asp:TextBox ID="nameTextBox" runat="server" Text='<%# Bind("name") %>'>
                </asp:TextBox><br />
                birthday:
                <asp:TextBox ID="birthdayTextBox" runat="server" Text='<%# Bind("birthday") %>'>
                </asp:TextBox><br />
                <asp:LinkButton ID="InsertButton" runat="server" CausesValidation="True" CommandName="Insert"
                    Text="Insert">
                </asp:LinkButton>
             </InsertItemTemplate>
            <ItemTemplate>
                sex:
                <asp:Label ID="sexLabel" runat="server" Text='<%# Bind("sex") %>'></asp:Label><br />
                name:
                <asp:Label ID="nameLabel" runat="server" Text='<%# Bind("name") %>'></asp:Label><br />
                id:
                <asp:Label ID="idLabel" runat="server" Text='<%# Bind("id") %>'></asp:Label><br />
                birthday:
                <asp:Label ID="birthdayLabel" runat="server" Text='<%# Bind("birthday") %>'></asp:Label><br />
                <asp:LinkButton ID="EditButton" runat="server" CausesValidation="False" CommandName="Edit"
                    Text="Edit">
                </asp:LinkButton>
                <asp:LinkButton ID="DeleteButton" runat="server" CausesValidation="False" CommandName="Delete"
                    Text="Delete">
                </asp:LinkButton>
                <asp:LinkButton ID="NewButton" runat="server" CausesValidation="False" CommandName="New"
                    Text="New">
                </asp:LinkButton>
            </ItemTemplate>
        </asp:FormView>
                  
         
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="ObjectDataSource1" AllowPaging="True" DataKeyNames="id" AllowSorting="True" ShowFooter="True" OnDataBound="GridView1_DataBound" OnRowDataBound="GridView1_RowDataBound">
            <Columns>
                <asp:TemplateField ShowHeader="False">
                    <EditItemTemplate>
                        <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="True" CommandName="Update"
                            Text="Update"></asp:LinkButton>
                        <asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" CommandName="Cancel"
                            Text="Cancel"></asp:LinkButton>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" CommandName="Edit"
                            Text="Edit"></asp:LinkButton>
                        <asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" CommandName="Delete"
                            Text="Delete"></asp:LinkButton>
                    </ItemTemplate>
                    <FooterTemplate>
                        <asp:LinkButton ID="LinkButton3" runat="server" OnClick="LinkButton3_Click">新增</asp:LinkButton>
                    </FooterTemplate>
                </asp:TemplateField>
                <asp:BoundField DataField="sex" HeaderText="sex" SortExpression="sex" />
                <asp:BoundField DataField="name" HeaderText="name" SortExpression="name" />
                <asp:BoundField DataField="id" HeaderText="id" SortExpression="id" />
                <asp:BoundField DataField="birthday" DataFormatString="{0:d}" HeaderText="birthday"
                    SortExpression="birthday" />
            </Columns>
        </asp:GridView>

 用F10仔细分析了一下事件的执行顺序。(问题:为什么以前的用gv的时候。新增之后都要bind的一次,是不是多余的?后来发现是凡是设置了可视化DataSource组件的,都会是在事件之后自动又绑定的,估计是触发了OBjectDataSource中类似dataChanged之类的事件,然后事件调用了所有订阅它的各控件的databind方法,而以前是DataSet做数据源,只能手工做了

照这样子。我才能解释我的gridview为什么会自动刷新。我新增一行以后。设置了可视化dataSource的girdview会执行一次绑定。所以他的数据是最新的。

其它的事件执行先后:RowCommand和RowDeleting执行先后不明了。(有空研究,好像没有什么大用),前台绑定—>RowDataBound--->DataBound事件  ,绑定的时候是先一行行再一列列,RowDataBound和RowCommand的执行先后也不明了(明天研究)

 还有一点要申明:在gv和fv中使用commandName="update"的button 的时候,会自动触发Row_Updating事件。使用 <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />就会等效于commandName="edit" 和commandName="delete"的两个按钮

原创粉丝点击