This is a migrated thread and some comments may be shown as answers.

pass parameters to user control used for edit mode

17 Answers 1022 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Miran Zgec
Top achievements
Rank 2
Miran Zgec asked on 03 Mar 2008, 04:06 PM
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







17 Answers, 1 is accepted

Sort by
0
Vlad
Telerik team
answered on 04 Mar 2008, 08:37 AM
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
0
Miran Zgec
Top achievements
Rank 2
answered on 04 Mar 2008, 11:10 AM
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?
0
Vlad
Telerik team
answered on 04 Mar 2008, 11:14 AM
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
0
Miran Zgec
Top achievements
Rank 2
answered on 05 Mar 2008, 01:23 PM
I'm kind of lost here, could you please post some sample code?

Thank you


0
Iana Tsolova
Telerik team
answered on 06 Mar 2008, 11:22 AM
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
0
Miran Zgec
Top achievements
Rank 2
answered on 07 Mar 2008, 10:42 AM
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
0
Accepted
Iana Tsolova
Telerik team
answered on 07 Mar 2008, 04:34 PM
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
0
Miran Zgec
Top achievements
Rank 2
answered on 08 Mar 2008, 09:43 AM
Just what I needed!

Thank u 1000 times.
0
Waleed Seada
Top achievements
Rank 2
answered on 05 Jun 2008, 05:53 AM
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
0
Iana Tsolova
Telerik team
answered on 05 Jun 2008, 09:54 AM
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
0
Waleed Seada
Top achievements
Rank 2
answered on 05 Jun 2008, 12:44 PM
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
0
Iana Tsolova
Telerik team
answered on 05 Jun 2008, 03:13 PM
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
0
Rajan
Top achievements
Rank 1
answered on 08 Jun 2015, 11:01 AM

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>

0
Rajan
Top achievements
Rank 1
answered on 09 Jun 2015, 04:52 AM
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 
0
Eyup
Telerik team
answered on 11 Jun 2015, 07:04 AM
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
0
Rajan
Top achievements
Rank 1
answered on 12 Jun 2015, 04:55 AM

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

0
Eyup
Telerik team
answered on 17 Jun 2015, 06:12 AM
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
Tags
Grid
Asked by
Miran Zgec
Top achievements
Rank 2
Answers by
Vlad
Telerik team
Miran Zgec
Top achievements
Rank 2
Iana Tsolova
Telerik team
Waleed Seada
Top achievements
Rank 2
Rajan
Top achievements
Rank 1
Eyup
Telerik team
Share this question
or