2005中GridView 的DataSource中存储过程用到的参数和DataKeyNames的冲突

来源:互联网 发布:centos安装hadoop集群 编辑:程序博客网 时间:2024/05/18 10:21
<asp:GridView ID="gvEvent" runat="server" CellPadding="4" ForeColor="#4761C0" AllowPaging="True"
         AutoGenerateColumns
="False" DataSourceID="SqlDataSourceforgvEvent"  DataKeyNames="ID,EventDescription" Width="700px" OnPageIndexChanging="gvEvent_PageIndexChanging" OnDataBound="gvEvent_DataBound">
            
<FooterStyle BackColor="#4761C0" Font-Bold="True" ForeColor="White" />
            
<RowStyle BackColor="White" Height="21px" />
            
<EditRowStyle BackColor="#2461BF" />
            
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
            
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
            
<HeaderStyle BackColor="#4761C0" Font-Bold="True" ForeColor="White" Height="22px" />
            
<AlternatingRowStyle BackColor="White" />
            
<Columns>
            
                
<asp:BoundField DataField="ID" InsertVisible="False" ReadOnly="True"  HeaderText="编号"
                    SortExpression
="ID">
                    
<ItemStyle Width="0px" />
                
</asp:BoundField>
                
<asp:BoundField DataField="PoliceName" HeaderText="派出所"  />
                
<asp:BoundField DataField="PlaceName" HeaderText="报警场所" />
                
<asp:BoundField DataField="EventTime" HeaderText="接警时间" HtmlEncode="False" DataFormatString="{0:yyyy-MM-dd}" />
                
<asp:BoundField DataField="EventParentCategoryName" HeaderText="事件性质" />
                
<asp:BoundField DataField="EventChildCategoryName" HeaderText="事件类别" />
                
<asp:BoundField DataField="EventDescription" HeaderText="事件描述" Visible="False"  >
                    
<ItemStyle CssClass="display:none" />
                
</asp:BoundField>
                
<asp:CommandField HeaderText="操作" ShowDeleteButton="True"  DeleteText="&lt;div id=&quot;de&quot; onclick=&quot;JavaScript:return confirm('确定删除吗?')&quot;&gt;删除&lt;/div&gt;" />
            
</Columns>
        
</asp:GridView>



<asp:SqlDataSource ID="SqlDataSourceforgvEvent" runat="server" ConnectionString="<%$ ConnectionStrings:PoliceManagerConnectionString %>"
            DeleteCommand
="UP_Event_Delete" DeleteCommandType="StoredProcedure" SelectCommand="UP_Event_GetList" SelectCommandType="StoredProcedure">
            
<DeleteParameters>
                
<asp:Parameter Name="ID" Type="Int32" />
                
<asp:Parameter Name="EventDescription" Type="string" />
            
</DeleteParameters>

DataKeyNames   里面有ID和EventDescription

在存储过程中本来只用一个id的参数就可以,可是这样删除的话就会发生异常

要避免异常的发生只有两种方法:

一:把  DataKeyNames里面的EventDescription去掉,只留下一个“ID”;

<DeleteParameters>标签里面只添加一个参数:<asp:Parameter Name="ID" Type="Int32" />

二:在存储过程“UP_Event_Delete”里面定义两个参数,再在<DeleteParameters>标签里面只添加两个参数:<asp:Parameter Name="ID" Type="Int32" /><asp:Parameter Name="EventDescription"

 Type="string" />

原创粉丝点击