gridview更新将空值保存到数据库
我的页面上有一个gridview,当我单击特定行上的更新链接时,编辑字段会正常打开,但是当我输入一些信息然后单击更新链接时,所有内容都会刷新,但更新的值为空白数据库。我试图在row_updated事件中捕获任何异常消息,但我得到一个成功的更新消息,因为显然没有例外。我无法弄清楚为什么更新直到最近才起作用。
Gridview代码
I have a gridview on my page and when I click the update link on a particular row the edit fields open up properly, but when i type in some information and then click the update link everything refreshes but the values updated are blank in the db. I have tried to catch any exception message in the row_updated event, but i get a successful update message as there apparently is no exceptions. I can't figure out why the update isn't working as it was until recently.
Gridview Code
<asp:GridView ID="GridView1" runat="server" AllowPaging="True"
AllowSorting="True" AutoGenerateColumns="False" BorderStyle="Solid"
BorderWidth="1px" CellPadding="1" CellSpacing="1" DataKeyNames="AlertID"
DataSourceID="SqlDataSource1" BackColor="White" HorizontalAlign="Center" OnPreRender="GridView1_PreRender"
Width="100%" CssClass="mGrid" PagerStyle-CssClass="pgr" OnRowDataBound="Gridview1_RowDataBound"
DataMember="DefaultView" OnRowUpdated="Gridview1_RowUpdated" OnSorting="GridView1OnSorting"
OnRowEditing="GridView1_RowEditing" OnPageIndexChanged="Gridview1OnPageIndexChanged"
EmptyDataText="No Matching records were found">
<PagerSettings Mode="NumericFirstLast" PageButtonCount="25" />
<RowStyle BorderColor="Black" BorderStyle="Solid" BorderWidth="1px"
Font-Names="Arial" Font-Size="X-Small" ForeColor="Black" />
<EmptyDataRowStyle Font-Bold="True" Font-Size="Medium" ForeColor="Red" />
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<asp:CheckBox runat="server" ID="cbSelectAll" AutoPostBack="false" OnClick="ChangeAllCheckBoxStates(this.checked)" />
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="chkStatus" runat="server" AutoPostBack="false" onclick="ChangeHeaderAsNeeded()" />
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField showeditbutton="true"
edittext="Assign"
canceltext="Cancel"
updatetext="Assign"
headertext="Assign" CausesValidation="true" ValidationGroup="Grid1Group" />
<asp:HyperLinkField DataNavigateUrlFields="AlertID,AlertType"
DataNavigateUrlFormatString="AlertDetails.aspx?AlertID={0}&AlertType={1}"
NavigateUrl="~/AlertDetails.aspx" Target="_blank"
HeaderText="Description" DataTextField="Description"
SortExpression="Description" />
<asp:BoundField DataField="Date" ReadOnly="true" HeaderText="Date Received"
SortExpression="Date" />
<asp:HyperLinkField DataNavigateUrlFields="Device"
DataNavigateUrlFormatString="AlertsMain.aspx?Device={0}" DataTextField="Device"
HeaderText="Device" NavigateUrl="~/AlertsMain.aspx" SortExpression="Device"
Target="_self" />
<asp:BoundField DataField="AlertType" ReadOnly="true" HeaderText="AlertType"
SortExpression="AlertType" />
<asp:BoundField DataField="Severity" ReadOnly="true" HeaderText="Severity"
SortExpression="Severity" />
<asp:BoundField DataField="Status" ReadOnly="true" HeaderText="Status"
SortExpression="Status" />
<asp:TemplateField HeaderText="Assigned To" SortExpression="AssignedTo">
<EditItemTemplate>
<asp:TextBox ID="AssignedTo" CausesValidation="true" runat="server" ValidationGroup="Grid1Group" Text="<%# SelectedSSO %>"></asp:TextBox>
<asp:RequiredFieldValidator ID="Grid1AssignedToValidator" runat="server" ErrorMessage="You must enter a value for Assigned to!" SetFocusOnError="True" ControlToValidate="AssignedTo" ValidationGroup="Grid1Group" Display="Static" Text="*"></asp:RequiredFieldValidator>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Ticket Number" SortExpression="TicketNumber">
<EditItemTemplate>
<asp:TextBox ID="TicketNumber" CausesValidation="true" runat="server" ValidationGroup="Grid1Group"></asp:TextBox>
<asp:RequiredFieldValidator ID="Grid1TicketNumberValidator" runat="server" ErrorMessage="You must enter a value for Ticket Number!" SetFocusOnError="True" ControlToValidate="TicketNumber" ValidationGroup="Grid1Group" Display="Static" Text="*"></asp:RequiredFieldValidator>
</EditItemTemplate>
</asp:TemplateField>
</Columns>
<EmptyDataTemplate>
No Matching records were found
</EmptyDataTemplate>
<FooterStyle Font-Names="Arial" Font-Size="X-Small" />
<PagerStyle Font-Names="Arial" Font-Size="X-Small" />
<HeaderStyle BorderColor="Black" BorderStyle="Solid" BorderWidth="1px"
Font-Names="Arial" Font-Size="X-Small" />
<AlternatingRowStyle BorderColor="Black"
BorderStyle="Solid" BorderWidth="1px" Font-Names="Arial"
Font-Size="X-Small" CssClass="alt" ForeColor="Black" />
</asp:GridView>
这是我在row_updated事件中尝试的内容
Here's what i tried in the row_updated event
Protected Sub Gridview1_RowUpdated(ByVal sender As Object, ByVal e As GridViewUpdatedEventArgs)
'Refresh the page after any row updates
If e.Exception Is Nothing Then
If e.AffectedRows = 1 Then
Dim keyFieldValue As String = e.Keys("AlertID").ToString
MsgBox("Record " & keyFieldValue & " updated successfully. ")
MsgBox("AssignedTo= " & e.NewValues(0).ToString)
Else
MsgBox("An error occured during the updated operation")
e.KeepInEditMode = True
End If
Else
MsgBox(e.Exception.Message)
e.ExceptionHandled = True
e.KeepInEditMode = True
End If
'refreshMyParent()
End Sub
我得到索引超出范围的错误我尝试以下一行
MsgBox(AssignedTo =& e.NewValues(0).ToString)
所以看起来因为某些原因新的值没有通过我是否尝试获取新值row_updating或row_updated事件。有关我可能需要查看的内容的任何建议?
I get the error that index is out of range where i try the following line
MsgBox("AssignedTo= " & e.NewValues(0).ToString)
so it looks like the new values aren't getting passed for some reason whether i try to get the new value in the row_updating or row_updated events. Any suggestions on what i may need to look at?
您需要设置UpdateParameters
和UpdateCommand
在SqlDataSource
中,以便更新GridView
。
请按照以下文章详细探讨。
1. 使用SqlDataSource(VB)插入,更新和删除数据 [ ^ ]。
2. 在GridView中插入更新编辑删除行记录 [ ^ ]。
3. 如何插入,编辑,更新和删除数据在gridview中使用sqldatasource使用asp.net [ ^ ]。
You need to set theUpdateParameters
andUpdateCommand
inSqlDataSource
in order to update theGridView
.
Follow the below articles to explore in details.
1. Inserting, Updating, and Deleting Data with the SqlDataSource (VB)[^].
2. Insert Update Edit Delete Rows Record In GridView[^].
3. how insert, Edit, Update and delete data in gridview with sqldatasource using asp.net [^].