Gridview不执行UpdateCommand
这很有趣,因为我在两个不同的项目中使用相同的代码结构,其中一个可以正常工作,但另一个甚至无法执行。
这是asp.net代码(BulkEditGridview是一个扩展控件,它使用gridview作为基本控件,因此它的工作方式基本上相同)
<cc1:BulkEditGridView ID="BulkEditGridView1" runat="server" DataKeyNames="id"
AutoGenerateColumns="False" BackColor="White" BorderColor="#CCCCCC"
BorderStyle="None" BorderWidth="1px" CellPadding="3"
DataSourceID="titlesSqlDataSource" EnableInsert="False"
EnableModelValidation="True" SaveButtonID="Button1">
<Columns>
<asp:BoundField DataField="title" HeaderText="Title" SortExpression="title" />
<asp:BoundField DataField="siteid" HeaderText="SiteID" ReadOnly="True"
SortExpression="siteid" />
<asp:BoundField DataField="block" HeaderText="Block" SortExpression="block" >
<ControlStyle Width="50px" />
<HeaderStyle Width="50px" />
<ItemStyle Width="50px" />
</asp:BoundField>
</Columns>
<FooterStyle BackColor="White" ForeColor="#000066" />
<HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" />
<RowStyle ForeColor="#000066" />
<SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
Button1 回发甚至执行 BulkEditGridView1.Save();
这是数据源和按钮代码:
<asp:SqlDataSource ID="titlesSqlDataSource" runat="server"
ConnectionString="<%$ ConnectionStrings:xxxx %>"
SelectCommand="sp_getBottomLinksTitles" SelectCommandType="StoredProcedure"
UpdateCommand="sp_updateBlockTitle" UpdateCommandType="StoredProcedure">
<SelectParameters>
<asp:SessionParameter DefaultValue="1" Name="siteid" SessionField="siteid"
Type="Int32" />
</SelectParameters>
<UpdateParameters>
<asp:Parameter Name="title" Type=String />
<asp:Parameter Name="id" Type=Int32 />
<asp:Parameter Name="block" Type=Int32 />
</UpdateParameters>
</asp:SqlDataSource>
<asp:Button ID="Button1" runat="server" Text="Save" onclick="Button1_Click" />
该代码不起作用,但是这个代码就像一个魅力:
<cc1:BulkEditGridView ID="GridViewExistingInvoices" SaveButtonID="AcceptButton" DataKeyNames=id runat="server" DataSourceID="InvoicesSqlDatasource" Font-Bold="False" AutoGenerateColumns="False" BackColor="White" BorderColor="#999999" BorderStyle="Solid" BorderWidth="1px" CellPadding="3" ForeColor="Black" GridLines="Vertical" Width="915px" AllowSorting="True">
<FooterStyle BackColor="#CCCCCC" />
<EmptyDataTemplate> No Invoices to make</EmptyDataTemplate>
<Columns>
<asp:BoundField DataField="date" ItemStyle-Font-Size="11px" HeaderText="Invoice Date" SortExpression="date" DataFormatString="{0:dd-MMM-yyyy}" ItemStyle-VerticalAlign="Top">
<HeaderStyle Width="80px" />
<ItemStyle Width="80px" Font-Size="11px"/>
<ControlStyle Width="80px" Font-Size="11px" />
</asp:BoundField>
<asp:TemplateField HeaderText="Amount">
<ItemTemplate>
<asp:TextBox Text='<%# Bind("Amount1") %>' Font-Size="11px" Width="60px" runat=server ID="amount">
</asp:TextBox><br/>
<asp:TextBox Text='<%# Bind("Amount2") %>' Font-Size="11px" Width="60px" runat=server ID="TextBox1">
</asp:TextBox><br/>
<asp:TextBox Text='<%# Bind("Amount3") %>' Font-Size="11px" Width="60px" runat=server ID="TextBox2">
</asp:TextBox><br/>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Payment Due">
<ItemTemplate>
<asp:TextBox Text='<%# Bind("payment_Due") %>' Font-Size="11px" Width="70px" runat=server ID="tdue1">
</asp:TextBox><br/>
<asp:TextBox Text='<%# Bind("due2") %>' Font-Size="11px" Width="70px" runat=server ID="tdue2">
</asp:TextBox><br/>
<asp:TextBox Text='<%# Bind("due3") %>' Font-Size="11px" Width="70px" runat=server ID="tdue3">
</asp:TextBox><br/>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Amount" ItemStyle-Font-Size="11px" HeaderText="Total Amount" SortExpression="Amount" DataFormatString="{0:c}" ItemStyle-VerticalAlign="Top" >
<HeaderStyle Width="60px" />
<ItemStyle Width="60px" Font-Size="11px"/>
<ControlStyle Width="60px" Font-Size="11px" />
</asp:BoundField>
<asp:BoundField DataField="T" HeaderText="T" ItemStyle-Font-Size="11px" SortExpression="T" ItemStyle-VerticalAlign="Top">
<HeaderStyle Width="25px" />
<ItemStyle Width="25px" Font-Size="11px"/>
<ControlStyle Width="40px" Font-Size="11px"/>
</asp:BoundField>
<asp:TemplateField HeaderText="Invoice Description">
<ItemTemplate>
<asp:TextBox Text='<%# Bind("Description") %>' Font-Size="11px" Width="200px" TextMode=MultiLine Height=50px runat=server ID="tDescription">
</asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Comments">
<ItemTemplate>
<asp:TextBox Text='<%# Bind("Comments") %>' Font-Size="11px" Width="190px" TextMode=MultiLine Height=50px runat=server ID="tComments">
</asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:CheckBoxField DataField="isblack" ItemStyle-Font-Size="11px" HeaderText="B" SortExpression="isblack" >
<HeaderStyle Width="20px" />
</asp:CheckBoxField>
<asp:CheckBoxField DataField="plusiva" ItemStyle-Font-Size="11px" HeaderText="+IVA" SortExpression="plusiva" >
<HeaderStyle Width="20px" />
</asp:CheckBoxField>
<asp:HyperLinkField DataNavigateUrlFields="url" ItemStyle-Font-Size="11px" HeaderText="Print" Text="Print" >
<HeaderStyle HorizontalAlign="Left" Width="50px" />
</asp:HyperLinkField>
<asp:HyperLinkField DataNavigateUrlFields="urlDeleteP" ItemStyle-Font-Size="11px" Text="Delete" />
</Columns>
<PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="#CCCCCC" />
</cc1:BulkEditGridView>
<asp:SqlDataSource ID="InvoicesSqlDatasource" runat="server" ConnectionString="<%$ ConnectionStrings:xxxConnectionString %>"
SelectCommand="SELECT payment.id,'Not assigned' id1,plusiva,payment.id, payment.[date],CAST( [totalAmount] as decimal(10,2)) as amount,amount as amount1,amount2,amount3,due2,due3, [payment_due], payment.[comments],paymentform AS T , [isblack],'view_invoice.aspx?id='+cast(payment.id as nvarchar(10)) as url, 'delete_preInvoice.aspx?id='+cast(payment.id as nvarchar(10)) as urlDeleteP , Description FROM [payment],lead WHERE (payment.clientid = @id AND payment.clientid=lead.id AND invoiceId is null) ORDER BY [date]"
UpdateCommand="sp_updateInvoiceN" UpdateCommandType=StoredProcedure>
<SelectParameters>
<asp:QueryStringParameter DefaultValue="1" Name="id" QueryStringField="id" Type="Int32" />
</SelectParameters>
<UpdateParameters>
<asp:Parameter Name="date" Type=DateTime/>
<asp:Parameter Name="payment_due" Type=DateTime/>
<asp:Parameter Name="comments" type=String />
<asp:Parameter Name="Description" type=String />
<asp:Parameter Name="amount" type=Double />
<asp:Parameter Name="id" type=String />
<asp:Parameter Name="isBlack" type=Boolean />
<asp:Parameter Name="plusiva" type=Boolean />
<asp:Parameter Name="due2" Type=DateTime/>
<asp:Parameter Name="due3" Type=DateTime/>
<asp:Parameter Name="amount2" type=Double />
<asp:Parameter Name="amount3" type=Double />
<asp:Parameter Name="amount1" type=Double />
</UpdateParameters>
</asp:SqlDataSource>
结构相同,使用相同的控件,相同的更新方式,Datakeynames 都存在于两者中,两者执行相同按钮单击事件上的事情,有点奇怪,使用 sql server profiler 我可以看到它执行 updatecommand,并从 Sql server management studio 测试存储过程,它工作正常。
这也不是连接问题,因为 selectCommand 可以工作,并且它从数据库加载信息。
有什么想法吗?
It is very funny because I use the same structure of code in 2 different projects and 1 one of them works fine but the other will just not even execute.
Here is the asp.net code (BulkEditGridview is an extended control that uses gridview as base control so it basically works the same way)
<cc1:BulkEditGridView ID="BulkEditGridView1" runat="server" DataKeyNames="id"
AutoGenerateColumns="False" BackColor="White" BorderColor="#CCCCCC"
BorderStyle="None" BorderWidth="1px" CellPadding="3"
DataSourceID="titlesSqlDataSource" EnableInsert="False"
EnableModelValidation="True" SaveButtonID="Button1">
<Columns>
<asp:BoundField DataField="title" HeaderText="Title" SortExpression="title" />
<asp:BoundField DataField="siteid" HeaderText="SiteID" ReadOnly="True"
SortExpression="siteid" />
<asp:BoundField DataField="block" HeaderText="Block" SortExpression="block" >
<ControlStyle Width="50px" />
<HeaderStyle Width="50px" />
<ItemStyle Width="50px" />
</asp:BoundField>
</Columns>
<FooterStyle BackColor="White" ForeColor="#000066" />
<HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" />
<RowStyle ForeColor="#000066" />
<SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
Button1 postbacks and even executes BulkEditGridView1.Save();
Here is the Datasource and Button code:
<asp:SqlDataSource ID="titlesSqlDataSource" runat="server"
ConnectionString="<%$ ConnectionStrings:xxxx %>"
SelectCommand="sp_getBottomLinksTitles" SelectCommandType="StoredProcedure"
UpdateCommand="sp_updateBlockTitle" UpdateCommandType="StoredProcedure">
<SelectParameters>
<asp:SessionParameter DefaultValue="1" Name="siteid" SessionField="siteid"
Type="Int32" />
</SelectParameters>
<UpdateParameters>
<asp:Parameter Name="title" Type=String />
<asp:Parameter Name="id" Type=Int32 />
<asp:Parameter Name="block" Type=Int32 />
</UpdateParameters>
</asp:SqlDataSource>
<asp:Button ID="Button1" runat="server" Text="Save" onclick="Button1_Click" />
That code is the one not working, however this one works like a charm:
<cc1:BulkEditGridView ID="GridViewExistingInvoices" SaveButtonID="AcceptButton" DataKeyNames=id runat="server" DataSourceID="InvoicesSqlDatasource" Font-Bold="False" AutoGenerateColumns="False" BackColor="White" BorderColor="#999999" BorderStyle="Solid" BorderWidth="1px" CellPadding="3" ForeColor="Black" GridLines="Vertical" Width="915px" AllowSorting="True">
<FooterStyle BackColor="#CCCCCC" />
<EmptyDataTemplate> No Invoices to make</EmptyDataTemplate>
<Columns>
<asp:BoundField DataField="date" ItemStyle-Font-Size="11px" HeaderText="Invoice Date" SortExpression="date" DataFormatString="{0:dd-MMM-yyyy}" ItemStyle-VerticalAlign="Top">
<HeaderStyle Width="80px" />
<ItemStyle Width="80px" Font-Size="11px"/>
<ControlStyle Width="80px" Font-Size="11px" />
</asp:BoundField>
<asp:TemplateField HeaderText="Amount">
<ItemTemplate>
<asp:TextBox Text='<%# Bind("Amount1") %>' Font-Size="11px" Width="60px" runat=server ID="amount">
</asp:TextBox><br/>
<asp:TextBox Text='<%# Bind("Amount2") %>' Font-Size="11px" Width="60px" runat=server ID="TextBox1">
</asp:TextBox><br/>
<asp:TextBox Text='<%# Bind("Amount3") %>' Font-Size="11px" Width="60px" runat=server ID="TextBox2">
</asp:TextBox><br/>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Payment Due">
<ItemTemplate>
<asp:TextBox Text='<%# Bind("payment_Due") %>' Font-Size="11px" Width="70px" runat=server ID="tdue1">
</asp:TextBox><br/>
<asp:TextBox Text='<%# Bind("due2") %>' Font-Size="11px" Width="70px" runat=server ID="tdue2">
</asp:TextBox><br/>
<asp:TextBox Text='<%# Bind("due3") %>' Font-Size="11px" Width="70px" runat=server ID="tdue3">
</asp:TextBox><br/>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Amount" ItemStyle-Font-Size="11px" HeaderText="Total Amount" SortExpression="Amount" DataFormatString="{0:c}" ItemStyle-VerticalAlign="Top" >
<HeaderStyle Width="60px" />
<ItemStyle Width="60px" Font-Size="11px"/>
<ControlStyle Width="60px" Font-Size="11px" />
</asp:BoundField>
<asp:BoundField DataField="T" HeaderText="T" ItemStyle-Font-Size="11px" SortExpression="T" ItemStyle-VerticalAlign="Top">
<HeaderStyle Width="25px" />
<ItemStyle Width="25px" Font-Size="11px"/>
<ControlStyle Width="40px" Font-Size="11px"/>
</asp:BoundField>
<asp:TemplateField HeaderText="Invoice Description">
<ItemTemplate>
<asp:TextBox Text='<%# Bind("Description") %>' Font-Size="11px" Width="200px" TextMode=MultiLine Height=50px runat=server ID="tDescription">
</asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Comments">
<ItemTemplate>
<asp:TextBox Text='<%# Bind("Comments") %>' Font-Size="11px" Width="190px" TextMode=MultiLine Height=50px runat=server ID="tComments">
</asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:CheckBoxField DataField="isblack" ItemStyle-Font-Size="11px" HeaderText="B" SortExpression="isblack" >
<HeaderStyle Width="20px" />
</asp:CheckBoxField>
<asp:CheckBoxField DataField="plusiva" ItemStyle-Font-Size="11px" HeaderText="+IVA" SortExpression="plusiva" >
<HeaderStyle Width="20px" />
</asp:CheckBoxField>
<asp:HyperLinkField DataNavigateUrlFields="url" ItemStyle-Font-Size="11px" HeaderText="Print" Text="Print" >
<HeaderStyle HorizontalAlign="Left" Width="50px" />
</asp:HyperLinkField>
<asp:HyperLinkField DataNavigateUrlFields="urlDeleteP" ItemStyle-Font-Size="11px" Text="Delete" />
</Columns>
<PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="#CCCCCC" />
</cc1:BulkEditGridView>
<asp:SqlDataSource ID="InvoicesSqlDatasource" runat="server" ConnectionString="<%$ ConnectionStrings:xxxConnectionString %>"
SelectCommand="SELECT payment.id,'Not assigned' id1,plusiva,payment.id, payment.[date],CAST( [totalAmount] as decimal(10,2)) as amount,amount as amount1,amount2,amount3,due2,due3, [payment_due], payment.[comments],paymentform AS T , [isblack],'view_invoice.aspx?id='+cast(payment.id as nvarchar(10)) as url,
'delete_preInvoice.aspx?id='+cast(payment.id as nvarchar(10)) as urlDeleteP
, Description FROM [payment],lead WHERE (payment.clientid = @id AND payment.clientid=lead.id AND invoiceId is null) ORDER BY [date]"
UpdateCommand="sp_updateInvoiceN" UpdateCommandType=StoredProcedure>
<SelectParameters>
<asp:QueryStringParameter DefaultValue="1" Name="id" QueryStringField="id" Type="Int32" />
</SelectParameters>
<UpdateParameters>
<asp:Parameter Name="date" Type=DateTime/>
<asp:Parameter Name="payment_due" Type=DateTime/>
<asp:Parameter Name="comments" type=String />
<asp:Parameter Name="Description" type=String />
<asp:Parameter Name="amount" type=Double />
<asp:Parameter Name="id" type=String />
<asp:Parameter Name="isBlack" type=Boolean />
<asp:Parameter Name="plusiva" type=Boolean />
<asp:Parameter Name="due2" Type=DateTime/>
<asp:Parameter Name="due3" Type=DateTime/>
<asp:Parameter Name="amount2" type=Double />
<asp:Parameter Name="amount3" type=Double />
<asp:Parameter Name="amount1" type=Double />
</UpdateParameters>
</asp:SqlDataSource>
The structure is the same, using the same control, same way of update, Datakeynames is there in both, both executes the same thing on button click event, it is a bit weird, using sql server profiler I can see it doenst execute the updatecommand, and testing the stored procedure from Sql server management studio it works fine.
It is not a Connection problem either because the selectCommand works, and it loads the information from the DB.
Any ideas??
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不知道它是否相关,这有点奇怪,但是您的工作代码中有字符串类型的 id:
同时,在不工作的代码中,
如果您确实在 BulkEditGridView1.Save(); 中使用了完全相同的代码;方法,理论上可能会造成麻烦。
Don't know if it's relevant, it is a bit strange, but you have in your working code id of type string:
Meanwhile in the not working one you have
If you really use pretty the same code in your BulkEditGridView1.Save(); method, it could theoretically cause troubles.
我认为这个问题是因为你的按钮上有一个自定义事件引起的。
框架将开始寻找该事件,并认为您将自己处理保存。
但为什么那里有一个保存按钮?您可以使用 gridview 控件中内置的按钮。您可以激活属性或向导中的编辑选项。
因为如果您想使用自己的控件,那么每行都需要一个控件,因此您需要添加一个模板行并在其中添加按钮。
嗯,我认为没有任何事情发生是很正常的。我认为,当按钮位于 gridview 内部时,将使用 gridview 将其链接到按钮的属性。
现在我知道您在寻找什么,我建议您看一下本教程。
它讨论的是您试图完成的同一件事。
链接 !
I think the problem is caused because you got a custom event on your button.
The framework will start to look for that event and will think you will handle the save by yourself.
But why do you have a save button over there? You can use the buttons which are build in in the the gridview control. You can activate the edit option in the properties or wizard.
Because if you want to use your own controls then you will need one on each row so you will need to add a templaterow and add the button in there.
Well i think it's normal that nothing is happening. The property you used of the gridview to link it to the button is used when the button is inside your gridview i think.
Now that i know what you are looking for i would suggest you take a look at this tutorial.
It discusses the same thing you try to accomplish.
Link!