Hi,
Based on the section Setting predefined values for controls inside user control on item insertion, how can I set predefined values inside a user control on item insertion at the Page Load when we set
Here is the code I have
Also, it works fines if I don't add
Thanks
Based on the section Setting predefined values for controls inside user control on item insertion, how can I set predefined values inside a user control on item insertion at the Page Load when we set
IsItemInserted = true in the PreRender Event ?Here is the code I have
protected void RadGrid_PreRender(object sender, System.EventArgs e) { if (!this.IsPostBack) { if (Request["mode"] != null && Request["mode"] == "add") { RadGrid1.MasterTableView.IsItemInserted = true; } RadGrid1.MasterTableView.Rebind(); } }protected void RadGrid_ItemCommand(object source, GridCommandEventArgs e){ if (e.CommandName == RadGrid.InitInsertCommandName) { if (Request.QueryString["SomeParameter"] != null) { e.Canceled = true; e.Item.OwnerTableView.InsertItem(); GridEditableItem insertedItem = e.Item.OwnerTableView.GetInsertItem(); String MyUserControlId = GridEditFormItem.EditFormUserControlID; UserControl MyUserControl = insertedItem.FindControl(MyUserControlId) as UserControl; HiddenField hidden = MyUserControl.FindControl("SomeControl") as HiddenField; hidden.Value = Request.QueryString["SomeParameter"]; } }}Also, it works fines if I don't add
IsItemInserted = true in the PreRender eventThanks
9 Answers, 1 is accepted
0
Hi pylacroix,
In Page_Load your insert form may not be initialized yet. The earliest point when you have the insert form available is RadGrid's ItemCreated event:
In the above code sample we are accessing the user control in the insert form just when it is initialized.
Veli
the Telerik team
In Page_Load your insert form may not be initialized yet. The earliest point when you have the insert form available is RadGrid's ItemCreated event:
protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e){ if (e.Item is GridEditFormInsertItem && e.Item.IsInEditMode) { GridEditFormInsertItem insertedItem = (GridEditFormInsertItem)e.Item; String MyUserControlId = GridEditFormItem.EditFormUserControlID; UserControl MyUserControl = insertedItem.FindControl(MyUserControlId) as UserControl; HiddenField hidden = MyUserControl.FindControl("SomeControl") as HiddenField; hidden.Value = Request.QueryString["SomeParameter"]; }}In the above code sample we are accessing the user control in the insert form just when it is initialized.
Veli
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
pylacroix
Top achievements
Rank 1
answered on 11 Nov 2010, 05:08 PM
Thanks Veli,
Do I have to cancel the event just as in the RagGrid1_ItemCommand event ?
With the code you provided, I am able to assign the value to my Hidden control on the Page_Load, but once the user control loads, the control has an empty value.
I managed to make it work using the Context
and in my user control
Is there a better and proper way to do it?
Thanks
Do I have to cancel the event just as in the RagGrid1_ItemCommand event ?
With the code you provided, I am able to assign the value to my Hidden control on the Page_Load, but once the user control loads, the control has an empty value.
I managed to make it work using the Context
Context.Items["ContextVar"] = Request.QueryString["SomeParameter"];and in my user control
if (Context.Items["ContextVar"] != null){ // do something with my value}Is there a better and proper way to do it?
Thanks
0
Hi pylacroix,
Can you show us some sample code, so that we can try to understand better what's going on?
Greetings,
Veli
the Telerik team
Can you show us some sample code, so that we can try to understand better what's going on?
Greetings,
Veli
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
pylacroix
Top achievements
Rank 1
answered on 11 Nov 2010, 08:14 PM
as requested here are the files and this occurs on Telerik version 2010.1.519.35. you will notice the Context is used in the RadgridAddOnLoad.aspx.cs
and in ucManufacturerAccountEdit.ascx.cs
so basically, if I remove those instructions, the manufacturer combobox is not predefined with the valued specified by the value of the querystring (ie: RadgridAddOnLoad.aspx?mode=add&intManufacturerId=5). If I leave those instructions then the combobox is predefined.
the files
Data.xml
RadgridAddOnLoad.aspx
RadgridAddOnLoad.aspx.cs
ucManufacturerAccountEdit.ascx
ucManufacturerAccountEdit.ascx.cs
Context.Items["manufID"] = Request.QueryString["intManufacturerId"];and in ucManufacturerAccountEdit.ascx.cs
if (Context.Items["manufID"] != null){ txtManufacturer.SelectedValue = Context.Items["manufID"].ToString();}so basically, if I remove those instructions, the manufacturer combobox is not predefined with the valued specified by the value of the querystring (ie: RadgridAddOnLoad.aspx?mode=add&intManufacturerId=5). If I leave those instructions then the combobox is predefined.
the files
Data.xml
<root> <entry ID="1" BusinessID="1" BusinessName="Business1" ManufacturerID="1" ManufacturerName="Manufacturer1" Description="Description1" Account="" PIN="PIN1" /> <entry ID="2" BusinessID="2" BusinessName="Business2" ManufacturerID="2" ManufacturerName="Manufacturer2" Description="Description2" Account="" PIN="PIN2" /> <entry ID="3" BusinessID="3" BusinessName="Business3" ManufacturerID="3" ManufacturerName="Manufacturer3" Description="Description3" Account="" PIN="PIN3" /> <entry ID="4" BusinessID="4" BusinessName="Business4" ManufacturerID="4" ManufacturerName="Manufacturer4" Description="Description4" Account="" PIN="PIN4" /></root>RadgridAddOnLoad.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="RadgridAddOnLoad.aspx.cs" Inherits="test_RadgridAddOnLoad" %><%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head id="Head1" runat="server"> <title></title></head><body> <form id="form1" runat="server"> <div> <telerik:RadScriptManager ID="RadScriptManager1" runat="server" /> <telerik:RadAjaxManager ID="RadAjaxManager1" runat="server" /> <telerik:RadAjaxManagerProxy ID="RadAjaxManagerProxy1" runat="server"> <AjaxSettings> <telerik:AjaxSetting AjaxControlID="RadAjaxManagerProxy1"> <UpdatedControls> <telerik:AjaxUpdatedControl ControlID="grdStore" LoadingPanelID="RadAjaxLoadingPanel1" /> </UpdatedControls> </telerik:AjaxSetting> <telerik:AjaxSetting AjaxControlID="grdStore"> <UpdatedControls> <telerik:AjaxUpdatedControl ControlID="grdStore" LoadingPanelID="RadAjaxLoadingPanel1" /> </UpdatedControls> </telerik:AjaxSetting> </AjaxSettings> </telerik:RadAjaxManagerProxy> <telerik:RadAjaxLoadingPanel runat="server" ID="RadAjaxLoadingPanel1" Height="150px" Width="150px" /> <telerik:RadScriptBlock ID="RadScriptBlock1" runat="server"> <script type="text/javascript"> function PopUpShowing(sender, eventArgs) { var popUp = eventArgs.get_popUp(); var scrollTop = (typeof window.innerHeight != 'undefined' ? 0 : document.documentElement.scrollTop); var popUpWidth = popUp.style.width.substr(0, popUp.style.width.indexOf("px")); var popUpHeight = popUp.style.height.substr(0, popUp.style.height.indexOf("px")); var windowHeight = (typeof window.innerHeight != 'undefined' ? window.innerHeight : document.body.offsetHeight - scrollTop); var windowWidth = document.body.offsetWidth; if (popUpHeight == "") popUpHeight = 300; // if the height isn't set on the popup, default to 300px popUp.style.position = "fixed"; popUp.style.left = (Math.floor((windowWidth - popUpWidth) / 2)).toString() + "px"; popUp.style.top = (Math.floor((windowHeight - popUpHeight) / 2) + scrollTop).toString() + "px"; } </script> </telerik:RadScriptBlock> <asp:HiddenField ID="txtDealerGUID" runat="server" /> <asp:HiddenField ID="txtAdministrationURL" runat="server" /> <telerik:RadGrid ID="grdStore" runat="server" GridLines="None" AllowPaging="True" AllowSorting="True" AllowFilteringByColumn="true" Width="100%" AutoGenerateColumns="False" OnPreRender="grdStore_PreRender" DataSourceID="XmlDataSource1" OnDeleteCommand="grdStore_DeleteCommand" OnItemDataBound="grdStore_ItemDataBound" OnUpdateCommand="grdStore_UpdateCommand" OnInsertCommand="grdStore_InsertCommand" OnItemCommand="grdStore_ItemCommand" OnItemCreated="grdStore_ItemCreated" AllowMultiRowEdit="False" AllowMultiRowSelection="False"> <ClientSettings> <ClientEvents OnPopUpShowing="PopUpShowing" /> </ClientSettings> <MasterTableView GridLines="None" Width="100%" DataKeyNames="ID" NoMasterRecordsText="<%$ Resources:Resource, lblNoRecordToDisplay%>" CommandItemDisplay="Top" CommandItemSettings-AddNewRecordText="Add" EditFormSettings-PopUpSettings-Modal="true" EditFormSettings-PopUpSettings-Height="200px" EditFormSettings-PopUpSettings-Width="730px" EditFormSettings-PopUpSettings-ZIndex="2999" EditMode="PopUp"> <rowindicatorcolumn visible="False"> <HeaderStyle Width="20px" /> </rowindicatorcolumn> <expandcollapsecolumn resizable="False" visible="False"> <HeaderStyle Width="20px" /> </expandcollapsecolumn> <CommandItemSettings RefreshImageUrl="Refresh.gif" /> <Columns> <telerik:GridBoundColumn SortExpression="BusinessName" HeaderText="<% $ Resources:Resource, lblStore %>" DataField="BusinessName" UniqueName="BusinessName" /> <telerik:GridBoundColumn SortExpression="ManufacturerName" HeaderText="<% $ Resources:Resource, lblManufacturer %>" DataField="ManufacturerName" UniqueName="ManufacturerName" /> <telerik:GridBoundColumn SortExpression="Description" HeaderText="<% $ Resources:Resource, lblDescription %>" DataField="Description" UniqueName="Description" /> <telerik:GridBoundColumn SortExpression="PIN" HeaderText="<% $ Resources:Resource, lblPIN %>" DataField="PIN" UniqueName="PIN" /> <telerik:GridTemplateColumn AllowFiltering="false" UniqueName="Status" HeaderText="<% $ Resources:Resource, lblStatus %>"> <ItemTemplate> <asp:Label ID="lblStatus" runat="server" /> </ItemTemplate> </telerik:GridTemplateColumn> <telerik:GridEditCommandColumn UpdateText="Update" UniqueName="EditCommandColumn" CancelText="Cancel" EditText="<% $ Resources:Resource, lblEdit %>" /> <telerik:GridButtonColumn ConfirmText="<% $ Resources:Resource, lblDeleteConfirmation %>" UniqueName="DeleteColumn" Text="<%$ Resources:Resource, lblDelete%>" CommandName="Delete" /> </Columns> <EditFormSettings UserControlName="~/Controls/ucManufacturerAccountEdit.ascx" EditFormType="WebUserControl" CaptionFormatString="<%$ Resources:Resource, lblAccounts%>" /> <PagerStyle Mode="NextPrev" /> </MasterTableView> </telerik:RadGrid><asp:XmlDataSource ID="XmlDataSource1" runat="server" DataFile="~/test/data.xml" ></asp:XmlDataSource> </div> </form></body></html>RadgridAddOnLoad.aspx.cs
using System;using System.Collections;using System.Configuration;using System.Data;using System.Linq;using System.Web;using System.Web.Security;using System.Web.UI;using System.Web.UI.HtmlControls;using System.Web.UI.WebControls;using System.Web.UI.WebControls.WebParts;using System.Xml.Linq;using Tech2020.Business.BusinessLogicLayer;using Telerik.Web.UI;using System.Collections.Generic;using System.Xml;public partial class test_RadgridAddOnLoad : System.Web.UI.Page{ #region " Events " protected void Page_Load(object sender, EventArgs e) { } protected void Page_PreInit(object sender, EventArgs e) { if (!IsPostBack) { } } protected void grdStore_PreRender(object sender, System.EventArgs e) { if (!this.IsPostBack) { if (Request["mode"] != null && Request["mode"] == "add") { grdStore.MasterTableView.IsItemInserted = true; } grdStore.MasterTableView.Rebind(); } } protected void grdStore_ItemCommand(object source, GridCommandEventArgs e) { switch (e.CommandName) { case RadGrid.EditCommandName: grdStore.MasterTableView.IsItemInserted = false; break; case RadGrid.InitInsertCommandName: grdStore.MasterTableView.ClearEditItems(); if (Request.QueryString["intManufacturerId"] != null) { e.Canceled = true; e.Item.OwnerTableView.InsertItem(); var insertedItem = e.Item.OwnerTableView.GetInsertItem(); var control = insertedItem.FindControl(GridEditFormItem.EditFormUserControlID) as UserControl; ((HiddenField)control.FindControl("ManufacturerID")).Value = Request.QueryString["intManufacturerId"]; } break; } } protected void grdStore_ItemDataBound(object sender, GridItemEventArgs e) { if (e.Item is GridCommandItem) { GridCommandItem item = (GridCommandItem)e.Item; //hide refresh linkbutton ((LinkButton)item.FindControl("RebindGridButton")).Visible = false; } } protected void grdStore_UpdateCommand(object source, GridCommandEventArgs e) { if (e.Item is GridEditableItem) { // update record } } protected void grdStore_InsertCommand(object source, GridCommandEventArgs e) { if (e.Item is GridEditableItem) { // add record } } protected void grdStore_DeleteCommand(object source, GridCommandEventArgs e) { // delete record } protected void grdStore_ItemCreated(object sender, GridItemEventArgs e) { if (e.Item is GridEditFormInsertItem && e.Item.IsInEditMode) { if (Request.QueryString["intManufacturerId"] != null) { var insertedItem = (GridEditFormInsertItem)e.Item; var control = insertedItem.FindControl(GridEditFormItem.EditFormUserControlID) as UserControl; ((HiddenField)control.FindControl("ManufacturerID")).Value = Request.QueryString["intManufacturerId"]; Context.Items["manufID"] = Request.QueryString["intManufacturerId"]; } } } #endregion}ucManufacturerAccountEdit.ascx
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="ucManufacturerAccountEdit.ascx.cs" Inherits="Controls_ucManufacturerAccountEdit" %><%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %><asp:HiddenField ID="ManufacturerID" runat="server" Value='<%# DataBinder.Eval( Container, "DataItem.ManufacturerID" ) %>' /><div style="padding:8px"> <table class="gridUcSingleEdit"> <tr> <td class="label"><asp:Label Text="<% $ Resources:Resource, lblStore %>" runat="server" /></td> <td><telerik:RadComboBox ID="txtBusiness" runat="server" Width="268px" /></td> <td class="label"><asp:Label Text="<% $ Resources:Resource, lblManufacturer %>" runat="server" /></td> <td><telerik:RadComboBox ID="txtManufacturer" runat="server" Width="268px" /></td> </tr> <tr> <td class="label"><asp:Label ID="lblAccount" Text="<% $ Resources:Resource, lblAccount %>" runat="server" /></td> <td><asp:TextBox ID="txtAccount" Text='<%# DataBinder.Eval( Container, "DataItem.Account" ) %>' MaxLength="255" runat="server" Width="264px" /><asp:TextBox ID="txtPIN" Enabled="false" Visible="false" Text='<%# DataBinder.Eval( Container, "DataItem.PIN" ) %>' MaxLength="255" runat="server" Width="264px" /><asp:RequiredFieldValidator Display="Dynamic" ErrorMessage="<%$ Resources:Resource, lblRequired%>" ControlToValidate="txtAccount" runat="server" /></td> <td class="label"><asp:Label Text="<% $ Resources:Resource, lblDescription %>" runat="server" /></td> <td><asp:TextBox ID="txtDescription" TextMode="MultiLine" Text='<%# DataBinder.Eval( Container, "DataItem.Description" ) %>' MaxLength="255" Width="264px" runat="server" /><asp:RequiredFieldValidator Display="Dynamic" ErrorMessage="<%$ Resources:Resource, lblRequired%>" ControlToValidate="txtDescription" runat="server" /></td> </tr> <tr> <td colspan="2" style="padding-left:2px;"><asp:Label CssClass="txtRefused" Text="<% $ Resources:Resource, lblAllFieldsRequired %>" runat="server" /></td> <td colspan="2" align="right" style="padding-right:14px;"> <asp:button id="btnUpdate" text="Update" runat="server" CssClass="button" CommandName="Update" Visible='<%# !(DataItem is Telerik.Web.UI.GridInsertionObject) %>'></asp:button> <asp:button id="btnInsert" text="Insert" runat="server" CssClass="button" CommandName="PerformInsert" Visible='<%# DataItem is Telerik.Web.UI.GridInsertionObject %>'></asp:button> <asp:button id="btnCancel" text="Cancel" runat="server" CssClass="button" causesvalidation="False" commandname="Cancel"></asp:button> </td> </tr> </table></div>ucManufacturerAccountEdit.ascx.cs
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;using Tech2020.Business.BusinessLogicLayer;public partial class Controls_ucManufacturerAccountEdit : System.Web.UI.UserControl{ private Tech2020.Business.BusinessLogicLayer.Manufacturer _manufacturerBLL = new Manufacturer(); private Common _common = new Common(); private Tech2020.Business.BusinessLogicLayer.Business _businessBll = new Tech2020.Business.BusinessLogicLayer.Business(); public object DataItem { get; set; } protected void Page_PreRender(object sender, EventArgs e) { txtManufacturer.DataSource = _manufacturerBLL.Get(); txtManufacturer.DataTextField = "Name"; txtManufacturer.DataValueField = "ID"; txtManufacturer.DataBind(); txtManufacturer.SelectedValue = (ManufacturerID.Value != "" ? ManufacturerID.Value : txtManufacturer.Items[0].Value); if (Context.Items["manufID"] != null) { txtManufacturer.SelectedValue = Context.Items["manufID"].ToString(); } txtBusiness.DataSource = _businessBll.GetDealerStores(Convert.ToInt32(Session["DealerID"]), Convert.ToInt32(Tech2020.Generic.Const.Const.Attribute.Business_Sale_Lead_Email), 0, 0, Convert.ToInt32(Session["LanguageID"])); txtBusiness.DataTextField = "Name"; txtBusiness.DataValueField = "ID"; txtBusiness.DataBind(); }}0
Hi pylacroix,
To reproduce this issue, I put your source code in a sample project and was able to run it. I removed the localization strings, and bound RadComboBox in the insert form to simple array of integer values. I also appended the query string ?intManufacturerId=7 to the URL to test how this value will be set to the combo. The result I am getting is that this seems to work without the help of the Context object. The way you have implemented it, the query string value will be set to the ManufacturerID hidden field right in ItemCommand event handler where you generate a new insert item and find the hidden field in it. (to check that, I have turned the hidden field to a TextBox control to see its value). Attaching the test page, user control and XML file I am using. Seems to work without the Context object too.
Veli
the Telerik team
To reproduce this issue, I put your source code in a sample project and was able to run it. I removed the localization strings, and bound RadComboBox in the insert form to simple array of integer values. I also appended the query string ?intManufacturerId=7 to the URL to test how this value will be set to the combo. The result I am getting is that this seems to work without the help of the Context object. The way you have implemented it, the query string value will be set to the ManufacturerID hidden field right in ItemCommand event handler where you generate a new insert item and find the hidden field in it. (to check that, I have turned the hidden field to a TextBox control to see its value). Attaching the test page, user control and XML file I am using. Seems to work without the Context object too.
Veli
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
pylacroix
Top achievements
Rank 1
answered on 12 Nov 2010, 03:52 PM
Hi Veli,
I took the code from the file testpage.zip, I created a new project, I added a reference to Telerik 2010.1.519, added
to the web.config and when I load the following page http://localhost:49226/test/RadgridAddOnLoad.aspx?mode=add&intManufacturerID=3 manufacturer 3 is not selected and the text field you added is empty either on IE or Firefox.
I took the code from the file testpage.zip, I created a new project, I added a reference to Telerik 2010.1.519, added
<add path="Telerik.Web.UI.WebResource.axd" verb="*" type="Telerik.Web.UI.WebResource, Telerik.Web.UI, Version=2010.1.519, Culture=neutral, PublicKeyToken=121fae78165ba3d4" validate="false"/>to the web.config and when I load the following page http://localhost:49226/test/RadgridAddOnLoad.aspx?mode=add&intManufacturerID=3 manufacturer 3 is not selected and the text field you added is empty either on IE or Firefox.
0
Accepted
Hello pylacroix,
Using the above code in PreRender now successfully fires the ItemCommand event handler in RadGrid, where we get the manufacturer ID and assign it to the textbox in the user control.
Veli
the Telerik team
Yes, it seems when you add mode=add to the query string parameters the value is not set to the combo. But this is because when you have mode=add, you do not fire the ItemCommand event for InitInsert (showing the insert form). This is the code that gets run:
protectedvoidgrdStore_PreRender(objectsender, System.EventArgs e){if(!this.IsPostBack){if(Request["mode"] !=null&& Request["mode"] =="add"){grdStore.MasterTableView.IsItemInserted =true;}grdStore.MasterTableView.Rebind();}}
Note that in the above code sample I only set the master table to be in insert mode and rebind the grid. No command is actually fired on this action. But what I need is the InitInsert command to fire, so that I can use the ItemCommand event handler to use that:
protectedvoidgrdStore_ItemCommand(objectsource, GridCommandEventArgs e){switch(e.CommandName){caseRadGrid.EditCommandName:grdStore.MasterTableView.IsItemInserted =false;break;caseRadGrid.InitInsertCommandName:grdStore.MasterTableView.ClearEditItems();if(Request.QueryString["intManufacturerId"] !=null){e.Canceled =true;e.Item.OwnerTableView.InsertItem();var insertedItem = e.Item.OwnerTableView.GetInsertItem();var control = insertedItem.FindControl(GridEditFormItem.EditFormUserControlID)asUserControl;((TextBox)control.FindControl("ManufacturerID")).Text = Request.QueryString["intManufacturerId"];}break;}}
So, what I need to do when checking for the mode=add query string is to fire the InitInsert command instead:
protectedvoidgrdStore_PreRender(objectsender, System.EventArgs e){if(!this.IsPostBack){if(Request["mode"] !=null&& Request["mode"] =="add"){grdStore.MasterTableView.GetItems(GridItemType.CommandItem)[0].FireCommandEvent(RadGrid.InitInsertCommandName, String.Empty);}}}
Using the above code in PreRender now successfully fires the ItemCommand event handler in RadGrid, where we get the manufacturer ID and assign it to the textbox in the user control.
Veli
the Telerik team
Browse the vast support resources we have to jumpstart your development with RadControls for ASP.NET AJAX. See how to integrate our AJAX controls seamlessly in SharePoint 2007/2010 visiting our common SharePoint portal.
0
pylacroix
Top achievements
Rank 1
answered on 17 Nov 2010, 01:55 PM
Thanks Veli it works fine now
0
Kiranmayee
Top achievements
Rank 1
answered on 15 Sep 2018, 02:36 AM
Hi,
I tried the Item Command syntax. It worked when I was using it in the master table but it does not work when trying to do the same in detail table. Says Object not set to reference. Please help.