更新不适用于 GridView

发布于 2024-11-08 19:46:02 字数 6210 浏览 2 评论 0原文

嘿伙计们, 我有一个绑定到 SqlDataSource 的 GridView,一切正常,只是更新不起作用,我正在调用存储过程,并且所有参数及其数据类型都正确,实际上没有任何事件在该更新按钮上起作用,我尝试更改将CommandName更改为“Change”并创建OnCommand事件,并在代码中写入Response Message。但更新按钮上没有任何反应,取消按钮工作正常,删除也工作正常,只是更新有问题,即使程序有一些问题至少应该引发事件,我什至检查了 Sql Profiler,但更新程序从来没有命中 Sql Server 2008...

但是当我删除 SqlDataSource 时,一切都工作正常,更新命令也具有相同的存储过程。

请帮我解决这个问题,我被困在这里

<asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource1" AutoGenerateColumns="False" 
    CellPadding="4" DataKeyNames="Title" EnableModelValidation="True"
    ForeColor="#333333" GridLines="None"
    ShowFooter="True" Width="950" AllowPaging="true" PageSize="10">
    <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
    <Columns>
        <asp:TemplateField HeaderText="Title">
            <ItemTemplate>
                <%# Eval("Title") %>
            </ItemTemplate>
            <EditItemTemplate>
                <asp:Label ID="txtEditTitle" runat="server" Text='<%# Bind("Title") %>'></asp:Label>
            </EditItemTemplate>
         </asp:TemplateField>
        <asp:TemplateField HeaderText="Quote">
            <ItemTemplate>
                <%# Eval("Quote") %>
            </ItemTemplate>
            <EditItemTemplate>
                <asp:TextBox ID="txtEditQuote" runat="server" Text='<%# Bind("Quote") %>'></asp:TextBox>
            </EditItemTemplate>
        </asp:TemplateField >
        <asp:TemplateField HeaderText="Quote Link">
            <ItemTemplate>
                <%# Eval("QuoteLink") %>
            </ItemTemplate>
            <EditItemTemplate>
                <asp:TextBox ID="txtEditQuoteLink" runat="server" Text='<%# Bind("QuoteLink") %>'></asp:TextBox>
            </EditItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Published">
            <ItemTemplate>
                <asp:CheckBox ID="chkBox" runat="server" Checked='<%# Convert.ToBoolean(Eval("Published")) %>' Enabled = "false" />  
            </ItemTemplate>
            <EditItemTemplate>
                <asp:CheckBox ID="chkEditBox" runat="server" Checked='<%# Bind("Published") %>' Enabled="true" />
            </EditItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="PublishedDate">
            <ItemTemplate>
                <%# Convert.ToDateTime(DataBinder.Eval(Container.DataItem,"PublishedDate")).ToString("MM.dd.yyyy") %>
            </ItemTemplate>
            <EditItemTemplate>
                <asp:TextBox ID="txtEditPublishedDate" runat="server" Text='<%# Bind("PublishedDate") %>'></asp:TextBox>
            </EditItemTemplate>
        </asp:TemplateField>
       <asp:TemplateField HeaderText="Commands">
            <ItemTemplate>
                <asp:ImageButton runat="server" ID="Edit" ImageUrl="/images/edit.gif" CommandName="Edit" />
                <asp:ImageButton runat="server" ID="Delete" ImageUrl="/images/delete.gif" CommandName="Delete" />
            </ItemTemplate>
            <EditItemTemplate>
                <asp:Button runat="server" ID="btn1" CommandName="Update" Text="Update" />
                <asp:ImageButton runat="server" ID="Cancel" ImageUrl="/images/delete.gif" CommandName="Cancel" />
            </EditItemTemplate>
        </asp:TemplateField>
    </Columns>
    <EditRowStyle BackColor="#999999" />
    <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
    <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" HorizontalAlign="Left" ForeColor="White" />
    <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
    <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
    <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
