Gridview][UpdateCommand的写法要点]
生活随笔
收集整理的這篇文章主要介紹了
Gridview][UpdateCommand的写法要点]
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
在ASP.NET2.0中的GridView為我們?yōu)g覽更新數(shù)據(jù)提供了一個(gè)方便的途徑。我們只需要添加一個(gè)sqldatasouce控件和一個(gè)GridView,再為sqldatasource寫上正確的UpdateCommand語(yǔ)句就可以達(dá)到自動(dòng)更新數(shù)據(jù)的目的。基本上無(wú)需手寫更新代碼:但在寫UpdateCommand語(yǔ)句時(shí),需注意,updateCommand中各Sql更新參數(shù)的順序必須與頁(yè)面輸出數(shù)據(jù)的順序一致.
舉個(gè)例子:
若頁(yè)面元素順序如下:
??????????? <asp:BoundField DataField="id" HeaderText="紀(jì)錄號(hào)" 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語(yǔ)句時(shí)應(yīng)該寫成以下順序:
Update [***] Set CompanyName=@CompanyName,CompanyAddress=@CompanyAddress where id=@id
而不能寫成:Update [***] Set CompanyAddress=@CompanyAddress,CompanyName=@CompanyName where id=@id寫法2會(huì)造成將CompanyAddress的數(shù)據(jù)寫入CompanyName字段,而將CompanyName的數(shù)據(jù)寫入CompanyAddress的混亂.
?
總結(jié)
以上是生活随笔為你收集整理的Gridview][UpdateCommand的写法要点]的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: GridView控件修改、删除示例(修改
- 下一篇: 在GridView内访问特定控件