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

Bind SelectedValue after populating DropDownList

4 Answers 1392 Views
Grid
This is a migrated thread and some comments may be shown as answers.
William
Top achievements
Rank 1
William asked on 15 Jun 2011, 07:09 PM
The popup edit window will not load when I have this line of code: "SelectedValue='<%# Bind("LocationID") %>".  My suspicion is because the DropDownList is populating after the control attempts to bind the SelectedValue, but I'm not sure. I have verified that the SelectedValue I am binding is in fact a value that is in the DropDownList. 

Form Template:

       <tr>
            <td>Location:</td>
            <td>
                <asp:DropDownList ID="ddlDevicesLocation" runat="server" SelectedValue='<%# Bind("LocationID") %>'>
                    
                </asp:DropDownList>
            </td>
        </tr>

C# Populate DropDown :: (OnItemDataBound)

protected void DevicesGrid_PopulateFields(object sender, GridItemEventArgs e)
{
            if ((e.Item is GridEditFormItem) && (e.Item.IsInEditMode))
            {
                GridEditFormItem editform = (GridEditFormItem)e.Item;

                // Define fields that we need to work with
                DropDownList locationddl = (DropDownList)editform.FindControl("ddlDevicesLocation");
                DropDownList typeddl = (DropDownList)editform.FindControl("typeddl");
                TextBox PrinterIP = (TextBox)editform.FindControl("PrinterIP");
                TextBox PrinterName = (TextBox)editform.FindControl("PrinterName");
                TextBox PrinterPort = (TextBox)editform.FindControl("PrinterPort");


                // Fill Location DropDown 
                LocationDropDown(locationddl);
}
}


4 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 16 Jun 2011, 07:29 AM
Hello William,

Since you are binding the DropDown In edit mode, the items are not available when rendering. One suggestion is to set the selected value from code behind itself.
C#:
protected void RadGrid1_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
{
if (e.Item is GridEditFormItem && e.Item.IsInEditMode)//popup&editform
        {
            DataRowView row = (DataRowView)e.Item.DataItem;
            DropDownList drp = (DropDownList)edititem.FindControl("ddlDevicesLocation");
            drp.DataSourceID ="SqlDataSource1";
            drp.DataTextField = "LastName";
            drp.SelectedValue = last;
            drp.DataBind();    
        }    
}

Thanks,
Shinu.
0
William
Top achievements
Rank 1
answered on 16 Jun 2011, 01:51 PM
Thank you for the response. Ideally I would like to bind the DropDownList before edit mode. Which event could I use to accomplish this, and how would I go about finding the control with that event?
0
Accepted
Jayesh Goyani
Top achievements
Rank 2
answered on 17 Jun 2011, 12:56 PM
hi,

<td>
    <asp:Lable ID="lblLocationID" runat="server" Text='<%# Eval("LocationID") %>'>
    <asp:DropDownList ID="ddlDevicesLocation" runat="server" SelectedValue='<%# Bind("LocationID") %>'>
    </asp:DropDownList>
</td>
protected void RadGrid1_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
{
if (e.Item is GridEditFormItem && e.Item.IsInEditMode && !e.Item.OwnerTableView.IsItemInserted)
        //if the item is about to edit
       {
           GridEditFormItem edititem = (GridEditFormItem)e.Item;
           DropDownList drp = (DropDownList)edititem.FindControl("ddlDevicesLocation");
           Label lblLocationID = (DropDownList)edititem.FindControl("lblLocationID");
           drp.DataSourceID ="SqlDataSource1";
           drp.DataTextField = "LastName";
           drp.DataBind(); 
            
           if(!String.IsnullorEmpty(lblLocationID.Text))
           drp.SelectedValue = lblLocationID.Text;
       }


if this LocationID is also a datakeyValue.
then edititem.getdatakeyvalue("LocationID").ToString();

let me know if any concern

Thanks,
Jayesh Goyani
0
William
Top achievements
Rank 1
answered on 17 Jun 2011, 01:54 PM
Thank you for your help, this solved my issue. I got the idea behind your code, but I did it a bit differently. See below:

<td>
<asp:Label ID="lblLocationID" runat="server" Visible="false" Text='<%# Eval("LocationID") %>' />
<asp:DropDownList ID="ddlDevicesLocation" runat="server" />
</td>

if ((e.Item is GridEditFormItem) && (e.Item.IsInEditMode))
//if the item is about to edit
{
    GridEditFormItem edititem = (GridEditFormItem)e.Item;
    DropDownList drp = (DropDownList)edititem.FindControl("ddlDevicesLocation");
    Label lblLocationID = (Label)edititem.FindControl("lblLocationID");
 
    // Populate DropDown
    LocationDropDown(drp);
 
    if (!String.IsNullOrEmpty(lblLocationID.Text))
        drp.SelectedValue = lblLocationID.Text;
}
Tags
Grid
Asked by
William
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
William
Top achievements
Rank 1
Jayesh Goyani
Top achievements
Rank 2
Share this question
or