</asp:GridView>
  <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
    ConnectionString="<%$ ConnectionStrings:SiteSqlServer %>" 
    DeleteCommand="DELETE FROM [Quotes] WHERE [Title] = @Title"
    SelectCommand= "SELECT [Title], [Quote], [QuoteLink], [Published], [PublishedDate] FROM [Quotes]" 
    UpdateCommand="sp_UpdateQuotes" UpdateCommandType="StoredProcedure"
    InsertCommand="INSERT INTO [Quotes] ([Title], [Quote], [QuoteLink], [Published], [PublishedDate]) VALUES (@Title, @Quote, @QuoteLink, @Published, @PublishedDate)">
    <DeleteParameters>
        <asp:Parameter Name="Title" Type="String" />
    </DeleteParameters>
    <InsertParameters>
        <asp:Parameter Name="Title" Type="String" />
        <asp:Parameter Name="Quote" Type="String" />
        <asp:Parameter Name="QuoteLink" Type="String" />
        <asp:Parameter Name="Published" Type="Boolean" />
        <asp:Parameter Name="PublishedDate" Type="DateTime" />
    </InsertParameters>
    <UpdateParameters>
        <asp:Parameter Name="Quote" Type="String" DefaultValue = "QUotes are simple" />
        <asp:Parameter Name="QuoteLink" Type="String" DefaultValue = "QUotes are Linked" />
        <asp:Parameter Name="Published" Type="Boolean" DefaultValue = "False" />
        <asp:Parameter Name="PublishedDate" Type="DateTime" DefaultValue = "05/15/2011" />
    </UpdateParameters>
</asp:SqlDataSource>

更改过程 [dbo].[sp_UpdateQuotes]
@Title varchar(max),
@引用 varchar(max),
@QuoteLink varchar(max),
@发布位,
@PublishedDate 日期时间
AS
开始
更新 dbo.Quotes SET
报价 = @Quote,
引用链接 = @QuoteLink,
已发布 = @已发布,
发布日期 = @发布日期

WHERE Title = @Title  

If @Published = 1  
BEGIN  
    UPDATE dbo.Quotes SET Published = 0 WHERE Title <> @Title AND Published = 1  
END  

END
希望这能让您清楚地了解是什么让我更新

Hey guys,
I have a GridView which is binded to SqlDataSource, everything works fine, just update does not works, i am calling stored procedure, and all parameters and its dataTypes are proper, actually none of the events work on that update button, i tried to change the CommandName to "Change" and created OnCommand Event, and wrote Response Message in the code. but nothing happens on the update button, cancel button works fine, and also delete works, there is just a problem with update, even if the procedure has some issue atleast event should raised, i even checked in Sql Profiler, but the update procedure never hits Sql Server 2008...

But when i remove SqlDataSource then everything works fine also the update command with same stored procedure.

Please help me out for this, i have stuck here badly

<asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource1" AutoGenerateColumns="False" 
    CellPadding="4" DataKeyNames="Title" EnableModelValidation="True"
    ForeColor="#333333" GridLines="None"
    ShowFooter="True" Width="950" AllowPaging="true" PageSize="10">
    <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
    <Columns>
        <asp:TemplateField HeaderText="Title">
            <ItemTemplate>
                <%# Eval("Title") %>
            </ItemTemplate>
            <EditItemTemplate>
                <asp:Label ID="txtEditTitle" runat="server" Text='<%# Bind("Title") %>'></asp:Label>
            </EditItemTemplate>
         </asp:TemplateField>
        <asp:TemplateField HeaderText="Quote">
            <ItemTemplate>
                <%# Eval("Quote") %>
            </ItemTemplate>
            <EditItemTemplate>
                <asp:TextBox ID="txtEditQuote" runat="server" Text='<%# Bind("Quote") %>'></asp:TextBox>
            </EditItemTemplate>
        </asp:TemplateField >
        <asp:TemplateField HeaderText="Quote Link">
            <ItemTemplate>
                <%# Eval("QuoteLink") %>
            </ItemTemplate>
            <EditItemTemplate>
                <asp:TextBox ID="txtEditQuoteLink" runat="server" Text='<%# Bind("QuoteLink") %>'></asp:TextBox>
            </EditItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Published">
            <ItemTemplate>
                <asp:CheckBox ID="chkBox" runat="server" Checked='<%# Convert.ToBoolean(Eval("Published")) %>' Enabled = "false" />  
            </ItemTemplate>
            <EditItemTemplate>
                <asp:CheckBox ID="chkEditBox" runat="server" Checked='<%# Bind("Published") %>' Enabled="true" />
            </EditItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="PublishedDate">
            <ItemTemplate>
                <%# Convert.ToDateTime(DataBinder.Eval(Container.DataItem,"PublishedDate")).ToString("MM.dd.yyyy") %>
            </ItemTemplate>
            <EditItemTemplate>
                <asp:TextBox ID="txtEditPublishedDate" runat="server" Text='<%# Bind("PublishedDate") %>'></asp:TextBox>
            </EditItemTemplate>
        </asp:TemplateField>
       <asp:TemplateField HeaderText="Commands">
            <ItemTemplate>
                <asp:ImageButton runat="server" ID="Edit" ImageUrl="/images/edit.gif" CommandName="Edit" />
                <asp:ImageButton runat="server" ID="Delete" ImageUrl="/images/delete.gif" CommandName="Delete" />
            </ItemTemplate>
            <EditItemTemplate>
                <asp:Button runat="server" ID="btn1" CommandName="Update" Text="Update" />
                <asp:ImageButton runat="server" ID="Cancel" ImageUrl="/images/delete.gif" CommandName="Cancel" />
            </EditItemTemplate>
        </asp:TemplateField>
    </Columns>
    <EditRowStyle BackColor="#999999" />
    <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
    <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" HorizontalAlign="Left" ForeColor="White" />
    <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
    <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
    <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
