我對編程比較陌生,我無法弄清楚如何檢測和防止用戶將重復值輸入到我包含多個控制元件的網格視圖中。目前,我正在使用 foreach 回圈遍歷包含不能重復的值的控制元件,并且它在大多數情況下都有效,但是當我編輯表中的第一條記錄并嘗試“更新”記錄時,即使我沒有更改任何值,我收到一個空參考例外,我不知道為什么。我在下面附上了任何相關的代碼,并希望得到解決方案的任何和所有幫助。
請原諒這篇長篇文章,我試圖在不添加無關資訊的情況下盡可能徹底。
首先,我有一個網格視圖,其中包含一系列控制元件,允許用戶將資訊輸入到我們的資料庫中。網格視圖和相關控制元件如下所示。
<asp:GridView ID="GridView2" runat="server"
EmptyDataText="No Miscellaneous" AutoGenerateColumns="False"
ShowHeaderWhenEmpty="True" DataKeyNames="RecID"
ShowFooter="True" DataSourceID="SqlDataSource7" Width="95%"
OnRowDataBound="OnRowDataBound" >
<EmptyDataTemplate>
<asp:DropDownList ID="ddlMiscEquipmentNew" runat="server" DataSourceID="SqlDataSourcePartEquipment" DataValueField="RecID"
DataTextField="EquipmentPart" AppendDataBoundItems="True" AutoPostBack="True" Width="220px" Height="20px" style="margin-left: 70px;" >
<asp:ListItem Value="0">---SELECT---</asp:ListItem>
</asp:DropDownList>
<asp:DropDownList ID="ddlMiscNew" runat="server" DataSourceID="SqlDataSourceMisc" DataValueField="RecID" DataTextField="Description"
AppendDataBoundItems="true" Width="220px" Height="20px" AutoPostBack="True" OnSelectedIndexChanged="ddlMiscNew_SelectedIndexChanged"
OnDataBound="ddlMiscNew_DataBinding">
<asp:ListItem Value="0">---SELECT---</asp:ListItem>
</asp:DropDownList>
<asp:TextBox ID="txtMiscQuantityNew" runat="server" Width="95px" Style="margin-left: 68px;" AutoPostBack="True" OnTextChanged="txtMiscQuantityNew_TextChanged" />
<asp:TextBox ID="txtMiscCostNew" runat="server" Width="95px" Text="0" ReadOnly="True" />
<asp:TextBox ID="txtMiscRequestedNew" runat="server" Width="95px" Text="0" ReadOnly="True" />
<asp:TextBox ID="txtMiscTaxNew" runat="server" Width="95px" Text="0" />
<asp:Button ID="InsertDetail" runat="server" CommandName="InsertDetail" Height="25px" Text="Add Detail" Width="85px" UseSubmitBehavior="True" />
</EmptyDataTemplate>
<AlternatingRowStyle BackColor="#CCCCCC" />
<Columns>
<asp:CommandField ShowEditButton="True" FooterText="Add -->" ShowDeleteButton="true" HeaderStyle-Width="70px" />
<asp:BoundField DataField="RecID" HeaderText="RecID" SortExpression="RecID" ReadOnly="True" Visible="False" />
<asp:TemplateField HeaderText="Miscellaneous" ItemStyle-HorizontalAlign="center">
<ItemTemplate>
<asp:Label ID="lblMiscEquipmentAdd" Text='<%# Bind("EquipmentType") %>' runat="server" Width="220px" />
<asp:Label ID="lblMiscDescriptionAdd" Text='<%# Bind("MiscDescription") %>' runat="server" Width="220px" />
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="ddlMiscEquipmentEdit" runat="server" DataSourceID="SqlDataSourcePartEquipment" DataValueField="RecID" DataTextField="EquipmentPart" SelectedValue='<%# Eval("EquipmentID") %>'
AppendDataBoundItems="True" AutoPostBack="True" Width="220px" Height="20px">
<asp:ListItem Value="0">---SELECT---</asp:ListItem>
</asp:DropDownList>
<asp:DropDownList ID="ddlMiscEdit" runat="server" DataSourceID="SqlDataSourceMisc" DataValueField="RecID" DataTextField="Description" SelectedValue='<%# Eval("MiscType") %>' AppendDataBoundItems="True" Width="220px" Height="20px" AutoPostBack="True" OnSelectedIndexChanged="ddlMiscEdit_SelectedIndexChanged">
<asp:ListItem Value="0">---SELECT---</asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
<FooterTemplate>
<asp:DropDownList ID="ddlMiscEquipmentInsert" runat="server" DataSourceID="SqlDataSourcePartEquipment" DataValueField="RecID" DataTextField="EquipmentPart"
AppendDataBoundItems="True" Width="220px" Height="20px" style="margin-left: 29px;">
<asp:ListItem Value="0">---SELECT---</asp:ListItem>
</asp:DropDownList>
<asp:DropDownList ID="ddlMiscInsert" runat="server" DataSourceID="SqlDataSourceMisc" DataValueField="RecID" DataTextField="Description" AppendDataBoundItems="True" Width="220px" Height="20px" AutoPostBack="True" OnSelectedIndexChanged="ddlMiscInsert_SelectedIndexChanged">
<asp:ListItem Value="0">---SELECT---</asp:ListItem>
</asp:DropDownList>
</FooterTemplate>
</asp:TemplateField>
網格視圖中有更多控制元件,但這些是問題所在。如您所見,有許多下拉串列和文本框,有問題的控制元件是編輯項模板中的下拉串列“ddlMiscEdit”和標簽“lblMiscDescriptionAdd”。當用戶在上述 ddl 中進行選擇并繼續點擊每行末尾的插入詳細資訊按鈕時,它會將值添加到 gridview,然后通過行命令事件將它們添加到我的資料庫中。這按預期作業,我遇到的問題是當用戶嘗試編輯 gridview 的第一行并更改 ddlMiscEdit 的值時,它會觸發標簽 lblMiscDescriptionAdd 的空參考例外。
為了檢測重復項,我在 ddlMiscEdit 下拉串列的 selectedIndexChanged 事件中有一個 For Each 回圈,如下所示。
For Each row In GridView2.Rows
Dim miscdescription As Label = (CType(row.FindControl("lblMiscDescriptionAdd"), Label))
If miscdescription.Text = ddlMiscEdit.SelectedItem.Text Then
lblErrorMisc.Text = "Miscellaneous items can only be claimed once"
Return
Else
lblErrorMisc.Text = ""
Return
End If
Next
我認為此例外與以下事實有關:當用戶單擊編輯時,它會激活下拉串列并隱藏標簽,因此當涉及到回圈時,“lblMiscDescriptionAdd”在該特定背景關系中不存在。
我還嘗試將 foreach 回圈放在“更新”commandName 部分中gridview 的行命令事件中,這會導致相同的問題但在不同的位置。當這個回圈在上面提到的更新部分時,我沒有在 gridview 的第一條記錄上接收到這個例外,而是在第二條/第三條/第四條記錄中接收到它,基本上除了第一條記錄之外的所有內容。
我在 InsertDetail 命令中使用了相同的 foreach 回圈,它按預期作業,防止將重復值輸入到 gridview 中,只有當我嘗試根據我放置回圈的位置編輯給定行時才會出現例外。
我為這篇長文道歉,但我真的不知道如何簡化我的問題,超出我在這里所做的。我對編程很陌生,所以我不知道我是否可以以更好/更有效的方式來做這件事,但是任何幫助/批評都將不勝感激,因為我不知道如何解決這個問題。預先感謝您為幫助我解決此問題所做的任何努力。
uj5u.com熱心網友回復:
使長話短說?
不要嘗試處理 GV。如果其他用戶正在添加資料會發生什么。您很可能沒有使用更新的資料重新加載網格。
在一條記錄的保存代碼中,簡單地點擊/拉取/查詢主資料庫并檢查記錄。這樣一來,您就不會整天忙于處理并從 GV 中提取價值。
始終嘗試從資料管理的角度進行思考,而不是嘗試將實際的 GV 用作某種資料庫 - 它僅用于顯示和選擇要編輯的資料,而是檢查重復項,然后簡單地查詢資料庫以獲取該資訊并檢查此類記錄是否已經存在。
例如,我有一個簡單的 GV 與一些酒店。(您可以在此處嘗試該作業示例:
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/531719.html
標籤:网VB.net
