Contents
Licensing
Installation and deployment
RadControls for ASP.NET AJAX Fundamentals
RadControls
RadAjax
RadAsyncUpload
RadBarcode
RadButton
RadCalendar
RadCaptcha
RadChart
RadColorPicker
RadComboBox
RadDataPager
RadDock
RadEditor
RadEditor at a glance
Changes and Backwards Compatibility
Standards Compliance
Creating Accessible Content
Getting / Setting Content
Handling Content
Editor Views and Modes
Buttons
DropDowns
Dialogs
Keyboard Shortcuts
Context Menus
System Modules
Appearance
Localization
Spellchecker
Client-side API Reference
Server-side API Reference
Troubleshooting
RadFileExplorer
RadFilter
RadFormDecorator
RadGrid
RadHtmlChart
RadImageEditor
RadInput
RadListBox
RadListView
RadMenu
RadNotification
RadODataDataSource
RadOrgChart
RadPanelBar
RadRating
RadRibbonBar
RadRotator
RadScheduler
RadScriptManager
RadSitemap
RadSlider
RadSocialShare
RadSpell
RadSplitter
RadStylesheetManager
RadTabStrip
RadTagCloud
RadToolBar
RadToolTip
RadTreeList
RadTreeView
RadUpload
RadWindow
RadXmlHttpPanel
Visual Studio Extensions
Integrating RadControls in ASPNET MVC
Integrating RadControls in DNN
Integrating RadControls in Mono
Integrating RadControls in SharePoint
API Reference
For More Help
|
|
        RadControls for ASP.NET AJAX The example below demonstrates displaying database content to a RadEditor and saving changes to the database. Configure the Web Application for RadEditor
