[ASP.NET]GridView中包入DropDownList後,進入編輯模式能夠與資料庫資料一致

摘要:[ASP.NET]GridView中包入DropDownList後,進入編輯模式能夠與資料庫資料一致

問題:

最近要在GridView中放入DropDownList

然後編輯時會發現顯示的資料與原本的資料不一致

後來找到解決的辦法,在DropDownList中加入"SelectedValue='<%# Bind("ID") %>'"即可

 


                    <EditItemTemplate>
                        <asp:DropDownList ID="DropDownList1" runat="server" 
                            DataSourceID="SqlDataSource1" DataTextField="NAME" 
                            DataValueField="ID" SelectedValue='<%# Bind("ID") %>' >
                        </asp:DropDownList>
                        <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
                            ConnectionString="<%$ ConnectionStrings:testConnectionString %>" 
                            SelectCommand="SELECT [ID], [NAME] FROM [test]">
                        </asp:SqlDataSource>
                    </EditItemTemplate>

 

在此記錄,以免忘記^^"

參考:http://www.eggheadcafe.com/community/asp-net/17/10251820/how-to-bound-values-to--dropdownlist-in-gridview--edit-item-template.aspx