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

access asp:dropdownlist in radgrid

2 Answers 121 Views
Grid
This is a migrated thread and some comments may be shown as answers.
areen
Top achievements
Rank 1
areen asked on 14 Feb 2011, 11:28 PM
I'm trying to populate a dropdownlis using the selectedindexchanged event of another dropdownlist. the ddl i'm trying to put data in is enclosed in a radgrid. how to do i access the ddl to bind data to it programmatically? i'm attaching my code below, but

DropDownList channels = (DropDownList)RadGrid1.MasterTableView.FindControl("ddlChannels");

is the line of code causing my problem.

any help would be appreciated. thanks.

    <telerik:RadGrid id="RadGrid1" runat="server"
        AllowSorting="True" DataSourceID="SqlDataSource1"
        GridLines="None" AutoGenerateColumns="False" AllowPaging="True"
        PageSize="20" OnItemDeleted="RadGrid1_ItemDeleted" OnItemCreated="RadGrid1_ItemCreated" OnNeedDataSource="RadGrid1_NeedDataSource"
        OnItemInserted="RadGrid1_ItemInserted" OnInsertCommand="RadGrid1_InsertCommand" OnUpdateCommand="RadGrid1_UpdateCommand" OnItemUpdated="RadGrid1_ItemUpdated">
<MasterTableView datakeynames="ChannelId,FmisServerId,PriorityId,StatusLevelId" CommandItemDisplay="TopandBottom" datasourceid="SqlDataSource1">
<CommandItemSettings ExportToPdfText="Export to Pdf"></CommandItemSettings>
 
<RowIndicatorColumn FilterControlAltText="Filter RowIndicator column">
<HeaderStyle Width="20px"></HeaderStyle>
</RowIndicatorColumn>
 
<ExpandCollapseColumn FilterControlAltText="Filter ExpandColumn column">
<HeaderStyle Width="20px"></HeaderStyle>
</ExpandCollapseColumn>
 
    <Columns>
        <telerik:GridEditCommandColumn FilterControlAltText="Filter EditCommandColumn column">
        </telerik:GridEditCommandColumn>
        <telerik:GridBoundColumn DataField="IpAddress"
            FilterControlAltText="Filter IpAddress column" HeaderText="FMIS Server"
            SortExpression="IpAddress" UniqueName="IpAddress">
        </telerik:GridBoundColumn>
        <telerik:GridBoundColumn DataField="ChannelSubId"
            FilterControlAltText="Filter ChannelSubId column" HeaderText="Channel ID"
            SortExpression="ChannelSubId" UniqueName="ChannelSubId" DataType="System.Int32">
        </telerik:GridBoundColumn>
    </Columns>
 
<EditFormSettings EditFormType="Template">
    <FormTemplate>
        <table border="0" cellpadding="1" cellspacing="2" width="100%" rules="none" style="border-collapse: collapse; background: white;" id="table1">
            <tr>
                <td>FMIS Server:</td>
                <td>
                    <asp:DropDownList ID="ddlFmisServer" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddlFmisServer_SelectedIndexChanged">
                    </asp:DropDownList>
                </td>
            </tr>
            <tr>
                <td>Channel ID:</td>
                <td>
                    <asp:DropDownList ID="ddlChannels" runat="server">
                    </asp:DropDownList>
                </td>
            </tr>
        </table>
    </formtemplate>
</EditFormSettings>
</MasterTableView>
 
<FilterMenu EnableImageSprites="False"></FilterMenu>
 
<HeaderContextMenu CssClass="GridContextMenu GridContextMenu_Default"></HeaderContextMenu>
    </telerik:RadGrid>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server"
        ConnectionString="<%$ ConnectionStrings:MgmtServerConnectionString %>"
         
        SelectCommand="SELECT Channel.ChannelId, Channel.IsActive, Channel.IsAvailable, Channel.Timeout, Channel.ChannelSubId, FmisServer.IpAddress, Priority.Description, StatusLevels.StatusLevel, FmisServer.FmisServerId, StatusLevels.StatusLevelId, Priority.PriorityId FROM Channel INNER JOIN FmisServer ON Channel.FmisServerId = FmisServer.FmisServerId INNER JOIN Priority ON Channel.PriorityId = Priority.PriorityId INNER JOIN StatusLevels ON Channel.StatusLevelId = StatusLevels.StatusLevelId ORDER BY FmisServer.IpAddress, Channel.ChannelSubId">
    </asp:SqlDataSource>
protected void ddlFmisServer_SelectedIndexChanged(object source, EventArgs e)
{
 
    DropDownList fmisServers = (DropDownList)source;
 
    if (fmisServers != null)
    {
        int intIndex = fmisServers.SelectedIndex;
 
        try
        {
            // call a method that returns all channel ids and sub ids as a dataset
            DataSet dsChannelSubIds = new DataSet();
            string strErrorText = string.Empty;
            MgmtServerAdmin.App_Code.AdminLibrary.GetAllChannelsbyFmisServer(intIndex, out dsChannelSubIds, out strErrorText);
 
            DropDownList channels = (DropDownList)RadGrid1.MasterTableView.FindControl("ddlChannels");
 
            // clear list to guard against duplicate entries
            channels.Items.Clear();
 
            // bind new DropDownList to the dataset
            channels.DataSource = dsChannelSubIds;
            channels.DataTextField = "ChannelSubId";
            channels.DataValueField = "ChannelId";
            channels.DataBind();
 
        }
        catch (Exception ex)
        {
            //SaveErrorTrackingOnTable(MgmtServerLib.Library_ValidateRequestError_Error, ex.Message);
            throw ex;
        }
    }
}

2 Answers, 1 is accepted

Sort by
0
Accepted
Shinu
Top achievements
Rank 2
answered on 15 Feb 2011, 06:45 AM
Hello Areen,

You can access the required DropDownList by using the NamingContainer property of the  DropDownList ddlFmisServer. Here is the sample code.
C#:
protected void ddlFmisServer_SelectedIndexChanged(object sender, EventArgs e)
 {
     DropDownList ddlServer=(DropDownList)sender;
     GridEditFormItem item = (GridEditFormItem)ddlServer.NamingContainer;
     DropDownList ddl = (DropDownList)item.FindControl("ddlChannels");
 }

Thanks,
Shinu.
0
areen
Top achievements
Rank 1
answered on 15 Feb 2011, 09:23 PM
thanks! works like a dream.
Tags
Grid
Asked by
areen
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
areen
Top achievements
Rank 1
Share this question
or