2012-10-12 72 views
0

我有一個GridView,在模板字段中有一個按鈕。當按鈕被點擊時,我對數據庫進行查詢並動態創建輸入字段(一個文本框和CuteEditor)並設置其「文本「屬性到我的查詢結果。GridView RowCommand觸發非預期功能

在這一點上是動態創建的更新按鈕(我的OnClientClick屬性設置爲功能「更新後」),所以如果用戶改變那些輸入字段內的任何內容,他們可以更新數據庫

的問題是,當用戶點擊GridView TemplateField中的Button時,我的funcion「UpdatePost」在它不應該被觸發時被觸發。

什麼導致UpdatePost過早觸發?

刪除OnClientClick可解決問題(該功能不會過早觸發)。

它像的OnClientClick是由我的按鈕的GridView控件模板內的點擊功能欄

觸發
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 

GRID-VIEW CODE

<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> 

回答

1

<asp:Button />呈現HTML將觸發回發。 OnClientClick屬性用於JavaScript,如果您不取消提交表單的默認操作,那麼您的客戶端事件和回發都將一起發生。您可以使用標準按鈕輸入(<input type="button" />)或嘗試在用於OnClientClick屬性的JavaScript函數中調用e.preventDefault();

編輯: 我認爲這裏的真正的答案是,你要使用的OnClientClick屬性要求它來調用服務器端代碼。這是爲了指定一個JavaScript函數,當按鈕被點擊時調用。 UpdatePost被調用的原因是因爲相同的按鈕自動連線以觸發回發。而且,由於匹配的CommandName在服務器端事件處理程序的條件下該按鈕,它調用UpdatePost與這行代碼:

editButton.OnClientClick = UpdatePost() 

這行代碼不做你的想法。它實際上執行UpdatePost方法並將OnClientClick屬性設置爲返回值UpdatePost

+0

哦哇...現在有道理。謝謝。我將如何爲我的動態創建的按鈕分配點擊功能? – user1698144

+0

@ user1698144您已擁有。你有一個處理GridControl的'RowCommand'的事件處理程序。該網格內的任何''都會觸發回傳。而且,就你而言,你已經爲其中一個按鈕設置了一個「CommandName」,並且實際上正在事件處理程序中檢查它的值。你的代碼可能更多,所以我不能給你一個更好的答案。它看起來像試圖顯示一個編輯器來編輯行,然後使用'UpdatePost'保存這些更改。 – Sumo

+0

我不是指項目模板按鈕。我指的是EditButton按鈕(Dim editButton As New Button) – user1698144

相關問題