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

Set selected value of Dropdown list inside Usercontrol

7 Answers 1312 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Damodar
Top achievements
Rank 1
Damodar asked on 25 Aug 2014, 03:49 AM
Hi,

In a page we have RadGrid which contain a usercontrol. Inside usercontrol we have Dropdown.
We are binding dropdown when we are modifying radgrid.

Before updating to 2014.2.618.45 it was working fine.

But now it's giving error.
While debugging found that items are present in the dropdown but findbytext is unable to find the item.

ddlAssign.SelectedValue = ddlAssign.Items.FindByText(Name).Value;


Best Regards,
Damodar

7 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 25 Aug 2014, 05:19 AM
Hi Damodar,

I guess you are having a UserControl in EditItemTemplate of a RadGrid and you want to set the SelectedValue for the DropDownList in Edit mode. Please try the following code snippet.

ASPX:
<telerik:GridTemplateColumn HeaderText="ShipCountry" DataField="ShipCountry">
    <ItemTemplate>
        <%#Eval("ShipCountry") %>
    </ItemTemplate>
    <EditItemTemplate>
        <uc1:DropDownList ID="DropDownList1" runat="server" />
    </EditItemTemplate>
</telerik:GridTemplateColumn>

C#:
protected void rgrdSample_ItemDataBound(object sender, GridItemEventArgs e)
{
 if (e.Item is GridEditableItem && e.Item.IsInEditMode)
  {
   GridEditableItem editItem = (GridEditableItem)e.Item;
   UserControl MyUserControl = (UserControl)editItem.FindControl("DropDownList1");
   RadDropDownList rddlCountry = (RadDropDownList)MyUserControl.FindControl("rddlShipCountry");
   rddlCountry.SelectedValue = DataBinder.Eval(editItem.DataItem, "ShipCountry").ToString(); 
  }
}

Please elaborate on your requirement if this doesn't help and provide your code snippet.

Thanks,
Shinu
0
Damodar
Top achievements
Rank 1
answered on 25 Aug 2014, 06:21 AM
Hi Shinu,

In our radDetails_ItemCommand in aspx page we are loading usercontrol like below 

protected void radDetails_ItemCommand(object sender, GridCommandEventArgs e)
        {
            if (e.CommandName == RadGrid.EditCommandName)
            {
                e.Item.OwnerTableView.IsItemInserted = false;
                e.Item.OwnerTableView.EditFormSettings.UserControlName = PageReferences.WorkFlowAddToConsultant;
            }            
        }
And in ascx page we are loading dropdown and setting selected value as below.
protected void WorkFlowAddToConsultant_DataBinding(object sender, System.EventArgs e)
        {           
            DataTable dt = process.GetWorkflowNoteList();
            ddlAssign.DataSource = dt;
            ddlAssign.DataBind();
            ddlAssign.SelectedValue = ddlAssign.Items.FindByText("Damodar").Value;
        }

And we are getting error in ddlAssign.Items.FindByText("Damodar").Value; line even though dt contains Damodar.

Let me know if you are not clear.
Do we have to load items to dropdown in ItemDataBound or can we load in ItemCommand?

Best Regards,
Damodar
0
Shinu
Top achievements
Rank 2
answered on 26 Aug 2014, 05:42 AM
Hi Damodar,

You can populate the DropDownList in the user control page and set its selected text/value from the main page in RadGrid's ItemDatabound event.

C#:
protected void rgrdSample_ItemDataBound(object sender, GridItemEventArgs e)
{
  if (e.Item is GridEditableItem && e.Item.IsInEditMode)
  {
   GridEditableItem editItem = (GridEditableItem)e.Item;
   UserControl MyUserControl = (UserControl)e.Item.FindControl(GridEditFormItem.EditFormUserControlID);
   DropDownList ddlAssign = (DropDownList)MyUserControl.FindControl("ddlAssign");         
   ddlAssign.SelectedValue= ddlAssign.Items.FindByText("Damodar").Value;
  }
}

Thanks,
Shinu
0
Damodar
Top achievements
Rank 1
answered on 26 Aug 2014, 11:21 PM
Hi Shinu,

Thanks!
It can't find Damodar from the list. Check attachment.
But it only finds first in the list. i.e. ala... 
Why the previous version works fine and new version is not working?

Best Regards,
Damodar
0
Shinu
Top achievements
Rank 2
answered on 27 Aug 2014, 04:24 AM
HI Damodar,

I was not able to replicate this issue at my end. I'm using version 2014,2,618,40 and below is the sample code snippet and the screenshot of the output.

