我有一个GridView,它在模板字段中有一个按钮。单击按钮后,我会对数据库进行查询,并动态创建、输入字段(文本框和CuteEditor),并将其"text"属性设置为查询结果。
在这一点上,一个更新按钮是动态创建的(我将onClientClick属性设置为"update Post"功能),因此,如果用户更改了这些输入字段中的任何内容,他们可以更新数据库
问题是,当用户点击GridView TemplateField中的按钮时,我的函数"UpdatePost"会在不应该触发的时候被触发。
是什么导致UpdatePost过早触发
删除OnClientClick解决了问题(该函数不会过早触发)。
这就像OnClientClick是由GridView模板字段中我的按钮的Click功能触发的
Private Function UpdatePost()
'Find subject and Post Content
Dim myPh As PlaceHolder = plcEditor
Dim EditorContent As CuteEditor.Editor = plcEditor.FindControl("editEditor")
Dim editorSubject As TextBox = plcEditor.FindControl("editorSubject")
Dim hiddenID As HiddenField = plcEditor.FindControl("hiddenID")
Dim connStr As String = ConfigurationManager.ConnectionStrings("oakfratnewsConnectionString").ConnectionString
Dim nCon As New SqlConnection(connStr)
Dim addCon As New SqlConnection(connStr)
Dim addCom As New SqlCommand("UPDATE News SET Subject = @Subject, [Content] = @Content WHERE (ID = @ID)", addCon)
addCom.Parameters.AddWithValue("@Subject", editorSubject.Text)
addCom.Parameters.AddWithValue("@ID", hiddenID.Value)
addCom.Parameters.AddWithValue("@Content", Server.HtmlDecode(EditorContent.Text))
Try
addCon.Open()
addCom.ExecuteNonQuery()
addCon.Close()
Catch ex As Exception
End Try
Return True
End Function
Private Sub GridView1_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles GridView1.RowCommand
If e.CommandName = "editPost" Then
'Remove DataGrid'''''''''
GridView1.Visible = False
'''''''''''''''''''''''''
Dim index As Integer = Convert.ToInt32(e.CommandArgument)
Dim row As GridViewRow = GridView1.Rows(index)
Dim ID As String = GridView1.Rows(index).Cells(0).Text
''''''''''''''''''''''''''''''''''''''''CREATE Controls for Placeholder
Dim editEditor As New CuteEditor.Editor
Dim hiddenID As New HiddenField
hiddenID.ID = "hiddenID"
hiddenID.Value = ID
editEditor.ID = "editEditor"
Dim subjectTXT As New TextBox
subjectTXT.ID = "editorSubject"
Dim br As New Literal
Dim editButton As New Button
Dim sbjLabel As New Label
sbjLabel.Text = "Subject: "
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
editEditor.AutoConfigure = CuteEditor.AutoConfigure.Simple
br.Text = "<br/><br/>"
plcEditor.Controls.Add(hiddenID)
plcEditor.Controls.Add(sbjLabel)
plcEditor.Controls.Add(subjectTXT)
subjectTXT.Width = "100"
subjectTXT.Height = "25"
subjectTXT.CssClass = "editInput"
plcEditor.Controls.Add(br)
plcEditor.Controls.Add(editEditor)
plcEditor.Controls.Add(br)
plcEditor.Controls.Add(br)
plcEditor.Controls.Add(editButton)
editButton.Text = " Submit Changes "
editButton.Height = 40
editButton.Width = 300
editButton.OnClientClick = UpdatePost()
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Dim connStr As String = ConfigurationManager.ConnectionStrings("oakfratnewsConnectionString").ConnectionString
Dim nCon As New SqlConnection(connStr)
Dim addCon As New SqlConnection(connStr)
Dim addCom As New SqlCommand("SELECT * FROM [News] WHERE ([ID] = @ID)", addCon)
addCom.Parameters.AddWithValue("@ID", ID)
Dim results As SqlDataReader
addCon.Open()
results = addCom.ExecuteReader
While results.Read()
Dim editText As String = results.Item("Content")
Dim Subject As String = results.Item("Subject")
editEditor.Text = editText
subjectTXT.Text = Subject
End While
addCon.Close()
End If
End Sub
网格视图代码
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
CellPadding="3" DataKeyNames="ID" DataSourceID="SqlDataSource1"
Width="776px" BackColor="White" BorderColor="#D8D8D8" BorderStyle="None"
BorderWidth="1px">
<Columns>
<asp:BoundField ItemStyle-Width="30" DataField="ID" HeaderText="ID" InsertVisible="False"
ReadOnly="True" SortExpression="ID" />
<asp:BoundField ItemStyle-Width="140" DataField="Subject" HeaderText="Subject"
SortExpression="Subject" />
<asp:BoundField DataField="Date" HeaderText="Date Published"
SortExpression="Date" />
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="grdEdit" height="70" Width="200" runat="server" CommandName="editPost" CommandArgument="<%# CType(Container,GridViewRow).RowIndex %>" Text="Edit Post" />
<asp:Button ID="Button2" height="70" Width="200" runat="server" CommandArgument="<%# CType(Container,GridViewRow).RowIndex %>" Text="Delete Post" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="White" ForeColor="Red" />
<HeaderStyle BackColor="Green" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" />
<RowStyle ForeColor="#000066" />
<SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#F1F1F1" />
<SortedAscendingHeaderStyle BackColor="#007DBB" />
<SortedDescendingCellStyle BackColor="#CAC9C9" />
<SortedDescendingHeaderStyle BackColor="#00547E" />
</asp:GridView>
<asp:Button />
呈现将触发回发的HTML。OnClientClick
属性用于JavaScript,如果不取消提交表单的默认操作,则客户端事件和回发仍将同时发生。使用标准按钮输入(<input type="button" />
),或者尝试在用于OnClientClick
属性的JavaScript函数中调用e.preventDefault();
。
编辑:我认为这里真正的答案是,您正在尝试使用OnClientClick
属性,期望它调用服务器端代码。它用于指定单击按钮时要调用的JavaScript函数。之所以调用UpdatePost
,是因为同一个按钮会自动连接以触发回发。而且,因为CommandName与该按钮的服务器端事件处理程序中的条件相匹配,所以它使用以下代码行调用UpdatePost
:
editButton.OnClientClick = UpdatePost()
那一行代码并没有达到你所想的效果。它实际上是在执行UpdatePost
方法,并将OnClientClick
属性设置为UpdatePost
的返回值。