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

Cant able to make, work the dropdownlist/radcombobox to populate a textbox inside a radgrid row

1 Answer 86 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Sri
Top achievements
Rank 1
Sri asked on 05 Mar 2014, 10:55 PM
Hello there,
                   My requirement is that each row in radgrid, has a textbox and a dropdownlist inside the item template. When a  value is selected in the dropdownlist, it should populate the textbox with the selected value.
                  I tried using the dropdownlist_SelectedIndexChanged, it does populates the textbox but as not expected.For exmaple, if the radgrid contains 3 rows, ifi  i select a value in the dropdownlist at row 1, it populates the textbox at row 1 and also the textbox on other rows with the corresponding selected value of the dropdownlist in that row. I just want the action to take place in the particular row, and not in all the rows.
                 I also tried using  the client side function and made the Autopostback=false. But it doesn't work as well. $find("<%= rgUserAttendance.ClientID %>"); -->always returns null.
                 Any help on this would be much appreciated.

.ascx
--------
<script type="text/javascript" >

 function OnSelectedIndexChange(rowindex,radGridClientID)
    {
         var grid = $find("<%= rgUserAttendance.ClientID %>");
         if(grid)
       {
            alert('grid found');
            var MasterTable = grid.get_masterTableView();
       }
       else 
       {
            alert('grid not found');
       } 
      
      var row = MasterTable.get_dataItems()[rowindex];
    }
</script>
<table width="900px" align="left">
<tr id="trGrid" runat="server" visible="true">
        <td class="bodyCopy">
            <radG:RadGrid ID="rgUserAttendance" runat="server" OnItemDataBound="rgUserAttendance_ItemDataBound"
                Skin="Inox" GridLines="None" AutoGenerateColumns="False" Width="100%" UseEmbeddedScripts="False">
                <MasterTableView DataKeyNames="EM_ID,EM_moduleID">
                    <Columns>
                        <radG:GridBoundColumn DataField="MDL_Code" HeaderText="Code" UniqueName="MDL_Code">
                            <ItemStyle VerticalAlign="Top" />
                        </radG:GridBoundColumn>
                        <radG:GridBoundColumn DataField="MDL_Name" HeaderText="Module" UniqueName="MDL_Name">
                            <ItemStyle VerticalAlign="Top" />
                        </radG:GridBoundColumn>
                        <radG:GridBoundColumn DataField="storeName" HeaderText="Store" UniqueName="storeName">
                            <ItemStyle VerticalAlign="Top" />
                        </radG:GridBoundColumn>
                        <radG:GridTemplateColumn UniqueName="AttendedColumn" HeaderText="Attended">
                            <ItemTemplate>
                                <asp:CheckBox ID="chkAttended" runat="server" Checked='<%# Eval("EM_isAttended") %>' />
                            </ItemTemplate>
                            <ItemStyle VerticalAlign="Top" />
                        </radG:GridTemplateColumn>
                        <radG:GridTemplateColumn HeaderText="Competent" UniqueName="TemplateColumn">
                            <ItemStyle HorizontalAlign="Center" VerticalAlign="Top" Width="100px" />
                            <ItemTemplate>
                                <asp:CheckBox ID="chkCompetent" runat="server" Checked='<%# Eval("EM_isCompetent") %>' />
                            </ItemTemplate>
                            <EditItemTemplate>
                                <asp:TextBox runat="server"></asp:TextBox>
                            </EditItemTemplate>
                        </radG:GridTemplateColumn>
                        <radG:GridTemplateColumn UniqueName="RPLColumn" HeaderText="RPL">
                            <ItemTemplate>
                                <asp:CheckBox ID="chkRPLColumn" runat="server" />
                            </ItemTemplate>
                            <ItemStyle VerticalAlign="Top" />
                        </radG:GridTemplateColumn>
                        <radG:GridTemplateColumn UniqueName="ScoreColumn" HeaderText="Pass %">
                            <ItemTemplate>
                                <asp:TextBox ID="txtScore" runat="server" Width="35px" CssClass="bodyCopy" Text='<%# Eval("EM_score") %>' />
                                <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" CssClass="bodyCopy"
                                    ControlToValidate="txtScore" ErrorMessage=" * Not valid" ValidationExpression="^\d+(\.\d{1,2})?$" />
                            </ItemTemplate>
                            <ItemStyle VerticalAlign="Top" />
                        </radG:GridTemplateColumn>
                        <radG:GridTemplateColumn UniqueName="UserFeedbackColumn" HeaderText="User Feedback"
                            Visible="False">
                            <ItemTemplate>
                                <asp:TextBox ID="txtUserFeedback" runat="server" Columns="30" Rows="3" TextMode="MultiLine"
                                    CssClass="bodyCopy" Text='<%# Eval("EM_userFeedback") %>' />
                            </ItemTemplate>
                        </radG:GridTemplateColumn>
                        <radG:GridTemplateColumn UniqueName="TrainerFeedbackColumn" HeaderText="Trainer Comments">
                            <ItemTemplate>
                                <asp:TextBox ID="txtTrainerFeedback" runat="server" Columns="30" Rows="3" TextMode="MultiLine"
                                    CssClass="bodyCopy" />
                            </ItemTemplate>
                        </radG:GridTemplateColumn>
                        <radG:GridTemplateColumn UniqueName="CommentColumn" HeaderText="Select Comments">
                            <ItemTemplate>
                                <asp:DropDownList ID="ddlComment" runat="server" AutoPostBack="false" />
                               <!-- <asp:DropDownList ID="ddlComment" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddlComment_SelectedIndexChanged"  /> -->
                            </ItemTemplate>
                        </radG:GridTemplateColumn>
                        <radG:GridTemplateColumn UniqueName="TestColumn" HeaderText="Test Comments">
                            <ItemTemplate>
                                <radCom:RadComboBox ID="rcbComment" runat="server" AutoPostBack="false" OnClientSelectedIndexChanged="rcbComment_SelectedIndexChanged">
                                    <Items>
                                        <radCom:RadComboBoxItem Text="1" Value="1" />
                                        <radCom:RadComboBoxItem Text="2" Value="2" />
                                        <radCom:RadComboBoxItem Text="3" Value="3" />
                                    </Items>
                                </radCom:RadComboBox>
                            </ItemTemplate>
                        </radG:GridTemplateColumn>
                    </Columns>
                    <HeaderStyle Font-Bold="True" CssClass="shutter" HorizontalAlign="Left" />
                    <ExpandCollapseColumn Visible="False">
                        <HeaderStyle Width="19px" />
                    </ExpandCollapseColumn>
                    <RowIndicatorColumn Visible="False">
                        <HeaderStyle Width="20px" />
                    </RowIndicatorColumn>
                </MasterTableView>
                <ClientSettings AllowColumnHide="True" >
                    <ClientEvents OnGridCreated="GetGridObject"></ClientEvents>
                </ClientSettings>
            </radG:RadGrid>
        </td>
    </tr>
