Gridview][UpdateCommand的写法要点]

在ASP.NET2.0中的GridView为我们浏览更新数据提供了一个方便的途径。我们只需要添加一个sqldatasouce控件和一个GridView,再为sqldatasource写上正确的UpdateCommand语句就可以达到自动更新数据的目的。基本上无需手写更新代码:但在写UpdateCommand语句时,需注意,updateCommand中各Sql更新参数的顺序必须与页面输出数据的顺序一致.

举个例子:

若页面元素顺序如下:

            <asp:BoundField DataField="id" HeaderText="纪录号" ReadOnly="True" Visible="false" />
            <asp:TemplateField HeaderText="公司名称">
                <ItemTemplate>
                    <asp:label ID="DisCompanyName" runat="server" Width="100" Text='<%# Eval("CompanyName") %>'></asp:label>
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:TextBox ID="txtCompanyName" Width="100" Text='<%# Bind("CompanyName") %>' runat="server"></asp:TextBox>
                </EditItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="公司地址">
                <ItemTemplate>
                    <asp:label ID="DisCompanyAddress" Text='<%# Eval("CompanyAddress") %>' runat="server" Width="100"></asp:label>
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:TextBox ID="txtCompanyAddress" Text='<%# Bind("CompanyAddress") %>' runat="server" Width="100"></asp:TextBox>
                </EditItemTemplate>
            </asp:TemplateField>
则在写SqlDataSource的updateCommand语句时应该写成以下顺序:
Update [***] Set CompanyName=@CompanyName,CompanyAddress=@CompanyAddress where id=@id
而不能写成:
Update [***] Set CompanyAddress=@CompanyAddress,CompanyName=@CompanyName where id=@id
写法2会造成将CompanyAddress的数据写入CompanyName字段,而将CompanyName的数据写入CompanyAddress的混乱.

 
posted @ 2007-03-20 00:28  leonardleonard  阅读(221)  评论(0编辑  收藏  举报