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

Cannot Find Control within Grid to populate RadTextBoxes

4 Answers 115 Views
Grid
This is a migrated thread and some comments may be shown as answers.
pmourfield
Top achievements
Rank 1
pmourfield asked on 19 Aug 2011, 03:33 PM
Good morning everyone. Here is my problem. I have a DropdownList inside of an edit form template. On ItemDataBound its items' text and values are populated by a stored procedure. This is working perfectly. The problem is what I want to happen next. When the user clicks an item, the item's text and value are sent to the database (SQL Server) as parameters in order to pull more data and populate textboxes within the template. In my old solution it was as easy at using FindControl to find those textboxes on the page and setting their text as equal to whatever was returned. This is proving to be much more difficult using RadGrid, though, as I cannot seem to find a way to actually find those controls to populate their text. I have included my markup for the radgrid and the DropDownList's selectedindexchanged method below:

####MARKUP####
 
<telerik:RadGrid ID="RadGrid1" runat="server" DataSourceID="theSource" AllowPaging="true"
            AllowAutomaticDeletes="true" AllowAutomaticInserts="true" AllowAutomaticUpdates="true"
            ExportSettings-Pdf-AllowPrinting="true" ShowStatusBar="true" ShowFooter="true"
            AllowSorting="true" AllowMultiRowEdit="true" ShowGroupPanel="true" OnDeleteCommand="RadGrid1_DeleteCommand"
            CellSpacing="0" GridLines="None" Skin="Web20" AutoGenerateColumns="false"
            OnItemCommand="RadGrid1_ItemCommand" OnInsertCommand="RadGrid1_InsertCommand"
            OnUpdateCommand="RadGrid1_UpdateCommand"
            OnItemInserted="RadGrid1_ItemInserted"
            onitemdatabound="RadGrid1_ItemDataBound">
            <PagerStyle Mode="NextPrevAndNumeric" />
            <ExportSettings IgnorePaging="true" OpenInNewWindow="true">
                <Pdf PageHeight="210mm" PageWidth="297mm" PageTitle="Weekly Goals" DefaultFontFamily="Arial Unicode MS"
                    PageBottomMargin="20mm" PageTopMargin="20mm" PageLeftMargin="20mm" PageRightMargin="20mm" />
            </ExportSettings>
            <MasterTableView EditMode="EditForms" CommandItemDisplay="Top" DataKeyNames="GoalID">
                <%--<CommandItemTemplate>
                <asp:Button ID="DownloadPDF" runat="server" Width="100%" CommandName="ExportToPdf" Text="Export to PDF"
                    CssClass="pdfButton" />
            </CommandItemTemplate>--%>
                <GroupByExpressions>
                    <telerik:GridGroupByExpression>
                        <SelectFields>
                            <telerik:GridGroupByField FieldName="BeginDate" HeaderText="Begin Date" FormatString="{0:d}" />
                        </SelectFields>
                        <GroupByFields>
                            <telerik:GridGroupByField FieldName="BeginDate" SortOrder="Descending" />
                        </GroupByFields>
                    </telerik:GridGroupByExpression>
                </GroupByExpressions>
                <Columns>
                    <telerik:GridEditCommandColumn>
                    </telerik:GridEditCommandColumn>
                    <telerik:GridBoundColumn DataField="GoalID" ReadOnly="true" Display="false" HeaderText="GoalID">
                    </telerik:GridBoundColumn>
                    <telerik:GridDateTimeColumn DataField="BeginDate" HeaderText="Begin Date" DataFormatString="{0:d}">
                    </telerik:GridDateTimeColumn>
                    <telerik:GridDateTimeColumn DataField="EndDate" HeaderText="End Date" DataFormatString="{0:d}"
                        EditFormColumnIndex="1">
                    </telerik:GridDateTimeColumn>
                    <telerik:GridBoundColumn DataField="GoalName" UniqueName="goalNameTextBox" HeaderText="Goal Name">
                    </telerik:GridBoundColumn>
                    <telerik:GridBoundColumn DataField="WeeklyGoal" HeaderText="Goal Description" EditFormColumnIndex="1">
                    </telerik:GridBoundColumn>
                    <telerik:GridCheckBoxColumn DataField="GoalComplete" HeaderText="Goal Complete?"
                        EditFormColumnIndex="2" DefaultInsertValue="false">
                    </telerik:GridCheckBoxColumn>
                    <telerik:GridBoundColumn DataField="Task1" Display="false" HeaderText="Step 1">
                    </telerik:GridBoundColumn>
                    <telerik:GridCheckBoxColumn DataField="T1Complete" Display="false" HeaderText="Complete"
                        EditFormColumnIndex="0" DefaultInsertValue="false">
                    </telerik:GridCheckBoxColumn>
                    <telerik:GridBoundColumn DataField="Task2" Display="false" HeaderText="Step 2">
                    </telerik:GridBoundColumn>
                    <telerik:GridCheckBoxColumn DataField="T2Complete" Display="false" HeaderText="Complete"
                        EditFormColumnIndex="0" DefaultInsertValue="false">
                    </telerik:GridCheckBoxColumn>
                    <telerik:GridBoundColumn DataField="Task3" Display="false" HeaderText="Step 3">
                    </telerik:GridBoundColumn>
                    <telerik:GridCheckBoxColumn DataField="T3Complete" Display="false" HeaderText="Complete"
                        EditFormColumnIndex="0" DefaultInsertValue="false">
                    </telerik:GridCheckBoxColumn>
                    <telerik:GridBoundColumn DataField="Task4" Display="false" HeaderText="Step 4">
                    </telerik:GridBoundColumn>
                    <telerik:GridCheckBoxColumn DataField="T4Complete" Display="false" HeaderText="Complete"
                        EditFormColumnIndex="0" DefaultInsertValue="false">
                    </telerik:GridCheckBoxColumn>
                    <telerik:GridBoundColumn DataField="Task5" Display="false" HeaderText="Step 5">
                    </telerik:GridBoundColumn>
                    <telerik:GridCheckBoxColumn DataField="T5Complete" Display="false" HeaderText="Complete"
                        EditFormColumnIndex="0" DefaultInsertValue="false">
                    </telerik:GridCheckBoxColumn>                   
                </Columns>
                <CommandItemSettings ExportToPdfText="Export to PDF"></CommandItemSettings>
                <RowIndicatorColumn FilterControlAltText="Filter RowIndicator column">
                </RowIndicatorColumn>
                <ExpandCollapseColumn FilterControlAltText="Filter ExpandColumn column">
                </ExpandCollapseColumn>
                <EditFormSettings InsertCaption="Add New Goal" ColumnNumber="3" EditFormType="Template">
                    <FormTemplate>
                        <table id="dateControlsTable" cellspacing="2" cellpadding="2" border="0" rules="none"
                            style="border-collapse: collapse;">
                            <tr class="EditFormHeader">
                                <td colspan="2">
                                    <b>Goal Data</b>
                                </td>
                            </tr>
                            <tr>
                                <td>
                                    <strong>Begin Date:</strong>
                                    <telerik:RadDatePicker runat="server" Skin="Web20" ID="beginDatePicker" DbSelectedDate="<%# Bind('BeginDate') %>">
                                    </telerik:RadDatePicker>
                                </td>
                                <td>
                                    <strong>End Date:</strong>
                                    <telerik:RadDatePicker runat="server" Skin="Web20" ID="endDatePicker" DbSelectedDate="<%# Bind('EndDate') %>">
                                    </telerik:RadDatePicker>
                                </td>
                                <td>
                                    <asp:HiddenField runat="server" ID="goalIDHF" Value="<%# Bind('GoalID') %>" />
                                </td>
                            </tr>
                        </table>
                        <p>
                        </p>
                        <table id="goalDataTable" cellspacing="2" cellpadding="2" border="0" rules="none"
                            style="border-collapse: collapse;">
                            <tr>
                                <td>
                                    <strong>Goal Name:</strong>
                                </td>
                                <td>
                                    <telerik:RadTextBox runat="server" Skin="Web20" ID="goalNameTextBox" Text="<%# Bind('GoalName') %>"
                                        Width="500">
                                    </telerik:RadTextBox>
                                </td>
                                <td>
                                    <strong>Goal Complete:</strong>
                                </td>
                                <td style="text-align: center">
                                    <%--<asp:CheckBox runat="server" ID="goalCompleteCheckBox" Checked="<%# Bind('GoalComplete') %>" />--%>
                                </td>
                            </tr>
                            <tr>
                                <td>
                                    <strong>Goal Description:</strong>
                                </td>
                                <td>
                                    <telerik:RadTextBox runat="server" Skin="Web20" ID="goalDescriptionTextBox" Text="<%# Bind('WeeklyGoal') %>"
                                        Width="500">
                                    </telerik:RadTextBox>
                                </td>
                                <td>
                                </td>
                                <td>
                                </td>
                            </tr>
                            <tr>
                                <td>
                                </td>
                            </tr>
                            <tr>
                                <td>
                                    <strong>Steps</strong>
                                </td>
                                <td>
                                </td>
                                <td>
                                    <strong>Hours</strong>
                                </td>
                                <td style="text-align: center">
                                    <strong>Complete</strong>
                                </td>
                            </tr>
                            <tr>
                                <td style="float: right">
                                    1.
                                </td>
                                <td>
                                    <telerik:RadTextBox runat="server" Skin="Web20" ID="step1TextBox" Text="<%# Bind('Task1') %>"
                                        Width="500">
                                    </telerik:RadTextBox>
                                </td>
                                <td>
                                    <telerik:RadTextBox runat="server" Skin="Web20" ID="step1HoursTextBox" Text="<%# Bind('T1EstimatedHours') %>"
                                        Width="30">
                                    </telerik:RadTextBox>
                                </td>
                                <td style="text-align: center">
                                    <asp:CheckBox runat="server" ID="step1Complete" Checked="<%# Bind('T1Complete') %>" />
                                </td>
                                <td>
                                    <asp:DropDownList ID="TemplateDropDown" runat="server"
                                            AppendDataBoundItems="True" AutoPostBack="True" CssClass="templateDropDown"
                                            OnSelectedIndexChanged="TemplateDropDown_SelectedIndexChanged"
                                            ViewStateMode="Enabled">
                                            <asp:ListItem Value="0">Choose...</asp:ListItem>
                                    </asp:DropDownList>
                                </td>
                            </tr>
                            <tr>
                                <td style="float: right">
                                    2.
                                </td>
                                <td>
                                    <telerik:RadTextBox runat="server" Skin="Web20" ID="step2TextBox" Text="<%# Bind('Task2') %>"
                                        Width="500">
                                    </telerik:RadTextBox>
                                </td>
                                <td>
                                    <telerik:RadTextBox runat="server" Skin="Web20" ID="step2HoursTextBox" Text="<%# Bind('T2EstimatedHours') %>"
                                        Width="30">
                                    </telerik:RadTextBox>
                                </td>
                                <td style="text-align: center">
                                    <asp:CheckBox runat="server" ID="step2Complete" Checked="<%# Bind('T2Complete') %>" />
                                </td>
                            </tr>
                            <tr>
                                <td style="float: right">
                                    3.
                                </td>
                                <td>
                                    <telerik:RadTextBox runat="server" Skin="Web20" ID="step3TextBox" Text="<%# Bind('Task3') %>"
                                        Width="500">
                                    </telerik:RadTextBox>
                                </td>
                                <td>
                                    <telerik:RadTextBox runat="server" Skin="Web20" ID="step3HoursTextBox" Text="<%# Bind('T3EstimatedHours') %>"
                                        Width="30">
                                    </telerik:RadTextBox>
                                </td>
                                <td style="text-align: center">
                                    <asp:CheckBox runat="server" ID="step3Complete" Checked="<%# Bind('T3Complete') %>" />
                                </td>
                            </tr>
                            <tr>
                                <td style="float: right">
                                    4.
                                </td>
                                <td>
                                    <telerik:RadTextBox runat="server" Skin="Web20" ID="step4TextBox" Text="<%# Bind('Task4') %>"
                                        Width="500">
                                    </telerik:RadTextBox>
                                </td>
                                <td>
                                    <telerik:RadTextBox runat="server" Skin="Web20" ID="step4HoursTextBox" Text="<%# Bind('T4EstimatedHours') %>"
                                        Width="30">
                                    </telerik:RadTextBox>
                                </td>
                                <td style="text-align: center">
                                    <asp:CheckBox runat="server" ID="step4Complete" Checked="<%# Bind('T4Complete') %>" />
                                </td>
                            </tr>
                            <tr>
                                <td style="float: right">
                                    5.
                                </td>
                                <td>
                                    <telerik:RadTextBox runat="server" Skin="Web20" ID="step5TextBox" Text="<%# Bind('Task5') %>"
                                        Width="500">
                                    </telerik:RadTextBox>
                                </td>
                                <td>
                                    <telerik:RadTextBox runat="server" Skin="Web20" ID="step5HoursTextBox" Text="<%# Bind('T5EstimatedHours') %>"
                                        Width="30">
                                    </telerik:RadTextBox>
                                </td>
                                <td style="text-align: center">
                                    <asp:CheckBox runat="server" ID="step5Complete" Checked="<%# Bind('T5Complete') %>" />
                                </td>
                            </tr>
                            <tr>
                                <td align="right" colspan="2">
                                    <asp:LinkButton ID="btnUpdate" Text='<%# (Container is GridEditFormInsertItem) ? "Insert" : "Update" %>'
                                        runat="server" CommandName='<%# (Container is GridEditFormInsertItem) ? "PerformInsert" : "Update" %>'>
                                    </asp:LinkButton> <asp:LinkButton ID="btnCancel" Text="Cancel" runat="server"
                                        CausesValidation="False" UseSubmitBehavior="false" CommandName="Cancel"></asp:LinkButton> <asp:LinkButton
                                            ID="btnDelete" Text="Delete" runat="server" CausesValidation="false" CommandName="Delete" />
                                </td>
                            </tr>
                        </table>
                    </FormTemplate>
                    <EditColumn FilterControlAltText="Filter EditCommandColumn column">
                    </EditColumn>
                </EditFormSettings>
            </MasterTableView>
            <FilterMenu EnableImageSprites="False">
            </FilterMenu>
            <HeaderContextMenu CssClass="GridContextMenu GridContextMenu_Default">
            </HeaderContextMenu>
        </telerik:RadGrid>
 
