Contents
Licensing
Installation and deployment
RadControls for ASP.NET AJAX Fundamentals
RadControls
RadAjax
RadAsyncUpload
RadAutoCompleteBox
RadBarcode
RadButton
RadCalendar
RadCaptcha
RadChart
RadColorPicker
RadComboBox
RadDataPager
RadDock
RadDropDownList
RadDropDownTree
RadEditor
RadFileExplorer
RadFilter
RadFormDecorator
RadGauge
RadGrid
Getting Started
Design Time
ASP.NET 3.5 Features
Columns
Rows
Defining Structure
Data Binding
Sorting
Paging
Scrolling
Grouping
Filtering
Selecting
Insert/Update/Delete
Hierarchical Grid Types and Load Modes
Exporting
Layout
Visible/Enabled Conventions
Ajaxified RadGrid
Inheritance
Control Lifecycle
Performance
Appearance and Styling
Accessibility and Internationalization
How To
Server-Side Programming
Client-Side Programming
Application Scenarios
Troubleshooting
RadHtmlChart
RadImageEditor
RadInput
RadListBox
RadListView
RadMenu
RadNotification
RadODataDataSource
RadOrgChart
RadPanelBar
RadPersistenceFramework
RadPivotGrid
RadProgressArea
RadRating
RadRibbonBar
RadRotator
RadScheduler
RadScriptManager
RadSearchBox
RadSitemap
RadSlider
RadSocialShare
RadSpell
RadSplitter
RadStylesheetManager
RadTabStrip
RadTagCloud
RadTileList
RadToolBar
RadToolTip
RadTreeList
RadTreeView
RadUpload
RadWindow
RadXmlHttpPanel
RadZipLibrary
Visual StyleBuilder
Visual Studio Extensions
Integrating RadControls in ASPNET MVC
Integrating RadControls in DNN
Testing with Test Studio
Integrating RadControls in Mono
Integrating RadControls in SharePoint
API Reference
For More Help
|
|
        RadControls for ASP.NET AJAX