</asp:GridView>
  <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
    ConnectionString="<%$ ConnectionStrings:SiteSqlServer %>" 
    DeleteCommand="DELETE FROM [Quotes] WHERE [Title] = @Title"
    SelectCommand= "SELECT [Title], [Quote], [QuoteLink], [Published], [PublishedDate] FROM [Quotes]" 
    UpdateCommand="sp_UpdateQuotes" UpdateCommandType="StoredProcedure"
    InsertCommand="INSERT INTO [Quotes] ([Title], [Quote], [QuoteLink], [Published], [PublishedDate]) VALUES (@Title, @Quote, @QuoteLink, @Published, @PublishedDate)">
    <DeleteParameters>
        <asp:Parameter Name="Title" Type="String" />
    </DeleteParameters>
    <InsertParameters>
        <asp:Parameter Name="Title" Type="String" />
        <asp:Parameter Name="Quote" Type="String" />
        <asp:Parameter Name="QuoteLink" Type="String" />
        <asp:Parameter Name="Published" Type="Boolean" />
        <asp:Parameter Name="PublishedDate" Type="DateTime" />
    </InsertParameters>
    <UpdateParameters>
        <asp:Parameter Name="Quote" Type="String" DefaultValue = "QUotes are simple" />
        <asp:Parameter Name="QuoteLink" Type="String" DefaultValue = "QUotes are Linked" />
        <asp:Parameter Name="Published" Type="Boolean" DefaultValue = "False" />
        <asp:Parameter Name="PublishedDate" Type="DateTime" DefaultValue = "05/15/2011" />
    </UpdateParameters>
</asp:SqlDataSource>


ALTER PROCEDURE [dbo].[sp_UpdateQuotes]
@Title varchar(max),
@Quote varchar(max),
@QuoteLink varchar(max),
@Published bit,
@PublishedDate DateTime
AS
BEGIN
UPDATE dbo.Quotes SET
Quote = @Quote,
QuoteLink = @QuoteLink,
Published = @Published,
PublishedDate = @PublishedDate

WHERE Title = @Title  

If @Published = 1  
BEGIN  
    UPDATE dbo.Quotes SET Published = 0 WHERE Title <> @Title AND Published = 1  
END  

END

Hope this would give you clear idea about what is messing me for update

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

辞旧 2024-11-15 19:46:02

您尚未传递@Title 参数,它是存储过程中的主键,并在where 子句 中使用。

如果你想看到UpdateParameters中没有Title参数:

<UpdateParameters>
    <asp:Parameter Name="Quote" Type="String" />
    <asp:Parameter Name="QuoteLink" Type="String" />
    <asp:Parameter Name="Published" Type="Boolean" />
    <asp:Parameter Name="PublishedDate" Type="DateTime" />
