将DetailsView 设置为GridView 的选定行
我正在创建一个 GridView/DetailsView 页面。我有一个显示一堆行的网格,当选择一行时,它使用 DetailsView 来允许插入/更新。
我的问题是链接这些的最佳方式是什么?我不想再次联系网络服务,我需要的所有数据都在选定的网格视图行中。我基本上有 2 个共享相同“DataObjectTypeName”的独立数据源,第一个数据源检索数据,另一个数据源执行 CRUD。
将选定的网格视图行传输到详细信息视图的最佳方法是什么?我是否必须手动处理插入/更新事件并自己调用数据源?
有没有办法链接这两个以便它们使用相同的数据源?
<asp:GridView ID="gvDetails" runat="server" DataKeyNames="ID, Code"
DataSourceID="odsSearchData" >
<Columns>
<asp:BoundField DataField="RowA" HeaderText="A" SortExpression="RowA" />
<asp:BoundField DataField="RowB" HeaderText="B" SortExpression="RowB" />
<asp:BoundField DataField="RowC" HeaderText="C" SortExpression="RowC" />
....Code...
<asp:DetailsView ID="dvDetails" runat="server" DataKeyNames="ID, Code"
DataSourceID="odsCRUD" GridLines="None" DefaultMode="Edit" AutoGenerateRows="false"
Visible="false" Width="100%">
<Fields>
<asp:BoundField DataField="RowA" HeaderText="A" SortExpression="RowA" />
<asp:BoundField DataField="RowB" HeaderText="B" SortExpression="RowB" />
<asp:BoundField DataField="RowC" HeaderText="C" SortExpression="RowC" />
...
I am creating a GridView/DetailsView page. I have a grid that displays a bunch of rows, when a row is selected it uses a DetailsView to allow for Insert/Update.
My question is what is the best way to link these? I do not want to reach out to the web service again, all the data i need is in the selected grid view row. I basically have 2 separate data sources that share the same "DataObjectTypeName", the first data source retrieves the data, and the other to do the CRUD.
What is the best way to transfer the Selected Grid View row to the Details View? Am I going to have to manualy handle the Insert/Update events and call the data source myself?
Is there no way to link these two so they use the same data source ?
<asp:GridView ID="gvDetails" runat="server" DataKeyNames="ID, Code"
DataSourceID="odsSearchData" >
<Columns>
<asp:BoundField DataField="RowA" HeaderText="A" SortExpression="RowA" />
<asp:BoundField DataField="RowB" HeaderText="B" SortExpression="RowB" />
<asp:BoundField DataField="RowC" HeaderText="C" SortExpression="RowC" />
....Code...
<asp:DetailsView ID="dvDetails" runat="server" DataKeyNames="ID, Code"
DataSourceID="odsCRUD" GridLines="None" DefaultMode="Edit" AutoGenerateRows="false"
Visible="false" Width="100%">
<Fields>
<asp:BoundField DataField="RowA" HeaderText="A" SortExpression="RowA" />
<asp:BoundField DataField="RowB" HeaderText="B" SortExpression="RowB" />
<asp:BoundField DataField="RowC" HeaderText="C" SortExpression="RowC" />
...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
标准方法是让 griview 的选定项目成为已连接到详细信息视图的对象数据源的控制参数。我可能不会太担心检索已有数据的开销,除非您要迎合连接速度较慢的用户,并且希望不惜一切代价避免往返网络服务器。
如果你真的想避免这种情况,那么你可以使用 javascript/jquery 从 gridview 中提取数据,然后通过 ajax 调用进行插入/更新。但这需要更多的编码。
The standard way to do it would be to have the selected item of the griview be a control parameter to the objectdatasource you have wired up to the detailsview. I would probably not worry too much about the overhead of retreiving data that you already have unless you are catering to users with such slow connections that you want to avoid roundtrips to the webserver at all costs.
If you really want to avoid that thenyou could pull the data out of the gridview using javascript/jquery and then do your inserts/updates via ajax calls. It would require lots more coding though.
这是一个非常古老的线程,但如果有人像我一样来到这里寻找答案,一个简单的解决方案是将此函数添加到您的代码中:(
请注意,只有当您的 GridView 中的行与您的 GridView 中的条目匹配时,这才有效DetailsView。)
并修改 GridView 和 DetailsView 以包含以下设置:
这将使 DetailsView 中选定的页面与 GridView 中选定的索引相匹配。
如果您不希望用户在“详细信息视图”中使用分页进行导航,则可以隐藏“详细信息视图”属性中的分页选项。
This is a really old thread, but in case anyone came here looking for an answer like I did, a simple solution is to add this function to your code:
(Note that this only works if the rows in your GridView match the entries in your DetailsView.)
And modify the GridView and DetailsView to include these settings:
This will make the selected page in the DetailsView match the selected index in the GridView.
You can hide the paging options in the DetailsView properties if you dont want users to navigate using paging in the DetailsView.