There are numerous situations in which you may need to get reference to controls in grid
row/edit form and modify their state depending on the option the user chooses for another control
in the same row/edit form. Basically, you can reference the grid item which wraps the controls
(either data item or edit form item), then locate the other control and modify its state as per
your custom needs. In some cases (when there are additional containers in the rows), you may need
to find the container first and then propagate the same steps (depicted above).
The upcoming code sample represents how to alter the color for the City column on checkbox state change and how to load different set of items for combo in par with the user selection in another combo. Please note that the items refresh action is not included:
CopyASPX <telerik:RadGrid ID="RadGrid1" runat="server" Width="400px">
<MasterTableView AutoGenerateColumns="false">
<Columns>
<telerik:GridTemplateColumn UniqueName="TemplateColumn" HeaderText="Check/UnCheck">
<ItemTemplate>
<asp:Panel ID="Panel1" runat="server">
<asp:CheckBox ID="CheckBox1" runat="server" AutoPostBack="true" OnCheckedChanged="CheckedChanged" />
</asp:Panel>
</ItemTemplate>
</telerik:GridTemplateColumn>
<telerik:GridDropDownColumn UniqueName="ddlContactName" ListTextField="ContactName"
ListValueField="CustomerID" ListDataMember="Customers" DataField="CustomerID" HeaderText="Contact name" />
<telerik:GridBoundColumn UniqueName="City" DataField="City" HeaderText="City" />
<telerik:GridDropDownColumn UniqueName="ddlContactTitle" ListTextField="ContactTitle"
ListValueField="CustomerID" ListDataMember="Customers" DataField="CustomerID" HeaderText="Contact title" />
<telerik:GridEditCommandColumn UniqueName="EditCommandColumn" />
</Columns>
</MasterTableView></telerik:RadGrid> CopyC# private void RadGrid1_NeedDataSource(object source, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
{
OleDbConnection MyOleDbConnection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + Server.MapPath("~/Grid/Data/Access/Nwind.mdb"));
OleDbDataAdapter MyOleDbDataAdapter = new OleDbDataAdapter();
MyOleDbDataAdapter.SelectCommand = new OleDbCommand("SELECT TOP 5 CustomerID, ContactName, ContactTitle, City FROM Customers", MyOleDbConnection);
DataTable myDataTable = new DataTable();
MyOleDbConnection.Open();
try
{
MyOleDbDataAdapter.Fill(myDataTable);
}
finally
{
MyOleDbConnection.Close();
}
RadGrid1.DataSource = myDataTable.DefaultView;
}
private void RadGrid1_ItemCreated(object sender, Telerik.Web.UI.GridItemEventArgs e)
{
if (e.Item is GridEditableItem && e.Item.IsInEditMode)
{
DropDownList list = (e.Item as GridEditableItem)["ddlContactName"].Controls[0] as DropDownList;
list.AutoPostBack = true;
list.SelectedIndexChanged += new System.EventHandler(this.list_SelectedIndexChanged);
}
}
private void list_SelectedIndexChanged(object sender, System.EventArgs e)
{
GridEditableItem editedItem = (sender as DropDownList).NamingContainer as GridEditableItem;
DropDownList ddList = editedItem["ddlContactTitle"].Controls[0] as DropDownList;
DataTable table = DataSourceHelperCS.GetDataTable("SELECT CustomerID, ContactTitle FROM Customers WHERE ContactName = '" + (editedItem["ddlContactName"].Controls[0] as DropDownList).SelectedItem.Text + "'");
ddList.DataSource = table;
ddList.DataBind();
RadGrid1.Controls.Add(new LiteralControl("<b>the available options for Contact Title has been changed</b>"));
}
protected void CheckedChanged(object sender, System.EventArgs e)
{
CheckBox chkBox = (sender as CheckBox);
Panel myPanel = chkBox.Parent as Panel;
GridDataItem dataItem = myPanel.NamingContainer as GridDataItem;
if (chkBox.Checked)
{
dataItem["City"].Style["color"] = "red";
}
else
{
dataItem["City"].Style["color"] = "black";
}
} CopyVB.NET Private Sub RadGrid1_NeedDataSource(ByVal source As Object, ByVal e As Telerik.Web.UI.GridNeedDataSourceEventArgs) Handles RadGrid1.NeedDataSource
Dim MyOleDbConnection As OleDbConnection = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + Server.MapPath
("~/Grid/Data/Access/Nwind.mdb"))
Dim MyOleDbDataAdapter As OleDbDataAdapter = New OleDbDataAdapter
MyOleDbDataAdapter.SelectCommand = New OleDbCommand("SELECT TOP 5 CustomerID, ContactName, ContactTitle, City FROM Customers",
MyOleDbConnection)
Dim myDataTable As DataTable = New DataTable
MyOleDbConnection.Open()
Try
MyOleDbDataAdapter.Fill(myDataTable)
Finally
MyOleDbConnection.Close()
End Try
RadGrid1.DataSource = myDataTable.DefaultView
End Sub
Private Sub RadGrid1_ItemCreated(ByVal sender As Object, ByVal e As Telerik.Web.UI.GridItemEventArgs) Handles RadGrid1.ItemCreated
If (TypeOf e.Item Is GridEditableItem AndAlso e.Item.IsInEditMode) Then
Dim list As DropDownList = CType(CType(e.Item, GridEditableItem)("ddlContactName").Controls(0), DropDownList)
list.AutoPostBack = True
AddHandler list.SelectedIndexChanged, AddressOf Me.list_SelectedIndexChanged
End If
End Sub
Private Sub list_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
Dim editedItem As GridEditableItem = CType(CType(sender, DropDownList).NamingContainer, GridEditableItem)
Dim ddList As DropDownList = CType(editedItem("ddlContactTitle").Controls(0), DropDownList)
Dim table As DataTable = DataSourceHelperVB.GetDataTable( & CType(editedItem("ddlContactName").Controls(0), DropDownList).SelectedItem.Text & )
ddList.ClearSelection()
ddList.DataSource = table
ddList.DataBind()
RadGrid1.Controls.Add(New LiteralControl("<b>the available options for Contact Title has been changed</b>"))
End Sub
Protected Sub CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs)
Dim chkBox As CheckBox = CType(sender, CheckBox)
Dim myPanel As Panel = CType(chkBox.Parent, Panel)
Dim dataItem As GridDataItem = CType(myPanel.NamingContainer, GridDataItem)
If chkBox.Checked Then
dataItem("City").Style("color") = "red"
Else
dataItem("City").Style("color") = "black"
End If
End Sub
Live example is available here.
|