ASPX:
<telerik:RadGrid ID="rgrdSample" runat="server" AutoGenerateColumns="false" DataSourceID="SqlDataSource1" AllowPaging="true" AutoGenerateEditColumn="true" AllowSorting="true" OnItemDataBound="rgrdSample_ItemDataBound">
    <MasterTableView DataKeyNames="OrderID" CommandItemDisplay="Top">
        <Columns>
            <telerik:GridBoundColumn UniqueName="OrderID" DataField="OrderID" HeaderText="OrderID">
            </telerik:GridBoundColumn>
            <telerik:GridBoundColumn DataField="ShipCity" HeaderText="ShipCity" UniqueName="ShipCity" />
        </Columns>
    </MasterTableView>
</telerik:RadGrid>

C#:
protected void Page_Load(object sender, EventArgs e)
{
    rgrdSample.ItemCommand += new GridCommandEventHandler(rgrdSample_ItemCommand);
}
 
void rgrdSample_ItemCommand(object sender, GridCommandEventArgs e)
{
    if (e.CommandName == RadGrid.EditCommandName)
    {
        e.Item.OwnerTableView.IsItemInserted = false;
        e.Item.OwnerTableView.EditFormSettings.EditFormType = GridEditFormType.WebUserControl;
        e.Item.OwnerTableView.EditFormSettings.UserControlName = "~/RadGrid8/WebUserControl.ascx";
    }
}
 
protected void rgrdSample_ItemDataBound(object sender, GridItemEventArgs e)
{
    if (e.Item is GridEditableItem && e.Item.IsInEditMode)
    {
        GridEditableItem editItem = (GridEditableItem)e.Item;
        UserControl MyUserControl = (UserControl)e.Item.FindControl(GridEditFormItem.EditFormUserControlID);
        DropDownList rddlCountry = (DropDownList)MyUserControl.FindControl("rddlShipCountry");
        rddlCountry.SelectedValue = rddlCountry.Items.FindByText("Berlin").Value;
    }
}

ASCX:
<asp:DropDownList ID="rddlShipCountry" runat="server" OnDataBinding="rddlShipCountry_DataBinding">
</asp:DropDownList>

ASCX.CS:
protected void rddlShipCountry_DataBinding(object sender, EventArgs e)
{
    rddlShipCountry.DataSourceID = "SqlDataSource1";
    rddlShipCountry.DataTextField = "ShipCity";
    rddlShipCountry.DataValueField = "ShipCity";
}

Thanks,
Shinu
0
Damodar
Top achievements
Rank 1
answered on 28 Aug 2014, 05:44 AM
Hi Shinu,

I have following code: You can add any data in radgridview.

ASPX.CS:
protected void radDetails_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
    {
 
    }
protected void radDetails_ItemCommand(object sender, GridCommandEventArgs e)
    {
     if (e.CommandName == RadGrid.EditCommandName)
        {
       e.Item.OwnerTableView.IsItemInserted = false;
            e.Item.OwnerTableView.EditFormSettings.EditFormType = GridEditFormType.WebUserControl;
            e.Item.OwnerTableView.EditFormSettings.UserControlName = "~/UCDropDown.ascx";
        }
 }
protected void radDetails_ItemDataBound(object sender, GridItemEventArgs e)
    {       
        if (e.Item is GridEditableItem && e.Item.IsInEditMode)
        {
            GridEditableItem editItem = (GridEditableItem)e.Item;
            UserControl MyUserControl = (UserControl)e.Item.FindControl(GridEditFormItem.EditFormUserControlID);
            DropDownList rddlCountry = (DropDownList)MyUserControl.FindControl("rddlShipCountry");
            //rddlCountry.SelectedValue = rddlCountry.Items.FindByText("Damodar").Value;
        }
 
    }
protected void radDetails_UpdateCommand(object sender, GridCommandEventArgs e)
    {
        GridEditFormItem item = (GridEditFormItem)e.Item;
        UserControl userControl = (UserControl)e.Item.FindControl(GridEditFormItem.EditFormUserControlID);
        int id = Convert.ToInt32(item.GetDataKeyValue("BookingWorkflowID"));
        string userName = Convert.ToString((userControl.FindControl("rddlShipCountry") as DropDownList).SelectedItem.Value);       
        radDetails.Rebind();
    }
protected void radDetails_DetailTableDataBind(object sender, GridDetailTableDataBindEventArgs e)
    {
        GridDataItem dataItem = (GridDataItem)e.DetailTableView.ParentItem;       
    }
ASPX:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="GridWithUserControl.aspx.cs" Inherits="GridWithUserControl" %>
 
