【发布时间】:2012-02-07 06:12:13
【问题描述】:
我有一个非常标准的 GridView,带有一个带有 SelectCommand/UpdateCommand/DeleteCommand 的 SqlDataSource。
我正在使用 Gridview 的 TemplateField'ed 的内置命令。
CommandName="Update"
CommandName="Edit"
CommandName="Cancel"
CommandName="Delete"
在我将 GridView 放入 UpdatePanel 之前,一切正常。 1. 当我点击“编辑”时,它会进入编辑模式,但当您点击取消或更新时,不会退出编辑模式。 2. 当我点击“删除”时,即使点击“编辑”,我也无法进入编辑模式
这是怎么回事?
示例代码:
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<div class="BoxFloat">
<h4>Aliases</h4>
<hr />
<asp:SqlDataSource ID="sqlPersonAlias" runat="server"
ConnectionString="<%$ ConnectionStrings:myConnectionString %>"
ProviderName="<%$ ConnectionStrings:myConnectionString.ProviderName %>"
DeleteCommand="DELETE FROM PersonAlias WHERE PersonAliasId=?PersonAliasId"
UpdateCommand="UPDATE PersonAlias SET AliasName=?AliasName WHERE PersonAliasId=?PersonAliasId"
SelectCommand="SELECT * FROM PersonAlias E INNER JOIN Person P ON P.PersonId=E.PersonId WHERE Username=?Username">
<DeleteParameters>
<asp:Parameter Name="PersonAliasId" />
</DeleteParameters>
<UpdateParameters>
<asp:Parameter Name="PersonAliasId" />
<asp:Parameter Name="AliasName" />
</UpdateParameters>
<SelectParameters>
<asp:QueryStringParameter Name="Username" QueryStringField="Username" Type="String" />
</SelectParameters>
</asp:SqlDataSource>
<asp:GridView ID="gridviewPersonAlias" runat="server"
AutoGenerateColumns="False"
DataKeyNames="PersonAliasId"
DataSourceID="sqlPersonAlias">
<Columns>
<asp:TemplateField ShowHeader="False">
<EditItemTemplate>
<asp:ImageButton ID="imagebuttonCancel" ToolTip="Cancel" CommandName="Cancel" ImageUrl="~/images/cancel16.png" runat="server" />
<asp:ImageButton ID="imagebuttonUpdate" ToolTip="Apply" OnClientClick="return confirm('Are you sure?');" CommandName="Update" ImageUrl="~/images/apply16.png" runat="server" />
</EditItemTemplate>
<ItemTemplate>
<asp:ImageButton ID="imagebuttonDelete" ToolTip="Delete" OnClientClick="return confirm('Are you sure?');" CommandName="Delete" ImageUrl="~/images/delete16.png" runat="server" />
<asp:ImageButton ID="imagebuttonEdit" ToolTip="Edit" CommandName="Edit" ImageUrl="~/images/edit16.png" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="AliasName" HeaderText="Alias" SortExpression="AliasName" />
</Columns>
</asp:GridView>
<hr />
<asp:TextBox ID="textboxPersonAlias" runat="server" />
<asp:Button ID="buttonPersonAliasAdd" runat="server" Text="Add" onclick="buttonPersonAliasAdd_Click" />
</div>
</ContentTemplate>
</asp:UpdatePanel>
单击“更新”时,数据库会更新。看来问题出在客户端。
注意:我使用的是“?”而不是“@”作为参数,因为 SqlDataSource 使用的是 MySq 而不是 MSSQL(我喜欢 VS2010)。
【问题讨论】:
-
当您单击“取消”和“更新”按钮时,您会看到什么行为?页面是否回传?什么都没有发生吗?尝试使用 FireBug 或 Chrome 的开发人员工具来查看正在发布到服务器的值。你用的是哪个浏览器?
-
什么也没发生(至少在客户端)。服务器端方法似乎有效(数据库更新)。我使用 Chrome 和 Firefox 进行了测试。
-
也许您在更新面板之外还有其他一些控件受到影响,或者您禁用了视图状态?
标签: asp.net gridview updatepanel