更新不适用于 GridView
嘿伙计们, 我有一个绑定到 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您尚未传递
@Title 参数
,它是存储过程中的主键,并在where 子句
中使用。如果你想看到UpdateParameters中没有Title参数:
如果添加Title参数就可以了:
You have not passed the
@Title parameter
, which is your primary Key in your stored procedure and is used in thewhere Clause
.If you want to see that there is no Title Parameter in Update Parameters:
If you add the Title Parameter, it will work:
我还遇到了 GridView 未更新的问题。检查 GridView 的 RowUpdating 事件中的 e.Newvalues 字典表明记录的旧值正在发送到数据库 UPDATE 查询。 DataKeyNames 不是我的问题;我已经正确设置了。我的 SELECT 查询的 WHERE 子句引用了表单上 TextBox 的控制参数。我无意中将此文本框的 EnableViewState 设置为“False”。因此,GridView 在 UPDATE 发生之前重新绑定自身。将 TextBox 上的 EnableViewState 设置为“True”解决了该问题。
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.