pass parameters to user control used for edit mode

18 posts, 1 answers
  1. Miran Zgec
    Miran Zgec avatar
    14 posts
    Member since:
    Oct 2006

    Posted 03 Mar 2008 Link to this post

    Hello

    I'm having problem passing parameters to user control used in edit mode of radgrid

    I Have a RadGrid with some users data. I use a custom user control for FormTemplate.

    The user control has it's own logic implemented to edit user data and takes one parameter "id_user". (It has public String id_user with get and set method defined  in codebehind)

    I tried to pass this parameter to UserControl as seen in code below, but it doesnt work. when i pass this parameter to label it shows. User control also works fine if I pass static parameter (example:   id_user="2" ).

    What is the right way to pass parameters to UC?

    <EditFormSettings EditFormType="Template"
       <FormTemplate> 
           <asp:Label ID="Label4" runat="server" Text='<%# Bind("id") %>'></asp:Label> 
           <uc1:edit_user ID="edit_user1" runat="server" id_user='<%# Bind("id") %>' />    
       </FormTemplate> 
       <EditColumn CurrentFilterFunction="NoFilter" FilterListOptions="VaryByDataType" 
                   uniquename="EditCommandColumn"
        </EditColumn> 
    </EditFormSettings>

    I'm would also like to know how to pass parameters when attachig UserControls like this:
     <EditFormSettings EditFormType="WebUserControl" UserControlName="edit_user.ascx"

    Thank you







  2. Vlad
    Admin
    Vlad avatar
    11100 posts

    Posted 04 Mar 2008 Link to this post

    Hi Miran,

    When using UserControl as edit form you should declare public object DataItem and the grid will pass automatically current DataItem to this property.

    All the best,
    Vlad
    the Telerik team

    Instantly find answers to your questions at the new Telerik Support Center
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Miran Zgec
    Miran Zgec avatar
    14 posts
    Member since:
    Oct 2006

    Posted 04 Mar 2008 Link to this post

    Thanks, works great.

    But now I have another problem.

    When RadGrid is loaded and "edit" is clicked for the first time everything is OK and UserControl loads fine with dataRowView parameters passed to it (I use label to display some passed valuses)

    but when I click on some other "edit" in some other row i get an error:
    "Object reference not set to an instance of an object" (this error comes from loaded UserControl where I try to acces DataItem)

    It looks like when I try to edit some other row DataItem property is not passed to user control?

    Any Ideas?
  5. Vlad
    Admin
    Vlad avatar
    11100 posts

    Posted 04 Mar 2008 Link to this post

    Hi Miran,

    DataItem is available only during data-binding. You can use DataKeyNames/DataKeyValues to save the key in the ViewState and use this key to find desired item in your collection when needed (post-back event for example).

    Regards,
    Vlad
    the Telerik team

    Instantly find answers to your questions at the new Telerik Support Center
  6. Miran Zgec
    Miran Zgec avatar
    14 posts
    Member since:
    Oct 2006

    Posted 05 Mar 2008 Link to this post

    I'm kind of lost here, could you please post some sample code?

    Thank you


  7. Iana Tsolova
    Admin
    Iana Tsolova avatar
    3388 posts

    Posted 06 Mar 2008 Link to this post

    Hello Miran Zgec,

    I've prepared a sample project for you and attached it to this post for your convenience. You can see how to pass a parameter from the main page to the user control using DataKeyValues and implement the update. Please let us know if you need more help.

    Sincerely yours,
    Iana
    the Telerik team

    Instantly find answers to your questions at the new Telerik Support Center
  8. Miran Zgec
    Miran Zgec avatar
    14 posts
    Member since:
    Oct 2006

    Posted 07 Mar 2008 Link to this post

    Hello
    Great example, cleared a lot of things for me.

    There's one more thing.

    In your example you set string employeeID (ascx file) in btn_Update_Clicked event, by reading DataKeyName EmployeeID.

    I need to set employeeID in Page_Load Event.
    I must be dooing something wrong, becouse I can't seem to get value of DataKeyName EmployeeID.
    (I was trying to do that on your example).

    In my application I do not fill ascx form data with  " Text='<%# DataBinder.Eval( Container, "DataItem.Country" ) %>' " method ,
    but with custom data obtained from some Db tables(depending on ID) , so It's crutial for me to get this ID in Page_load event, before I call functions that fetch data from DB.

    I hope I'm clear.

    Thank you
  9. Answer
    Iana Tsolova
    Admin
    Iana Tsolova avatar
    3388 posts

    Posted 07 Mar 2008 Link to this post

    Hi Miran Zgec,

    You can get the employeeID value on PageLoad from the _dataItem property in the user control. Because its value is set before the PageLoad event fires, you only need to find the employeeID in its array collection there. I've modified the sample from my previous post to show how this can be done.

    Best regards,
    Iana
    the Telerik team

    Instantly find answers to your questions at the new Telerik Support Center
  10. Miran Zgec
    Miran Zgec avatar
    14 posts
    Member since:
    Oct 2006

    Posted 08 Mar 2008 Link to this post

    Just what I needed!

    Thank u 1000 times.
  11. Waleed Seada
    Waleed Seada avatar
    241 posts
    Member since:
    May 2006

    Posted 05 Jun 2008 Link to this post

    Hello Iana,

    I just downloaded your sample code, and it doesn't work for me;
    - Clicking Update button doesn't fire ...
    - The delete button is working fine.
    - The Add New Record gives an error says:
    "Object reference not set to an instance of an object"

    I didn't change anything, I don't know If I have to or not ...

    can you help me in this...
    Best regards
    Waleed
  12. Iana Tsolova
    Admin
    Iana Tsolova avatar
    3388 posts

    Posted 05 Jun 2008 Link to this post

    Hello Waleed,

    I double-checked the last project I sent you:

    • The Update button fires its click command as expected which is implemented in the user control. If you would like it to fire the grid UpdateCommand, you need to set its CommandName to "Update".
    • To avoid the error on Add New Record click, please modify the code in the user control as follows:

    WebUserControl1.ascx.cs Old:

    protected void Page_Load(object sender, EventArgs e)  
    {  
        this.DataBinding += new System.EventHandler(this.EmployeeDetails_DataBinding);  
        if (this._dataItem != null)  
        {  
            employeeID = ((System.Data.DataRowView)(this._dataItem)).Row.ItemArray[0].ToString();  
        }  


    WebUserControl1.ascx.cs New:
    protected void Page_Load(object sender, EventArgs e)  
    {  
        this.DataBinding += new System.EventHandler(this.EmployeeDetails_DataBinding);     
        if (this._dataItem != null &&  (this._dataItem is DataRowView))  
        {  
            employeeID = DataBinder.Eval(DataItem, "EmployeeID").ToString();  
        }  

    Let me know whether this helps.

    Best regards,
    Iana
    the Telerik team

    Instantly find answers to your questions at the new Telerik Support Center
  13. Waleed Seada
    Waleed Seada avatar
    241 posts
    Member since:
    May 2006

    Posted 05 Jun 2008 Link to this post

    Hello Iana,

    If you can help me ifentify the issue with my project I will be gratfull to you.

    I downloaded the project as it is, but the ONLY update is not firing ...

    can you have a look at this support ticket : 142310

    I appreciate your help very much
    Best regards
    Waleed
  14. Iana Tsolova
    Admin
    Iana Tsolova avatar
    3388 posts

    Posted 05 Jun 2008 Link to this post

    Hello Waleed,

    Thank you for opening a support ticket and sending your project. I will have a look at it and do my best to find a resolution to your problem.

    Best regards,
    Iana
    the Telerik team

    Instantly find answers to your questions at the new Telerik Support Center
  15. Rajan
    Rajan avatar
    9 posts
    Member since:
    May 2015

    Posted 08 Jun 2015 in reply to Iana Tsolova Link to this post

    I have added below code for DataBinding Handler. I have a radgrid within a UserControl "find" which in turn is loading another user control "radSelect". I am able to fetch the instance of radGrid. However EditItems returns no items. Here is the event handler.

    Protected Sub EmployeeDetails_DataBinding(sender As Object, e As System.EventArgs)
            If Not Me.Page.FindControl("find") Is Nothing Then
                For Each item As GridEditableItem In TryCast(Me.Page.FindControl("find").FindControl("radSelect"), RadGrid).EditItems
                    'get the DataKeyValues
                    'perform the update  for tha Item with such EmployeeID here
                    Me.EmployeeInfoID = item.OwnerTableView.DataKeyValues(item.ItemIndex)("EmployeeInfoID").ToString()
                Next
            End If
        End Sub

    ASPX

     

    <telerik:RadAjaxLoadingPanel ID="RadAjaxLoadingPanel1" runat="server" Skin="WebBlue">
    </telerik:RadAjaxLoadingPanel>
    <telerik:RadAjaxPanel ID="RadAjaxPanel1" runat="server" LoadingPanelID="RadAjaxLoadingPanel1">
        <telerik:RadAjaxManager ID="RadAjaxManager1" runat="server">
            <AjaxSettings>
                <telerik:AjaxSetting AjaxControlID="radSelect">
                    <UpdatedControls>
                        <telerik:AjaxUpdatedControl ControlID="radSelect"></telerik:AjaxUpdatedControl>
                    </UpdatedControls>
                </telerik:AjaxSetting>
            </AjaxSettings>
        </telerik:RadAjaxManager>
        <telerik:RadCodeBlock ID="RadCodeBlock1" runat="server">
        <script type="text/javascript">
            function RowDblClick(sender, eventArgs) {
                sender.get_masterTableView().editItem(eventArgs.get_itemIndexHierarchical());
            }
        </script>
    </telerik:RadCodeBlock>
        <telerik:RadGrid ID="radSelect" runat="server" AllowFilteringByColumn="True" AllowPaging="True"
            AutoGenerateColumns="False" ResolvedRenderMode="Classic" CellSpacing="-1"
            GridLines="Both">
            <ClientSettings>
                <ClientEvents OnRowDblClick="RowDblClick" />
            </ClientSettings>
            <MasterTableView AllowNaturalSort="False" DataKeyNames="EmployeeInfoID" ClientDataKeyNames="EmployeeInfoID">
                <CommandItemSettings ShowAddNewRecordButton="False" ShowRefreshButton="False" />
                <Columns>
                      
                </Columns>
                <EditFormSettings EditFormType="Template">
                    <FormTemplate>
                    <table id="Table2" cellspacing="2" cellpadding="1" width="100%" border="0" rules="none"
                            style="border-collapse: collapse;">
                            <tr class="EditFormHeader">
                                <td colspan="2">
                                    <b>Employee Details</b>
                                </td>
                            </tr>
                            <tr>
                                <td><uc1:DualDatePicker id="DatePicker1" IsShort="False" runat="server"></uc1:DualDatePicker></td>
                            </tr>
                            <tr>
                            </tr>
                        </table>
                    </FormTemplate>
                </EditFormSettings>               
            </MasterTableView>
        </telerik:RadGrid>
        <telerik:RadToolTip ID="RadToolTip1" runat="server" OffsetY="3" Position="TopCenter"
                        ShowCallout="false" Height="20px" ShowEvent="fromcode" />
    </telerik:RadAjaxPanel>

  16. Rajan
    Rajan avatar
    9 posts
    Member since:
    May 2015

    Posted 08 Jun 2015 Link to this post

    I know that this is very old post and nobody may be monitoring... Can someone please help me resolve this issue? I am stuck currently 
  17. Eyup
    Admin
    Eyup avatar
    3013 posts

    Posted 11 Jun 2015 Link to this post

    Hi Rajan,

    You can access the generated edit form user control and execute the desired logic:
    ( Section Accessing controls in edit/insert mode )
    http://www.telerik.com/help/aspnet-ajax/grid-accessing-cells-and-rows.html


    You can also access the container GridEditableItem in the WebUserControl code-behind using the this.NamingContainer directive and get the DataKeyID of the edited item using the GetDataKeyValue() method.

    Hope this helps.

    Regards,
    Eyup
    Telerik
    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 Feedback Portal and vote to affect the priority of the items
  18. Rajan
    Rajan avatar
    9 posts
    Member since:
    May 2015

    Posted 11 Jun 2015 in reply to Eyup Link to this post

    Thanks Eyup for the Reply. I am able to get Radgrid object in my WebControl. As mentioned in this post, i have handled DataBinding handler and got radSelect (which is my radgrid) My problem is that whenever i try to access EditItems Collection, i don't find any items in It and hence don't get the employee id of the row which i double clicked.  

    Protected Sub EmployeeDetails_DataBinding(sender As Object, e As System.EventArgs)
            If Not Me.Page.FindControl("find") Is Nothing Then
                For Each item As GridEditableItem In TryCast(Me.Page.FindControl("find").FindControl("radSelect"), RadGrid).EditItems
                    'get the DataKeyValues
                    'perform the update  for tha Item with such EmployeeID here
                    Me.EmployeeInfoID = item.OwnerTableView.DataKeyValues(item.ItemIndex)("EmployeeInfoID").ToString()
                Next
            End If
        End Sub

  19. Eyup
    Admin
    Eyup avatar
    3013 posts

    Posted 17 Jun 2015 Link to this post

    Hi Rajan,

    Probably the DataBinding event is too early in the page life cycle. Please try another event like PreRender, Load or DataBound.

    Regards,
    Eyup
    Telerik
    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 Feedback Portal and vote to affect the priority of the items
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017