ASP.NET ObjectDataSource,更改 FormView 按钮的 CommandName 属性
我有一个与 FormView 一起使用的 ObjectDataSource,它工作正常,但我想更改一件小事情。在 FormView 上,触发更新的按钮将 CommandName 属性设置为“更新”,但我想将该属性更改为“更新”以外的其他属性 - 当我更改该属性时,更新将不再起作用。我想这样做的原因是我在同一页面上有多个 FormView,并且需要有多个更新按钮。下面是我的代码:
FormView:
<asp:FormView ID="fvGeneralInfo" runat="server"
DataSourceID="objInstructorDetails" CssClass="Gridview"
OnItemCommand="fvGeneralInfo_ItemCommand"
DefaultMode="Edit">
<EditItemTemplate>
<table>
....
<tr>
<td style="text-align:right;">
<asp:Label runat="server" ID="lblGeneralInfoMessage" Text="General Info updated successfully" Visible="false" />
</td>
<td>
<asp:Button runat="server" ID="btnUpdateGeneralInfo" ValidationGroup="UpdateGeneralInfo" Text="Update" CommandName="Update" />
<asp:Button runat="server" ID="btnCancelGeneralInfo" Text="Cancel" CommandName="CancelGeneralInfo" />
</td>
</tr>
</table>
</EditItemTemplate>
</asp:FormView>
ObjectDataSource:
<asp:ObjectDataSource ID="objInstructorDetails" runat="server" TypeName="AIMLibrary.BLL.Instructor" SelectMethod="GetInstructorDetails"
InsertMethod="InsertInstructor" UpdateMethod="UpdateInstructor" OnInserting="objInstructorDetails_OnInserting"
OnUpdating="objInstructorDetails_OnUpdating" >
<SelectParameters>
<asp:QueryStringParameter Name="InstructorId" QueryStringField="InstructorId" />
</SelectParameters>
<UpdateParameters>
<asp:Parameter Name="instructorId" Type="Int32" />
<asp:Parameter Name="firstName" Type="String" DefaultValue="" />
<asp:Parameter Name="middleName" Type="String" DefaultValue="" />
<asp:Parameter Name="lastName" Type="String" DefaultValue="" />
<asp:Parameter Name="phone" Type="String" DefaultValue="" />
<asp:Parameter Name="email" Type="String" DefaultValue="" />
<asp:Parameter Name="addressLine1" Type="String" DefaultValue="" />
<asp:Parameter Name="addressLine2" Type="String" DefaultValue="" />
<asp:Parameter Name="city" Type="String" DefaultValue="" />
<asp:Parameter Name="state" Type="String" DefaultValue="" />
<asp:Parameter Name="zip" Type="String" DefaultValue="" />
<asp:Parameter Name="abcBoardNumber" Type="String" DefaultValue="" />
</UpdateParameters>
</asp:ObjectDataSource>
I have an ObjectDataSource that I'm using with a FormView, and it works fine, but i want to change one small thing. On the FormView the button that fires the update has the CommandName attribute set to "Update," but I would like to change that attribute to something other than "Update" - when I do change that attribute the update no longer works. The reason I want to do this is I have multiple FormViews on the same page and need to have multiple update buttons. Below is my code:
FormView:
<asp:FormView ID="fvGeneralInfo" runat="server"
DataSourceID="objInstructorDetails" CssClass="Gridview"
OnItemCommand="fvGeneralInfo_ItemCommand"
DefaultMode="Edit">
<EditItemTemplate>
<table>
....
<tr>
<td style="text-align:right;">
<asp:Label runat="server" ID="lblGeneralInfoMessage" Text="General Info updated successfully" Visible="false" />
</td>
<td>
<asp:Button runat="server" ID="btnUpdateGeneralInfo" ValidationGroup="UpdateGeneralInfo" Text="Update" CommandName="Update" />
<asp:Button runat="server" ID="btnCancelGeneralInfo" Text="Cancel" CommandName="CancelGeneralInfo" />
</td>
</tr>
</table>
</EditItemTemplate>
</asp:FormView>
ObjectDataSource:
<asp:ObjectDataSource ID="objInstructorDetails" runat="server" TypeName="AIMLibrary.BLL.Instructor" SelectMethod="GetInstructorDetails"
InsertMethod="InsertInstructor" UpdateMethod="UpdateInstructor" OnInserting="objInstructorDetails_OnInserting"
OnUpdating="objInstructorDetails_OnUpdating" >
<SelectParameters>
<asp:QueryStringParameter Name="InstructorId" QueryStringField="InstructorId" />
</SelectParameters>
<UpdateParameters>
<asp:Parameter Name="instructorId" Type="Int32" />
<asp:Parameter Name="firstName" Type="String" DefaultValue="" />
<asp:Parameter Name="middleName" Type="String" DefaultValue="" />
<asp:Parameter Name="lastName" Type="String" DefaultValue="" />
<asp:Parameter Name="phone" Type="String" DefaultValue="" />
<asp:Parameter Name="email" Type="String" DefaultValue="" />
<asp:Parameter Name="addressLine1" Type="String" DefaultValue="" />
<asp:Parameter Name="addressLine2" Type="String" DefaultValue="" />
<asp:Parameter Name="city" Type="String" DefaultValue="" />
<asp:Parameter Name="state" Type="String" DefaultValue="" />
<asp:Parameter Name="zip" Type="String" DefaultValue="" />
<asp:Parameter Name="abcBoardNumber" Type="String" DefaultValue="" />
</UpdateParameters>
</asp:ObjectDataSource>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
每个 FormView 都有自己的事件来处理更新,因此不同 FormView 的 CommandName 相同应该不是问题。
如果出现问题,您可以通过更改按钮的文本值来更改按钮的名称。
Each FormView will have it's own event for handling updates so the CommandName being the same for different FormViews should not be a problem.
You can change the name of the buttons if that's an issue by changing it's text value.