####C Sharp####
 
protected void TemplateDropDown_SelectedIndexChanged(object sender, EventArgs e)
    {
        DropDownList TemplateDropDown = (DropDownList)sender;
 
        if (TemplateDropDown.SelectedIndex > 0)
        {
            string templateID = TemplateDropDown.SelectedItem.Value.ToString();
            string templateName = TemplateDropDown.SelectedItem.Text.ToString();
 
            DataTable dt = new DataTable();
            SqlConnection connection = new SqlConnection(GetConnectionString());
            try
            {
                connection.Open();
                SqlCommand cmd = new SqlCommand("RetrieveWeeklyGoalTemplate", connection);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@GoalID", templateID);
                cmd.Parameters.AddWithValue("@TemplateName", templateName);
                SqlDataAdapter da = new SqlDataAdapter(cmd);
 
                da.Fill(dt);
                if (dt.Rows.Count >= 0)  //the greater than or equal to is necessary to delete the last row in the gridview.
                {
                    //GridEditFormItem editform = (GridEditFormItem)sender;
 
                    string estHours1 = dt.Rows[0][2].ToString();
                    string estHours2 = dt.Rows[0][4].ToString();
                    string estHours3 = dt.Rows[0][6].ToString();
                    string estHours4 = dt.Rows[0][8].ToString();
                    string estHours5 = dt.Rows[0][10].ToString();
 
                    valueChecker.valueChecker value = new valueChecker.valueChecker();
 
                    System.Collections.Specialized.ListDictionary templateValues = new System.Collections.Specialized.ListDictionary();
 
                    templateValues["goalNameTextBox"] = dt.Rows[0]["TemplateName"].ToString();
                    templateValues["goalDescriptionTextBox"] = dt.Rows[0]["WeeklyGoal"].ToString();
                    templateValues["step1TextBox"] = dt.Rows[0]["Task1"].ToString();
                    templateValues["step2TextBox"] = dt.Rows[0]["Task2"].ToString();
                    templateValues["step3TextBox"] = dt.Rows[0]["Task3"].ToString();
                    templateValues["step4TextBox"] = dt.Rows[0]["Task4"].ToString();
                    templateValues["step5TextBox"] = dt.Rows[0]["Task5"].ToString();
 
                    Response.Write(RadGrid1.MasterTableView.Items["goalNameTextBox"].ID.ToString());
                  
                    ((TextBox)RadGrid1.MasterTableView.FindControl("goalNameTextBox")).Text = dt.Rows[0]["TemplateName"].ToString();
                    ((TextBox)RadGrid1.MasterTableView.FindControl("goalDescriptionTextBox")).Text = dt.Rows[0]["WeeklyGoal"].ToString();
                    ((TextBox)RadGrid1.MasterTableView.FindControl("step1TextBox")).Text = dt.Rows[0]["Task1"].ToString();
                    ((TextBox)RadGrid1.MasterTableView.FindControl("step1TextBox")).Text = dt.Rows[0]["Task2"].ToString();
                    ((TextBox)RadGrid1.MasterTableView.FindControl("step1TextBox")).Text = dt.Rows[0]["Task3"].ToString();
                    ((TextBox)RadGrid1.MasterTableView.FindControl("step1TextBox")).Text = dt.Rows[0]["Task4"].ToString();
                    ((TextBox)RadGrid1.MasterTableView.FindControl("step1TextBox")).Text = dt.Rows[0]["Task5"].ToString();
                     
                     
                }
                TemplateDropDown.Items.Clear();
                TemplateDropDown.Items.Add(new ListItem("Personal Goal Library...", "0"));
                                 
            }
            catch (System.Data.SqlClient.SqlException ex)
            {
                string msg = "Fetch Error:";
                msg += ex.Message;
                throw new Exception(msg);
            }
            finally
            {
                connection.Close();
                connection.Dispose();
            }           
        }
    }

