簡體   English   中英

帶有Sqldatasource編輯/刪除按鈕的Gridview

[英]Gridview with Sqldatasource Edit/Delete buttons

我正在使用帶有c#代碼的asp.net,而我想要做的就是擁有一個gridview + sqldatasource,可以在運行時進行操作,並且無論什么原因按鈕都不起作用。 我有一個登錄頁面,該頁面通過查詢字符串傳遞用戶名,然后此頁面現在僅是gridview。 當我單擊刪除按鈕時,它將立即帶我進入登錄頁面,字段再次為空。 出於測試目的,我僅使用刪除按鈕,但是我也對編輯按鈕具有相同的經驗,它從未像看起來那樣使用“更新模式”。

        <asp:GridView DataKeyNames="ID" ID="GridView1" runat="server" DataSourceID="SqlDataSource1" AllowPaging="True" AllowSorting="True" PageSize="8" OnRowDataBound="GridView1_RowDataBound">
            <Columns>
                <asp:CommandField ShowDeleteButton="True" />
            </Columns>
        </asp:GridView>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString='<%$ ConnectionStrings:WebMasterLogConnectionString2 %>'
            SelectCommand="SELECT * FROM [WebmasterLogs] WHERE ([Username] = @Username) ORDER BY [ID]"
            ConflictDetection="CompareAllValues" 
            DeleteCommand="DELETE FROM [WebmasterLogs] WHERE [ID] = @original_ID" 
            OldValuesParameterFormatString="original_{0}" >
            <DeleteParameters>
                <asp:Parameter Name="original_ID" Type="Int32" />
            </DeleteParameters>
            <SelectParameters>
                <asp:Parameter Name="ID" Type="Int32" />
                <asp:QueryStringParameter Name="Username" QueryStringField="Username" Type="String" />
                <asp:Parameter Name="LogEntryDate" Type="DateTime" />
                <asp:Parameter Name="TimeSpent" Type="String" />
                <asp:Parameter Name="Description" Type="String" />
            </SelectParameters>
        </asp:SqlDataSource>

網絡配置:

<configuration>
  <configSections>
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  </configSections>
  <system.web>
    <compilation debug="true" targetFramework="4.5" />
    <httpRuntime targetFramework="4.5" />
  </system.web>
  <appSettings>
    <add key="ValidationSettings:UnobtrusiveValidationMode" value="None" />
  </appSettings>
  <connectionStrings>
    <add name="WebMasterLogConnectionString" connectionString="Data Source=35sql;Initial Catalog=WebMasterLog;Persist Security Info=False;User ID=*****;Password=*********"
      providerName="System.Data.SqlClient" />
  </connectionStrings>
</configuration>

由於設置了OldValuesParameterFormatString="original_{0}"因此@original_ID參數將由數據控件自動提交,因此無需在此處定義Delete參數。

因此,參數@original_ID將已經具有適當的ID值。


<asp:Parameter ... />聲明僅在要以編程方式設置參數時使用。

想通了。 我尚未正確定義單擊事件以進行編輯/更新/刪除。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM