This is a migrated thread and some comments may be shown as answers.

Updating RadEditor from Gridview

3 Answers 91 Views
Editor
This is a migrated thread and some comments may be shown as answers.
Paul Rickard
Top achievements
Rank 1
Paul Rickard asked on 07 Oct 2010, 08:40 PM

Hi,

I'm currently using the RadEditor to update html content added into a Gridview. I modified the sample code but it doesn't work the same? How it works is the person can add their content, visually with all the styles of bold, red text, etc. Then submit that to the database. If they want to edit that html page, they just go to the Gridview, click edit, and the content from the grid will populate the editor where they can edit it, again visually, and then resubmit overwriting the content that's there. My problem is I can submit to the database but I can't pull it back out meaning edit doesn't work nor does delete? Here's the code:

Imports System
Imports System.Data
Imports System.Configuration
Imports System.Collections
Imports System.Web
Imports System.Web.Security
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports System.Web.UI.HtmlControls
Imports Telerik.Web.UI
Imports System.Data.SqlClient
Imports System.IO

Partial Class _Default
    Inherits System.Web.UI.Page

 

    ' Important!!! The method is called AFTER Page_Load method is executed, so make sure you do not
    ' inadvertently overwrite the editor content there before you process it here!
    Public Sub btnSubmit_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnSubmit.Click

        Dim connection As SqlConnection = CreateConnection()
        Dim command As SqlCommand = Nothing

        If EditedNews.Value <> String.Empty Then
            command = New SqlCommand("UPDATE ElectPageEditor SET NewsDate = GETDATE(), NewsText = @NewsText WHERE NewsID = @NewsID", connection)
            command.Parameters.AddWithValue("@NewsText", NewsEditor.Content)
            command.Parameters.AddWithValue("@NewsID", Convert.ToInt32(EditedNews.Value))
        Else
            command = New SqlCommand("INSERT INTO ElectPageEditor (NewsDate, NewsText) VALUES (GETDATE(), @NewsText)", connection)
            command.Parameters.AddWithValue("@NewsText", NewsEditor.Content)
        End If
        Try
            connection.Open()
            command.ExecuteNonQuery()
        Catch ex As Exception
            lblError.Text = "Error in submit form for " & ex.Message
        Finally
            connection.Close()
        End Try

        Me.Response.Redirect(Me.Request.Url.PathAndQuery)
    End Sub

    Private Function CreateConnection() As SqlConnection
        Dim _connectionString As String = ConfigurationManager.ConnectionStrings("VRASConnectionString").ConnectionString
        Return New SqlConnection(_connectionString)
    End Function

    Protected Sub ReadAllRecords()
        Dim connection As SqlConnection = CreateConnection()
        Try
            connection.Open()
            Dim command2 As New SqlCommand("SELECT NewsID, NewsDate, NewsText FROM ElectPageEditor", connection)
            NewsGrid.DataSource = command2.ExecuteReader()
            NewsGrid.DataBind()
        Catch ex As Exception
            lblError.Text = "Error in application " & ex.Message
        Finally
            connection.Close()
        End Try
    End Sub

    Protected Sub NewsGrid_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles NewsGrid.RowCommand
        Dim connection As SqlConnection = CreateConnection()
        Dim row As GridViewRow = TryCast(NewsGrid.Rows(e.CommandArgument), GridViewRow)
        NewsGrid.DataKeys(row.RowIndex).Value.ToString()
        If e.CommandName = "Delete" Then
            Dim com As New SqlCommand("DELETE FROM ElectPageEditor WHERE NewsID = @NewsID", connection)

            com.Parameters.AddWithValue("@NewsID", row.Cells(0).Text)
            Try
                connection.Open()
                com.ExecuteNonQuery()
            Catch ex As Exception
                lblError.Text = "Error running command. " & ex.Message
            Finally
                connection.Close()
            End Try
        ElseIf e.CommandName = "Edit" Then
            Dim command As New SqlCommand("SELECT NewsText FROM ElectPageEditor WHERE NewsID = @NewsID", connection)
            command.Parameters.AddWithValue("@NewsID", row.Cells(0).Text)

            Try
                connection.Open()
                Dim record As SqlDataReader = command.ExecuteReader(CommandBehavior.CloseConnection)
                If record.Read() Then
                    NewsEditor.Content = record.GetString(0)
                    EditedNews.Value = row.Cells(0).Text
                Else
                    NewsEditor.Content = ""
                    EditedNews.Value = ""
                End If

                ' Will close the connection as well
                record.Close()
            Catch ex As Exception
                lblError.Text = "Error modifying information. " & ex.Message
            Finally
                connection.Close()

            End Try
        End If
        ' Add code to delete row from data source.
        ReadAllRecords()
    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not Page.IsPostBack Then
            ReadAllRecords()
        End If

    End Sub
End Class

3 Answers, 1 is accepted

Sort by
0
Paul Rickard
Top achievements
Rank 1
answered on 08 Oct 2010, 05:02 PM
Anybody have an idea???
0
Dobromir
Telerik team
answered on 11 Oct 2010, 11:50 AM
Hi Paul,

I already answered your support ticket on the subject. For convenience I will paste my answer here as well.

This problem is not directly related to RadEditor but a is a general ASP.NET knowledge. When the CommandName property of a ButtonField is set to a Edit or Delete the GridView is firing the corresponding event and you need to register an event handler for this event.

In this particular scenario you can either, separate the RowCommand's event handler to RowEditing's and RowDeleting's event handlers, or just register an empty handlers.

Also, when a bound field is invisible (Visibility="false") its data is not populated. You can find more information on the subject in this ASP.NET Forum's thread. This is the reason why the editor does not load the cell content.


Best wishes,
Dobromir
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Paul Rickard
Top achievements
Rank 1
answered on 12 Oct 2010, 07:22 PM
That worked, excellent!
Tags
Editor
Asked by
Paul Rickard
Top achievements
Rank 1
Answers by
Paul Rickard
Top achievements
Rank 1
Dobromir
Telerik team
Share this question
or