4 Answers, 1 is accepted

Sort by
0
Jayesh Goyani
Top achievements
Rank 2
answered on 19 Aug 2011, 05:23 PM
Hello,

Please see below code snippet.

protected void TemplateDropDown_SelectedIndexChanged(object sender, EventArgs e)
   {
       DropDownList TemplateDropDown= (DropDownList)sender;
       GridEditFormItem editItem = (GridEditFormItem)TemplateDropDown.NamingContainer;
       TextBox goalNameTextBox= (TextBox)editItem.FindControl("goalNameTextBox");
       goalNameTextBox.Text ="Your Text Sholud Come here";
 
   }

Let me know if any concern.

Thanks,
Jayesh Goyani
0
pmourfield
Top achievements
Rank 1
answered on 19 Aug 2011, 05:28 PM
Jayesh,

You are a live saver, my friend. Thank you for this answer.

Respectfully,

Joshua
0
Mark Wilbanks
Top achievements
Rank 1
answered on 15 Sep 2011, 10:12 PM
I would like to know how to add text to two textboxes from a click event of a check box. Javascript would be nice but code behind would work as well. any code would be helpful.
0
Shinu
Top achievements
Rank 2
answered on 16 Sep 2011, 07:53 AM
Hello Mark,

Try the following server side code to achieve your scenario.
C#:
protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
   {
       CheckBox chk=(CheckBox) sender;
       GridDataItem item = (GridDataItem)chk.NamingContainer;
       TextBox txt=(TextBox)   item.FindControl("Text1");
       txt.Text = "Your text Here";
   }

Thanks,
Shinu.
Tags
Grid
Asked by
pmourfield
Top achievements
Rank 1
Answers by
Jayesh Goyani
Top achievements
Rank 2
pmourfield
Top achievements
Rank 1
Mark Wilbanks
Top achievements
Rank 1
Shinu
Top achievements
Rank 2
Share this question
or