<%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %>
<!DOCTYPE html>
 
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <telerik:RadScriptManager ID="RadScriptManager1" runat="server">
            <Scripts>
                <asp:ScriptReference Assembly="Telerik.Web.UI" Name="Telerik.Web.UI.Common.Core.js"></asp:ScriptReference>
                <asp:ScriptReference Assembly="Telerik.Web.UI" Name="Telerik.Web.UI.Common.jQuery.js"></asp:ScriptReference>
                <asp:ScriptReference Assembly="Telerik.Web.UI" Name="Telerik.Web.UI.Common.jQueryInclude.js"></asp:ScriptReference>
            </Scripts>
        </telerik:RadScriptManager>
        <div>           
            <asp:UpdatePanel ID="upnl" runat="server">
                <ContentTemplate>
                    <telerik:RadGrid ID="radDetails" runat="server" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False"
                        OnDetailTableDataBind="radDetails_DetailTableDataBind" OnItemCommand="radDetails_ItemCommand" OnNeedDataSource="radDetails_NeedDataSource"
                        OnUpdateCommand="radDetails_UpdateCommand" OnItemDataBound="radDetails_ItemDataBound" ShowStatusBar="True" Width="95%" PageSize="100" DataSourceID="SqlDataSource1">
                        <HeaderStyle HorizontalAlign="Center" />
                        <MasterTableView AllowMultiColumnSorting="True" Name="Details" Width="100%" datasourceid="SqlDataSource1">
                            <Columns>
                                <telerik:GridBoundColumn DataField="Priority" HeaderText="Priority" FilterControlAltText="Filter Priority column" SortExpression="Priority" UniqueName="Priority" >                               
                                    <ColumnValidationSettings>
                                        <ModelErrorMessage Text="" />
                                    </ColumnValidationSettings>
                                </telerik:GridBoundColumn>
                                <telerik:GridBoundColumn DataField="DisplayDate" HeaderText="DisplayDate" DataType="System.DateTime" FilterControlAltText="Filter DisplayDate column" SortExpression="DisplayDate" UniqueName="DisplayDate" >
                                    <ColumnValidationSettings>
                                        <ModelErrorMessage Text="" />
                                    </ColumnValidationSettings>
                                </telerik:GridBoundColumn>
                                <telerik:GridEditCommandColumn ButtonType="PushButton" UniqueName="EditCommandColumn" EditText="Assign To Consultant" />
                            </Columns>
                            <EditFormSettings EditFormType="WebUserControl" EditColumn-Display="false" UserControlName="UCDropDown.ascx">
                                <EditColumn UniqueName="EditColumn"></EditColumn>
                            </EditFormSettings>
                            <PagerStyle AlwaysVisible="True" />
                        </MasterTableView>
                    </telerik:RadGrid>
                    <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:OzConnectionString %>" SelectCommand="SELECT TOP (10) RP.Priority, BB.DisplayDate FROM Booking.Workflow AS BB INNER JOIN Reference.Priority AS RP ON BB.PriorityID = RP.PriorityID"></asp:SqlDataSource>
                </ContentTemplate>
            </asp:UpdatePanel>
        </div>
    </form>
</body>
</html>
ASCX.cs
protected void Page_Load(object sender, EventArgs e)
    {
 
    }
    protected void rddlShipCountry_DataBinding(object sender, EventArgs e)
    {
        Dictionary<string,string> distItems = new Dictionary<string,string>();
        distItems.Add("Adel","Adel");
        distItems.Add("Alba", "Alba");
        distItems.Add("Bri","Bri");
        distItems.Add("Bunda", "Bunda");               
        distItems.Add("Cairn", "CNS");
        distItems.Add("Damodar", "Damodar");
         
        DataTable dtCities = new DataTable();
        dtCities.Columns.Add("Name");
        dtCities.Columns.Add("Code");  
        dtCities.TableName = "Name";
        foreach (var item in distItems)
        {
            DataRow dr = dtCities.NewRow();
            dr["Name"] = item.Key;
            dtCities.Rows.Add(dr);
        }
        rddlShipCountry.DataSource = dtCities;
        rddlShipCountry.DataTextField = "Name";
        rddlShipCountry.DataValueField = "Code";
    }
ASCX
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="UCDropDown.ascx.cs" Inherits="UCDropDown" %>
<asp:DropDownList ID="rddlShipCountry" runat="server" OnDataBinding="rddlShipCountry_DataBinding" DataTextField="Name" DataValueField="Code">
</asp:DropDownList>

And I am getting the same null error. Have removed Update panel but still it is the same.

Best Regards,
Damodar
0
Shinu
Top achievements
Rank 2
answered on 28 Aug 2014, 06:35 AM
Hi Damodar,

You are setting the SelectedValue to the Value field of that dropdown which is the Code column, but you have not bound the values to it. Please correct the code as below.

ASCX.CS:
foreach (var item in distItems)
 {
   DataRow dr = dtCities.NewRow();
   dr["Name"] = item.Key;
   //Bind values to Code
   dr["Code"] = item.Key;
   dtCities.Rows.Add(dr);            
 }

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