</table>

ascx.cs
-----------
  protected void rgUserAttendance_ItemDataBound(object sender, GridItemEventArgs e)
    {
        if (e.Item is GridDataItem)
        {
            GridDataItem item = (GridDataItem)e.Item;
            DropDownList ddl = item.FindControl("ddlComment") as DropDownList;
     Hashtable hshComment = new Hashtable();
            hshComment.Add("0", "--Select Comment--");
            hshComment.Add("1", "Comment 1");
            hshComment.Add("2", "Comment 2");
            hshComment.Add("3", "Comment 3");

            //populating the dropdownlist
            foreach (DictionaryEntry entry in hshComment)
            {
                ListItem itemComment = new ListItem();
                itemComment.Text = entry.Value.ToString();
                itemComment.Value = entry.Key.ToString();

                ddl.Items.Add(itemComment);

            }
            ddl.Attributes.Add("onChange", "OnSelectedIndexChange('" + item.ItemIndex + "','" + this.rgUserAttendance.ClientID + "' );");
          }
    }

 public void ddlComment_SelectedIndexChanged(object sender, EventArgs e)
    {
        DropDownList dropdownlist1 = (DropDownList)sender;
        GridDataItem item = (GridDataItem)dropdownlist1.NamingContainer;

        TextBox txtTrainerFeedback = (TextBox)item.FindControl("txtTrainerFeedback");


        switch (Convert.ToInt32(dropdownlist1.SelectedValue))
        {
            case 1:
                txtTrainerFeedback.Text += " " + FrameWorkSettings.SessionAttendanceComment1;
                break;
            case 2:
                txtTrainerFeedback.Text += " " + FrameWorkSettings.SessionAttendanceComment2;
                break;
            case 3:
                txtTrainerFeedback.Text += " " + FrameWorkSettings.SessionAttendanceComment3;
                break;
            default:
                txtTrainerFeedback.Text += " ";
                break;
        }
    }

 














1 Answer, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 06 Mar 2014, 09:39 AM
Hi Sri,

Please have a look into the sample code snippet which works fine at my end.

ASPX:
<telerik:RadGrid ID="RadGrid1" runat="server" DataSourceID="SqlDataSource1">
    <MasterTableView>
        <Columns>
            <telerik:GridTemplateColumn>
                <ItemTemplate>
                    <asp:TextBox ID="TextBox1" runat="server">
                    </asp:TextBox>
                </ItemTemplate>
            </telerik:GridTemplateColumn>
            <telerik:GridTemplateColumn>
                <ItemTemplate>
                    <telerik:RadComboBox ID="RadComboBox1" runat="server" EmptyMessage="select" OnClientSelectedIndexChanged="OnClientSelectedIndexChanged">
                        <Items>
                            <telerik:RadComboBoxItem Text="Item1" />
                            <telerik:RadComboBoxItem Text="Item2" />
                            <telerik:RadComboBoxItem Text="Item3" />
                            <telerik:RadComboBoxItem Text="Item4" />
                        </Items>
                    </telerik:RadComboBox>
                </ItemTemplate>
            </telerik:GridTemplateColumn>
        </Columns>
    </MasterTableView>
</telerik:RadGrid>

JavaScript:
<script type="text/javascript">
    function OnClientSelectedIndexChanged(sender, args) {
        var node = getElement(sender.get_element());
        var foundElement = null;
        while (node) {
            foundElement = $telerik.findElement(node, "TextBox1");
            if (foundElement)
                break;
            node = getElement(node.parentNode);
        }
        if (foundElement)
            foundElement.value = args.get_item().get_text();
    }
    function getElement(element) {
        return Telerik.Web.UI.Grid.GetFirstParentByTagName(element, "tr");
    }
</script>

Thanks,
Shinu.
Tags
Grid
Asked by
Sri
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Share this question
or