我有一个GridView,它在模板字段中有一个按钮。当单击该按钮时,我查询数据库并动态创建,输入字段(文本框和CuteEditor)并将其“text”属性设置为我的查询结果。
此时动态创建更新按钮(我将onClientClick属性设置为函数“Update Post”),因此如果用户更改了这些输入字段中的任何内容,则可以更新数据库
问题是,当用户点击GridView TemplateField中的Button时,我的功能“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>
答案 0 :(得分:1)
<asp:Button />
呈现会触发回发的HTML。 OnClientClick
属性用于JavaScript,如果您不取消提交表单的默认操作,那么您的客户端事件和回发仍将一起发生。使用标准按钮输入(<input type="button" />
)或尝试在用于OnClientClick
属性的JavaScript函数中调用e.preventDefault();
。
修改强>
我认为这里真正的答案是你试图使用OnClientClick
属性来期望它调用服务器端代码。它用于指定单击按钮时要调用的JavaScript函数。调用UpdatePost
的原因是因为相同的按钮会自动连接以触发回发。并且,因为CommandName与该按钮的服务器端事件处理程序中的条件匹配,所以它使用以下代码行调用UpdatePost
:
editButton.OnClientClick = UpdatePost()
这行代码不符合您的想法。它实际上正在执行UpdatePost
方法并将OnClientClick
属性设置为UpdatePost
的返回值。