Insert in Grid with Tooltip
This online example demonstrates integration between RadGrid, RadToolTip and RadAjax. The example shows how to insert several new records in the grid with a single button click using Ajax requests.
The control, containing the fields for the new records is added to the content of the RadToolTip when the OnAjaxUpdate event of the RadToolTipManager fires. Once the user hits the [Add Products] button, the new records are inserted into the datatable, the active tooltip is closed and the the appropriate nested table is rebound.
As you cannot display RadAjax loading panel when you are using the RadToolTipManager in this case and generate dynamically the tooltip content, you can display a separate RadAjaxLoadingPanel when you are making Ajax requests.
In order to see the new records, you have to expand the corresponding item in the MasterTableView.
Below are the code snippets from the demo referred above:
<asp:ScriptManager ID="ScriptManager1" runat="server" />
<telerik:RadAjaxLoadingPanel ID="RadAjaxLoadingPanel1" runat="server" />
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<telerik:RadGrid RenderMode="Lightweight" ID="RadGrid1" runat="server" DataSourceID="SqlDataSource1" Width="700px"
AutoGenerateColumns="False" PageSize="10" AllowSorting="True" AllowPaging="True"
GridLines="None" ShowStatusBar="true" OnItemDataBound="RadGrid1_ItemDataBound"
OnDataBound="RadGrid1_DataBound">
<PagerStyle Mode="NumericPages"></PagerStyle>
<MasterTableView DataSourceID="SqlDataSource1" DataKeyNames="SupplierID" AllowMultiColumnSorting="True"
Width="100%" HierarchyLoadMode="ServerOnDemand">
<DetailTables>
<telerik:GridTableView DataKeyNames="ProductID" DataSourceID="SqlDataSource2"
Width="100%" runat="server">
<ParentTableRelation>
<telerik:GridRelationFields DetailKeyField="SupplierID" MasterKeyField="SupplierID" />
</ParentTableRelation>
<Columns>
<telerik:GridBoundColumn SortExpression="ProductName" HeaderText="Product name" HeaderButtonType="TextButton"
DataField="ProductName" UniqueName="ProductName">
</telerik:GridBoundColumn>
<telerik:GridBoundColumn SortExpression="QuantityPerUnit" HeaderText="Quantity per unit"
HeaderButtonType="TextButton" DataField="QuantityPerUnit" UniqueName="QuantityPerUnit">
</telerik:GridBoundColumn>
<telerik:GridBoundColumn SortExpression="UnitsInStock" HeaderText="Units in stock"
HeaderButtonType="TextButton" DataField="UnitsInStock" UniqueName="UnitsInStock">
</telerik:GridBoundColumn>
<telerik:GridBoundColumn SortExpression="UnitsOnOrder" HeaderText="Units on order"
HeaderButtonType="TextButton" DataField="UnitsOnOrder" UniqueName="UnitsOnOrder">
</telerik:GridBoundColumn>
</Columns>
<SortExpressions>
<telerik:GridSortExpression FieldName="ProductName"></telerik:GridSortExpression>
</SortExpressions>
<ExpandCollapseColumn Visible="False">
<HeaderStyle Width="19px" />
</ExpandCollapseColumn>
<RowIndicatorColumn Visible="False">
<HeaderStyle Width="20px" />
</RowIndicatorColumn>
<EditFormSettings>
<EditColumn UniqueName="EditCommandColumn1">
</EditColumn>
</EditFormSettings>
</telerik:GridTableView>
</DetailTables>
<Columns>
<telerik:GridBoundColumn DataField="SupplierID" DataType="System.Int32" HeaderText="SupplierID"
SortExpression="SupplierID" UniqueName="SupplierID">
</telerik:GridBoundColumn>
<telerik:GridBoundColumn DataField="CompanyName" HeaderText="CompanyName" SortExpression="CompanyName"
UniqueName="CompanyName">
</telerik:GridBoundColumn>
<telerik:GridBoundColumn DataField="ContactName" HeaderText="ContactName" SortExpression="ContactName"
UniqueName="ContactName">
</telerik:GridBoundColumn>
<telerik:GridBoundColumn DataField="ContactTitle" HeaderText="ContactTitle" SortExpression="ContactTitle"
UniqueName="ContactTitle">
</telerik:GridBoundColumn>
<telerik:GridBoundColumn DataField="Phone" HeaderText="Phone" SortExpression="Phone"
UniqueName="Phone">
</telerik:GridBoundColumn>
<telerik:GridTemplateColumn UniqueName="AddProducts">
<ItemTemplate>
<asp:HyperLink runat="server" NavigateUrl="#" onclick="return false;" Text="Add Products to Supplier"
ID="hlAddProducts">
</asp:HyperLink>
</ItemTemplate>
</telerik:GridTemplateColumn>
</Columns>
<SortExpressions>
<telerik:GridSortExpression FieldName="CompanyName"></telerik:GridSortExpression>
</SortExpressions>
<ExpandCollapseColumn>
<HeaderStyle Width="19px" />
</ExpandCollapseColumn>
<RowIndicatorColumn Visible="False">
<HeaderStyle Width="20px" />
</RowIndicatorColumn>
</MasterTableView>
</telerik:RadGrid>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
ProviderName="System.Data.SqlClient" SelectCommand="SELECT * FROM Suppliers">
</asp:SqlDataSource>
<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
ProviderName="System.Data.SqlClient" SelectCommand="SELECT * FROM Products WHERE (SupplierID = @SupplierID)">
<SelectParameters>
<asp:SessionParameter Name="SupplierID" SessionField="SupplierID" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
<telerik:RadToolTipManager RenderMode="Lightweight" ID="RadToolTipManager1" runat="server" OnAjaxUpdate="RadToolTipManager1_AjaxUpdate"
RelativeTo="Element" Width="225px" Height="390px" style="z-index: 31000" Position="BottomLeft" HideEvent="ManualClose"
Title="Products" ShowEvent="OnClick">
</telerik:RadToolTipManager>
</ContentTemplate>
</asp:UpdatePanel>
<asp:Button ID="btnAddNewProduct" runat="server" Text="Add more" UseSubmitBehavior="false"
OnClick="btnAddNewProduct_Click" /><br />
<br />
<span style="color: Red">You must fill all inputs before adding extra fields or inserting
the product</span>
<br />
<br />
<asp:Panel ID="pNewProducts" runat="server" Width="200px" Style="float: left;">
</asp:Panel>
<div style="clear: both; height: 1px; margin-bottom: -1px;">
<!-- -->
</div>
<br />
<asp:Button ID="btnAddProducts" runat="server" Text="Add products" OnClick="btnAddProducts_Click"
UseSubmitBehavior="false" />
private int _loadedControls = 0;
private bool _isPostBack = false;
protected void Page_Load(object sender, EventArgs e)
{
if (!_isPostBack)
{
AddInitialNewProducts();
}
}
protected void AddInitialNewProducts()
{
this.pNewProducts.Controls.Add(LoadProduct(LoadedControls));
LoadedControls++;
this.pNewProducts.Controls.Add(LoadProduct(LoadedControls));
LoadedControls++;
}
protected void btnAddNewProduct_Click(object sender, EventArgs e)
{
this.pNewProducts.Controls.Add(LoadProduct(LoadedControls));
LoadedControls++;
}
protected void btnAddProducts_Click(object sender, EventArgs e)
{
ControlCollection newProductsFields = this.pNewProducts.Controls;
ArrayList newProducts = new ArrayList();
foreach (Control newProduct in newProductsFields)
{
if ((newProduct is NewProductFormCS) && ((NewProductFormCS)newProduct).ProductName != String.Empty)
{
newProducts.Add(newProduct);
}
}
if (newProducts.Count > 0)
{
foreach (Control newProduct in newProducts)
{
NewProductFormCS product = (NewProductFormCS)newProduct;
SessionDataSource products = (SessionDataSource)Page.FindControl("SessionDataSource2");
if (products != null)
{
IDataSource dataSource = (IDataSource)products;
DataSourceView view = dataSource.GetView("DefaultView");
IOrderedDictionary data = new OrderedDictionary();
data.Add("ProductName", product.ProductName);
data.Add("QuantityPerUnit", product.QuantityPerUnit);
data.Add("UnitsInStock", product.UnitsInStock);
data.Add("UnitsOnOrder", product.UnitsOnOrder);
data.Add("SupplierID", this.ID);
view.Insert(data, new DataSourceViewOperationCallback(OnDataSourceOperationComplete));
}
}
}
//Close the active ToolTip.
ScriptManager.RegisterClientScriptBlock(
this.Page,
this.GetType(),
"WebUserControlSript",
"CloseActiveToolTip('" + this.ID + "')",
true);
}
private bool OnDataSourceOperationComplete(int count, Exception e)
{
if (e != null)
{
throw e;
}
return true;
}
private Control LoadProduct(int index)
{
Control product = Page.LoadControl("NewProductFormCS.ascx");
product.ID = "MyControl" + index.ToString();
return product;
}
protected override void LoadViewState(object savedState)
{
object[] state = (object[])savedState;
LoadedControls = (int)state[0];
string controlID = (string)state[1];
if (controlID == this.ID)
{
for (int i = 0; i < LoadedControls; i++)
{
pNewProducts.Controls.Add(LoadProduct(i));
}
_isPostBack = true;
}
else
{
_loadedControls = 0;
_isPostBack = false;
}
base.LoadViewState(state[2]);
}
protected override object SaveViewState()
{
object[] state = new object[] {
LoadedControls,
this.ID,
base.SaveViewState()
};
return state;
}
public int LoadedControls
{
get
{
return _loadedControls;
}
set
{
_loadedControls = value;
}
}