Set DetailsView as selected row of GridView

依然范特西╮ 提交于 2020-01-14 05:39:28

问题


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" />

...

回答1:


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.




回答2:


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.)

protected void GridView1_OnSelectedIndexChanged(object sender, EventArgs e)
{
     DetailsView1.SetPageIndex(GridView1.SelectedIndex);
}

And modify the GridView and DetailsView to include these settings:

<asp:GridView ... OnSelectedIndexChanged="GridView1_OnSelectedIndexChanged" ... >
<asp:DetailsView ... AllowPaging="True" ... >

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.



来源:https://stackoverflow.com/questions/2804830/set-detailsview-as-selected-row-of-gridview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!