</UpdateParameters>

如果添加Title参数就可以了:

    <asp:Parameter Name="Title" Type="String" />

You have not passed the @Title parameter, which is your primary Key in your stored procedure and is used in the where Clause.

If you want to see that there is no Title Parameter in Update Parameters:

<UpdateParameters>
    <asp:Parameter Name="Quote" Type="String" />
    <asp:Parameter Name="QuoteLink" Type="String" />
    <asp:Parameter Name="Published" Type="Boolean" />
    <asp:Parameter Name="PublishedDate" Type="DateTime" />
</UpdateParameters>

If you add the Title Parameter, it will work:

    <asp:Parameter Name="Title" Type="String" />
辞慾 2024-11-15 19:46:02

我还遇到了 GridView 未更新的问题。检查 GridView 的 RowUpdating 事件中的 e.Newvalues 字典表明记录的旧值正在发送到数据库 UPDATE 查询。 DataKeyNames 不是我的问题;我已经正确设置了。我的 SELECT 查询的 WHERE 子句引用了表单上 TextBox 的控制参数。我无意中将此文本框的 EnableViewState 设置为“False”。因此,GridView 在 UPDATE 发生之前重新绑定自身。将 TextBox 上的 EnableViewState 设置为“True”解决了该问题。

    Protected Sub MyGridView_RowUpdating _
    (sender As Object, e As System.Web.UI.WebControls.GridViewUpdateEventArgs) _
    Handles MyGridView.RowUpdating

    ' Inspect the parameters being sent to the database for an ASP NET GridView UPDATE.

    Dim I As Integer

    I = 0

    For Each MVO As System.Collections.DictionaryEntry In e.OldValues
        If MVO.Value Is DBNull.Value OrElse MVO.Value Is Nothing Then
            Debug.Print(I.ToString + ": " + MVO.Key + " Value:  ")
        Else
            Debug.Print(I.ToString + ": " + MVO.Key + " Value:  " + MVO.Value.ToString)
        End If

        I += 1
    Next MVO

    I = 0
    For Each MVN As System.Collections.DictionaryEntry In e.NewValues
        If MVN.Value Is DBNull.Value OrElse MVN.Value Is Nothing Then
            Debug.Print(I.ToString + ": " + MVN.Key + " Value:  ")
        Else
            Debug.Print(I.ToString + ": " + MVN.Key + " Value:  " + MVN.Value.ToString)
        End If
        I += 1
    Next MVN


End Sub

I also had a problem with a GridView that was not updating. Inspection of the e.Newvalues Dictionary in the GridView's RowUpdating event showed that the old values for the record were being sent to the database UPDATE query. DataKeyNames was not my problem; I had it set correctly. The WHERE clause of my SELECT query referenced a control parameter against a TextBox on my form. I had inadvertently set EnableViewState for this textbox to "False". Because of this, the GridView was rebinding itself before the UPDATE occurred. Setting EnableViewState on the TextBox to "True" fixed the problem.

    Protected Sub MyGridView_RowUpdating _
    (sender As Object, e As System.Web.UI.WebControls.GridViewUpdateEventArgs) _
    Handles MyGridView.RowUpdating

    ' Inspect the parameters being sent to the database for an ASP NET GridView UPDATE.

    Dim I As Integer

    I = 0

    For Each MVO As System.Collections.DictionaryEntry In e.OldValues
        If MVO.Value Is DBNull.Value OrElse MVO.Value Is Nothing Then
            Debug.Print(I.ToString + ": " + MVO.Key + " Value:  ")
        Else
            Debug.Print(I.ToString + ": " + MVO.Key + " Value:  " + MVO.Value.ToString)
        End If

        I += 1
    Next MVO

    I = 0
    For Each MVN As System.Collections.DictionaryEntry In e.NewValues
        If MVN.Value Is DBNull.Value OrElse MVN.Value Is Nothing Then
            Debug.Print(I.ToString + ": " + MVN.Key + " Value:  ")
        Else
            Debug.Print(I.ToString + ": " + MVN.Key + " Value:  " + MVN.Value.ToString)
        End If
        I += 1
    Next MVN


End Sub
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文