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

How to fire click event using a button in nestedviewtemplate inside a grid

5 Answers 151 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Abdul
Top achievements
Rank 1
Abdul asked on 06 Jun 2013, 09:56 PM
I am evaluating Telerik component for a project and I am running into some issue that I hope someone could assist me with.

I have a button inside a nestedviewtemplate which is embedded inside a grid control. I have populated the grid with my data using the OnNeedDataSource property, I was also able to get detailed records on the ListView, however, I needed to perform an update to some tables when a button inside the nestedviewtemplate is clicked.

I could not find the button inside the click event. Also, I do not know how to wire the click event to my button since it cannot be referenced. I have my codes attached.

I will appreciate any assistance while I continue to search the forum for possible prior solution to this type of question

ASPX Codes:
===========
                <telerik:RadGrid ID="grdExtrUntReq" <br>                runat="server" <br>                AllowPaging="True" <br>                CellSpacing="0" <br>                CssClass="RadGridSearch"<br>                GridLines="None" <br>                OnNeedDataSource="grdExtrUntReq_NeedDataSource" <br>                PageSize="10"> <br>                    <MasterTableView AutoGenerateColumns="False"><br>                        <NoRecordsTemplate><br>                            No records<br>                        </NoRecordsTemplate><br>                        <RowIndicatorColumn Visible="True"><br>                            <HeaderStyle Width="20px"></HeaderStyle><br>                        </RowIndicatorColumn><br>                        <ExpandCollapseColumn Visible="True"><br>                            <HeaderStyle Width="20px"></HeaderStyle><br>                        </ExpandCollapseColumn><br>                        <Columns><br>                            <telerik:GridTemplateColumn DataField="personid" HeaderText="ID" UniqueName="TemplateColumn"><br>                                <ItemTemplate><br>                                    <asp:Label ID="lblID" runat="server" Text='<%# Eval("personid") %>'></asp:Label><br>                                    <asp:HiddenField ID="hfID" runat="server" Value='<%# Eval("personid") %>' /><br>                                </ItemTemplate><br>                            </telerik:GridTemplateColumn><br>                            <telerik:GridTemplateColumn DataField="fullname" HeaderText="Full Name" UniqueName="TemplateColumn1"><br>                                <ItemTemplate><br>                                    <asp:Label ID="Label1" runat="server" Text='<%# Eval("fullname").ToString() %>'></asp:Label><br>                                </ItemTemplate><br>                            </telerik:GridTemplateColumn><br>                            <telerik:GridTemplateColumn DataField="ayear" HeaderText="Year" UniqueName="TemplateColumn2"><br>                                <ItemTemplate><br>                                    <asp:Label ID="Label2" runat="server" Text='<%# Eval("ayear").ToString() %>'></asp:Label><br>                                </ItemTemplate><br>                            </telerik:GridTemplateColumn><br>                            <telerik:GridTemplateColumn DataField="levelcode" HeaderText="Level" UniqueName="TemplateColumn3"><br>                                <ItemTemplate><br>                                    <asp:Label ID="Label3" runat="server" Text='<%# Eval("levelcode").ToString() %>'></asp:Label><br>                                </ItemTemplate><br>                            </telerik:GridTemplateColumn><br>                            <telerik:GridTemplateColumn DataField="semcode" HeaderText="Seminoles" UniqueName="TemplateColumn4"><br>                                <ItemTemplate><br>                                    <asp:Label ID="Label4" runat="server" Text='<%# Eval("semcode").ToString() %>'></asp:Label><br>                                </ItemTemplate><br>                            </telerik:GridTemplateColumn><br><br>                        </Columns><br>                        <br>                        <br>                        <NestedViewTemplate><br>                        <telerik:RadListView <br>                        ID="lvExtrUntReqApprovalList" <br>                        runat="server" <br>                        OnNeedDataSource="lvExtrUntReqApprovalList_NeedDataSource"<br>                        ItemPlaceholderID="phExtraUntReq"><br>                            <LayoutTemplate><br>                                <fieldset><br>                                    <legend style="padding: 5px;"><b>Request Details</b><br>                                    </legend><br>                                        <asp:PlaceHolder ID="phExtraUntReq" runat="server"></asp:PlaceHolder><br>                                </fieldset><br>                            </LayoutTemplate><br>                            <ItemTemplate><br>                            <table><br>                            <tr><br>                            <td style="font-style:italic;">Full Name:</td><br>                            <td style="font-weight:bold"><br>                            <asp:Label ID="Label8" runat="server" Text='<%# Eval("fullname") %>'><br>                            </asp:Label><br>                            </td><br>                            </tr><br>                            <tr><br>                            <td style="font-style:italic;">Overall Grade:</td><br>                            <td style="font-weight:bold"><br>                            <asp:Label ID="Label9" runat="server" Text='<%# Eval("grade") %>'><br>                            </asp:Label><br>                            </td><br>                            </tr><br>                            <tr><br>                            <td style="font-style:italic;">Requested Unit(s):</td><br>                            <td style="font-weight:bold"><br>                            <asp:Label ID="Label10" runat="server" Text='<%# Eval("unitrequested") %>'><br>                            </asp:Label><br>                            </td><br>                            </tr><br>                            <tr><br>                            <td style="font-style:italic;">Request Date & Time:</td><br>                            <td style="font-weight:bold"><br>                            <asp:Label ID="Label12" runat="server" Text='<%# Eval("daterequested") %>'><br>                            </asp:Label><br>                            </td><br>                            </tr><br>                            <tr><br>                            <td>Approve/Reject Request:</td><br>                            <td><br>                                <asp:CheckBox ID="chkAprv" runat="server" Text="Approve" /> <br>                                <asp:CheckBox ID="chkRej" runat="server" Text="Reject" /><br>                                <cc1:MutuallyExclusiveCheckBoxExtender ID="chkAprvE" runat="server" TargetControlID="chkAprv" Key="AprvRej" /> <br>                                <cc1:MutuallyExclusiveCheckBoxExtender ID="chkRejE" runat="server" TargetControlID="chkRej" Key="AprvRej" /><br>                            </td><br>                            </tr><br>                            <tr><br>                            <td style="font-style:italic;">Unit(s) Approved:</td><br>                            <td style="font-weight:bold"><br>                                <asp:DropDownList ID="ddlUntAprv" runat="server"><br>                                <asp:ListItem Text="Select..." Value=""></asp:ListItem><br>                                <asp:ListItem Text="1" Value="1"></asp:ListItem><br>                                <asp:ListItem Text="2" Value="2"></asp:ListItem><br>                                <asp:ListItem Text="3" Value="3"></asp:ListItem><br>                                <asp:ListItem Text="4" Value="4"></asp:ListItem><br>                                <asp:ListItem Text="5" Value="5"></asp:ListItem><br>                                <asp:ListItem Text="6" Value="6"></asp:ListItem><br>                                <asp:ListItem Text="7" Value="6"></asp:ListItem><br>                                <asp:ListItem Text="8" Value="6"></asp:ListItem><br>                                <asp:ListItem Text="9" Value="6"></asp:ListItem><br>                                <asp:ListItem Text="10" Value="6"></asp:ListItem><br>                                <asp:ListItem Text="11" Value="6"></asp:ListItem><br>                                <asp:ListItem Text="12" Value="6"></asp:ListItem><br>                                </asp:DropDownList><br>                            </td><br>                            </tr><br>                            <tr><br>                            <td><br>                               <asp:Button ID="btnExUntReq" runat="server" Text="Submit" onclick="btnExUntReq_Click" /></td><br>                            </tr><br>                            </table><br>                            </ItemTemplate><br>                        </telerik:RadListView><br>                    </NestedViewTemplate><br>                        <br>                        <br>                        <EditFormSettings><br>                            <EditColumn FilterControlAltText="Filter EditCommandColumn column"><br>                            </EditColumn><br>                        </EditFormSettings><br>                    </MasterTableView><br>                </telerik:RadGrid>

