我有一个 GridView,它在模板字段中有一个按钮。单击该按钮时,我对数据库进行查询并动态创建、输入字段(文本框和 CuteEditor)并将它们的“文本”属性设置为我的结果询问。
此时会动态创建一个更新按钮(我将 onClientClick 属性设置为函数“更新帖子”),因此如果用户更改这些输入字段中的任何内容,他们可以更新数据库
问题是,当用户单击 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>