In a new AJAX Enabled Web Application: Add Data File
Locate the Access database file "email.mdb" in the \<RadControls for ASP.NET AJAX installation folder>\Live Demos\App_Data.
In the Solution Explorer, copy the "email.mdb" file to the project's App_Data folder.
Configure Controls in Designer
Add a RadComboBox from the ToolBox to the web page. Set the RadComboBox properties:
AutoPostBack = True Skin = Vista AllowCustomText = True
Add a Standard Button control from the ToolBox to the web page. Set the Button ID property to "btnNew". Set the Text property to "New".
Add a Standard Button control from the ToolBox to the web page. Set the Button ID property to "btnSave". Set the Text property to "Save".
Open the RadEditor Smart Tag and set the the Skin to Vista from the drop down list.
Configure Editor Tools
In the Property Window, click the RadEditor Tools property ellipses. This step will open the EditorToolGroup Collection Editor dialog.
In the EditorToolGroup Collection Editor dialog, click the Add button. This will add an EditorToolGroup item to the collection.
Click the EditorToolGroup item Tools property ellipses. This step will open the EditorTool Collection Editor dialog.
Click the EditorTool Collection Add button. In the properties for the EditorTool, locate the Name property, click the drop down arrow and select "Bold" from the list.
Click the EditorTool Collection Add button. In the properties for the EditorTool, locate the Name property, click the drop down arrow and select "Italic" from the list.
Click the EditorTool Collection Add button. In the properties for the EditorTool, locate the Name property, click the drop down arrow and select "Underline" from the list.
Click the OK button to close the EditorTool Collection Editor dialog.
Click the OK button to close the EditorToolGroup Collection Editor dialog.
The ASP.NET markup for the steps so far should look something like the example below: CopyASPX <telerik:RadComboBox
ID="RadComboBox1"
runat="server"
AutoPostBack="True"
OnSelectedIndexChanged="RadComboBox1_SelectedIndexChanged"
Skin="Vista"
AllowCustomText="True">
</telerik:RadComboBox>
<asp:Button ID="btnNew" runat="server" OnClick="btnNew_Click" Text="New" />
<asp:Button ID="btnSave" runat="server" Text="Save" OnClick="btnSave_Click" /><br />
<telerik:RadEditor ID="RadEditor1" runat="server" Skin="Vista" Width="500px" Height="400px">
<Tools>
<telerik:EditorToolGroup>
<telerik:EditorTool Name="Bold" />
<telerik:EditorTool Name="Italic" />
<telerik:EditorTool Name="Underline" />
</telerik:EditorToolGroup>
</Tools>
</telerik:RadEditor>
Handle Events in Code-Behind
In the designer, double-click the surface of the web page to navigate to the Page_Load event handler. Replace the event handler with the following code:
CopyC# protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ReadAllEmail();
}
} CopyVB.NET Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
If Not IsPostBack Then
ReadAllEmail()
End If
End Sub
Add a series of helper methods to create a connection to the Access database, update, insert and read records:
CopyC#
private OleDbConnection CreateConnection()
{
OleDbConnection connection = new OleDbConnection();
connection.ConnectionString =
"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" +
Request.MapPath("\\App_Data\\email.mdb") + ";User ID=;Password=;";
connection.Open();
return connection;
}
private void ReadAllEmail()
{
OleDbCommand command = new OleDbCommand("SELECT ID, Subject FROM email", CreateConnection());
RadComboBox1.DataSource = command.ExecuteReader();
RadComboBox1.DataTextField = "Subject";
RadComboBox1.DataValueField = "ID";
RadComboBox1.DataBind();
}
private string ReadEmail(string id)
{
OleDbCommand command = new OleDbCommand("SELECT Body from Email where ID = @id", CreateConnection());
command.Parameters.AddWithValue("id", id);
OleDbDataReader reader = command.ExecuteReader();
if (reader.Read())
{
return reader.GetString(0);
}
return "";
}
private void InsertEmail(string subject, string body)
{
OleDbCommand command = new OleDbCommand("INSERT INTO Email (Subject, Body) VALUES (@subject, @body)", CreateConnection());
command.Parameters.AddWithValue("subject", RadComboBox1.Text);
command.Parameters.AddWithValue("body", RadEditor1.Content);
command.ExecuteNonQuery();
connection.Close();
}
private void UpdateEmail(int id, string subject, string body)
{
OleDbCommand command = new OleDbCommand("UPDATE Email SET Subject = @subject, Body = @body WHERE ID = @id", CreateConnection());
command.Parameters.AddWithValue("subject", subject);
command.Parameters.AddWithValue("body", body);
command.Parameters.AddWithValue("id", id);
command.ExecuteNonQuery();
connection.Close();
} CopyVB.NET
Private Function CreateConnection() As OleDbConnection
Dim connection As New OleDbConnection()
connection.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Request.MapPath("\App_Data\email.mdb") + ";User ID=;Password=;"
connection.Open()
Return connection
End Function
Private Sub ReadAllEmail()
Dim command As New OleDbCommand("SELECT ID, Subject FROM email", CreateConnection())
RadComboBox1.DataSource = command.ExecuteReader()
RadComboBox1.DataTextField = "Subject"
RadComboBox1.DataValueField = "ID"
RadComboBox1.DataBind()
End Sub
Private Function ReadEmail(ByVal id As String) As String
Dim command As New OleDbCommand("SELECT Body from Email where ID = @id", CreateConnection())
command.Parameters.AddWithValue("id", id)
Dim reader As OleDbDataReader = command.ExecuteReader()
If reader.Read() Then
Return reader.GetString(0)
End If
Return ""
End Function
Private Sub InsertEmail(ByVal subject As String, ByVal body As String)
Dim command As New OleDbCommand("INSERT INTO Email (Subject, Body) VALUES (@subject, @body)", CreateConnection())
command.Parameters.AddWithValue("subject", RadComboBox1.Text)
command.Parameters.AddWithValue("body", RadEditor1.Content)
command.ExecuteNonQuery()
connection.Close()
End Sub
Private Sub UpdateEmail(ByVal id As Integer, ByVal subject As String, ByVal body As String)
Dim command As New OleDbCommand("UPDATE Email SET Subject = @subject, Body = @body WHERE ID = @id", CreateConnection())
command.Parameters.AddWithValue("subject", subject)
command.Parameters.AddWithValue("body", body)
command.Parameters.AddWithValue("id", id)
command.ExecuteNonQuery()
connection.Close()
End Sub
In the designer, double-click the RadComboBox control to create a SelectedIndexChanged event handler. Replace the event handler with the code below. In this snippet, the SelectedValue contains the ID for the selected record.
CopyC# protected void RadComboBox1_SelectedIndexChanged(object o, Telerik.Web.UI.RadComboBoxSelectedIndexChangedEventArgs e)
{
RadEditor1.Content = ReadEmail((o as RadComboBox).SelectedValue);
} CopyVB.NET Protected Sub RadComboBox1_SelectedIndexChanged(ByVal o As Object, ByVal e As Telerik.Web.UI.RadComboBoxSelectedIndexChangedEventArgs) _
Handles RadComboBox1.SelectedIndexChanged
RadEditor1.Content = ReadEmail((TryCast(o, RadComboBox)).SelectedValue)
End Sub
In the designer, double-click the "btnNew" Button control to create a Click event handler. Replace the event handler with the code below.
CopyC# protected void btnNew_Click(object sender, EventArgs e)
{
RadComboBox1.SelectedIndex = -1;
RadEditor1.Content = "";
} CopyVB.NET Protected Sub btnNew_Click(ByVal sender As Object, ByVal e As EventArgs) _
Handles btnNew.Click
RadComboBox1.SelectedIndex = -1
RadEditor1.Content = ""
End Sub
In the designer, double-click the "btnSave" Button control to create a Click event handler. Replace the event handler with the code below.
CopyC# protected void btnSave_Click(object sender, EventArgs e)
{
if (RadComboBox1.SelectedIndex == -1)
InsertEmail(RadComboBox1.Text, RadEditor1.Content);
else
UpdateEmail(
Convert.ToInt32(RadComboBox1.SelectedValue),
RadComboBox1.Text,
RadEditor1.Content);
ReadAllEmail();
} CopyVB.NET Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As EventArgs) _
Handles btnSave.Click
If RadComboBox1.SelectedIndex = -1 Then
InsertEmail(RadComboBox1.Text, RadEditor1.Content)
Else
UpdateEmail(Convert.ToInt32(RadComboBox1.SelectedValue), RadComboBox1.Text, RadEditor1.Content)
End If
ReadAllEmail()
End Sub
Press F5 to run the application. As you change selections in the RadComboBox list, the content of the RadEditor should change in unison. Make changes to content and click the save button. Navigate to and from the edited record to verify that the content has been saved to the database.
See Also
|