CS Codes
========
   
protected void grdExtrUntReq_NeedDataSource(object source, GridNeedDataSourceEventArgs e)<br>    {<br>        //Get year<br>        LookUpRecordsInformation l1 = LookUps.GetApplicationSettingValues("CURRENT_YEAR");<br>        string sYear = l1.sSettingValue;<br><br>        //Get seminole<br>        LookUpRecordsInformation l2 = LookUps.GetApplicationSettingValues("CURRENT_SEMINOLE");<br>        string sSeminole = l2.sSettingValue;<br><br>        string sApproverID = Page.User.Identity.Name.ToString();<br><br>        grdExtrUntReq.DataSource = GetRecords.GetUnitRequestListForApproval(sYear, sSeminole, sApproverID);<br>    }<br><br>    protected void lvExtrUntReqApprovalList_NeedDataSource(object sender, RadListViewNeedDataSourceEventArgs e)<br>    {<br><br>        //Get year<br>        LookUpRecordsInformation l1 = LookUps.GetApplicationSettingValues("CURRENT_YEAR");<br>        string sYear = l1.sSettingValue;<br><br>        //Get seminole<br>        LookUpRecordsInformation l2 = LookUps.GetApplicationSettingValues("CURRENT_SEMINOLE");<br>        string sSeminole = l2.sSettingValue;<br><br>        GridDataItem parentItem = ((sender as RadListView).NamingContainer as GridNestedViewItem).ParentItem as GridDataItem;<br>        string sID = (parentItem.FindControl("hfID") as HiddenField).Value;<br>        (sender as RadListView).DataSource = GetRecords.GetUnitRequestList(sID, sYear, sSeminole);<br>    }<br><br><br>        protected void btnExUntReq_Click(object sender, EventArgs e)<br>    {<br>        //Got completely lost here....on how to wire button btnExUntReq to fire the codes to perform my updates<br>    <br>    bool success = UpdateRecords.UpdateUntRequestList(sID, sYear, sSeminole);<br>    }

5 Answers, 1 is accepted

Sort by
0
Accepted
Jayesh Goyani
Top achievements
Rank 2
answered on 07 Jun 2013, 12:39 PM
Hello,

protected void btnExUntReq_Click(object sender, EventArgs e)
    {
        // Get button
        Button btn = sender as Button;
        // Get current NestedViewTemppate row
        GridNestedViewItem CurrentItem = btn.NamingContainer as GridNestedViewItem;
        // Get Parent Item
        GridDataItem ParentItem = CurrentItem.ParentItem as GridDataItem;
 
    }
<asp:Button ID="btnExUntReq" runat="server" Text="Submit" OnClick="btnExUntReq_Click" CausesValidation="false" />


Thanks,
Jayesh Goyani
0
Abdul
Top achievements
Rank 1
answered on 09 Jun 2013, 12:48 AM
I really appreciate your response. Please, if you can still assist with this. I am having issues finding a label placed inside the NestedViewTemplate from within the Click even handler of the button.

    protected void btnExUntReq_Click(object sender, EventArgs e)
    {
      // Get button
        Button btn = sender as Button;
        // Get current NestedViewTemppate row
        GridNestedViewItem CurrentItem = btn.NamingContainer as GridNestedViewItem;
        // Get Parent Item
        GridDataItem ParentItem = CurrentItem.ParentItem as GridDataItem;//Got NullReferenceException: Object reference not set to an instance of an object.
        Label mLbl = ParentItem .FindControl("lblTest") as Label;
        mLbl.Text = "Hello";
    }
0
Princy
Top achievements
Rank 1
answered on 10 Jun 2013, 03:43 AM
Hi,

I guess you want to access a label inside the nested view template,please try the following code,and check if it helps.

C#:
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
{
  if (e.Item is GridNestedViewItem)
  {
    GridNestedViewItem nestedview = (GridNestedViewItem)e.Item;
    Label lbl = (Label)nestedview.FindControl("Label1");//Access the label inside nested view
    lbl.Text = "Text!";
  }
}

Thanks
Princy
0
Abdul
Top achievements
Rank 1
answered on 10 Aug 2013, 03:38 AM
Jayesh/Forum folks,

Maybe I did not make myself clear in my earlier post. Here is the declarative part of my aspx containing a NestedViewTemplate that I populated with single record returned from the database based on the muti-row returned from the parent Radgrid control. On the NestedViewTemplate, I have a button with event hadler where I need to get the values of the labels then save their values back into the database after a file is uploaded.

My request is that I need to figure out how to get the labels inside the NestedViewTemplate. I have tried the method you suggested earlier but I got null reference error message. I really need to do this inside a button click on the NestedViewTemplate.

I will appreciate any assistance.

<NestedViewTemplate>
    <telerik:RadListView ID="lvStuCurr" runat="server" OnNeedDataSource="lvStuCurr_NeedDataSource"
        ItemPlaceholderID="phCourseCode">
        <LayoutTemplate>
            <fieldset>
                <legend style="padding: 5px;"><b>Submit Completed Assignment</b> </legend>
                <asp:PlaceHolder ID="phCourseCode" runat="server"></asp:PlaceHolder>
            </fieldset>
        </LayoutTemplate>
        <ItemTemplate>
            <table>
                <tr>
                    <td style="font-style: italic;">
                        Course Code:
                    </td>
                    <td style="font-weight: bold">
                        <asp:Label ID="lblTemplCourseCode" runat="server" Text='<%# Eval("CourseCode") %>'>
                        </asp:Label>
                        <asp:Label ID="Label1" runat="server" Text="Hello">
                        </asp:Label>
                    </td>
                </tr>
                <tr>
                    <td style="font-style: italic;">
                        Lesson:
                    </td>
                    <td style="font-weight: bold">
                        <asp:Label ID="lblTemplLesson" runat="server" Text='<%# Eval("lesson") %>'>
                        </asp:Label>
                    </td>
                </tr>
                <tr>
                    <td style="font-style: italic;">
                        Exercise:
                    </td>
                    <td style="font-weight: bold">
                        <asp:Label ID="lblTemplExercise" runat="server" Text='<%# Eval("exercise") %>'>
                        </asp:Label>
                    </td>
                </tr>
                <tr>
                    <td style="font-style: italic;">
                        Upload:
                    </td>
                    <td>
                        <telerik:RadAsyncUpload ID="aUpAssgnUpld_1" runat="server" MaxFileSize="1049000"
                            MultipleFileSelection="Automatic" TemporaryFolder="../images/AssignUpload/TempAttach"
                            OnClientFileUploaded="onClientFileUploaded">
                        </telerik:RadAsyncUpload>
                    </td>
                </tr>
                <tr>
                    <td>
                    </td>
                    <td>
                        <asp:Button ID="btnUpldAssign" runat="server" Text="Submit" OnClick="btnUpldAssign_Click" />
                    </td>
                </tr>
            </table>
        </ItemTemplate>
    </telerik:RadListView>
</NestedViewTemplate>
0
Kostadin
Telerik team
answered on 14 Aug 2013, 02:25 PM
Hi Abdul,

You could use the following approach in order to find the GridNestedViewItem.
protected void btnUpldAssign_Click(object sender, EventArgs e)
    {
        Control control = sender as Control;
        bool isNestedViewItemFound = false;
        do
        {
            if (control is GridNestedViewItem)
            {
                isNestedViewItemFound = true;
            }
            else
            {
                control = control.NamingContainer;
            }
        }
        while (!isNestedViewItemFound);
 
        GridNestedViewItem nestedViewTempalte = control as GridNestedViewItem;
        Label label = nestedViewTempalte.FindControl("Label1") as Label;
    }


Regards,
Kostadin
Telerik
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to the blog feed now.
Tags
Grid
Asked by
Abdul
Top achievements
Rank 1
Answers by
Jayesh Goyani
Top achievements
Rank 2
Abdul
Top achievements
Rank 1
Princy
Top achievements
Rank 1
Kostadin
Telerik team
Share this question
or