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

Fill DropDownList in FormTemplate in EditMode

3 Answers 43 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Lukas
Top achievements
Rank 1
Lukas asked on 22 Jan 2014, 10:22 AM
Hello,

On my Website i have two RadGrids. One for Servers and one for Products. 
The ServerGrid got Columns like:
ID (PK)
CustomerID(FK)
Name
Location
IP
...

The ProductsGrid got Columns like:
ID (PK)
CustomerID(FK)
Name
Version
Server (based on the Column IP from ServerGrid)
...

Each Customer can have multiple Servers and Products. So when the Customer have 4 Servers and want to insert a new Product with "Add new Record" i have a FormTemplate with an DropDownList. All avaiable Servers(IP) for the customer are shown in this List. I fill this List in the ItemDatabound Event from the ProductsGrid:

if (e.Item is GridEditFormInsertItem && e.Item.OwnerTableView.IsItemInserted)
  {
    GridEditableItem item = e.Item as GridEditableItem;
    DropDownList ServerDDL = (DropDownList)item.FindControl("ddlServer");
    // Code to filter and get all ServerIP avaible for this Customer
    ServerDDL.Item.Add(Item);
  }


Code from the DropDownList in the FormTemplate:
<td>
  <asp:DropDownList ID="ddlServer" runat="server" SelectedValue='<%# Bind("Server") %>' TabIndex="5"   AppendDataBoundItems="True">
    <asp:ListItem Selected="True" Text="Select" Value="">
    </asp:ListItem>
  </asp:DropDownList>
</td>

This works perfekt for inserting a new Product. But when i want to Edit an Product i can“t fill the DropDownList with the required Items before the SelectedValue are set wich cause an RunTimeError in JavaScript saying that the DropDownList got an invalid SelectedValue because it is not avaible in the ElementList.

So my question is, how can i set the Items for this DropdownList before the Grid want to set the SelectedValue?

Kind regards
Lukas

3 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 22 Jan 2014, 12:57 PM
Hi Lukas,

I'm not clear about your requirement, i guess you are trying to bind the values to the DropDownList during edit mode. Please try the following code snippet:

ASPX:
<asp:DropDownList ID="ddlServer" runat="server" AppendDataBoundItems="True">
   <asp:ListItem Selected="True" Text="Select" Value="">
   </asp:ListItem>
</asp:DropDownList>

C#:
if (e.Item is GridEditableItem && e.Item.IsInEditMode)
{
    GridEditableItem edit = (GridEditableItem)e.Item;
    DropDownList drop = (DropDownList)edit.FindControl("ddlServer");
    drop.DataTextField = "Server";
    drop.DataValueField = "Server";
    drop.DataSourceID = "SqlDataSource1";
    drop.SelectedValue = DataBinder.Eval(edit.DataItem, "Server").ToString();
}

Thanks,
Princy
0
Lukas
Top achievements
Rank 1
answered on 22 Jan 2014, 01:30 PM
Hi Princy,

Thanks for your quick answer. I have implemented your code and the DropDownList is filled with the correct Items wich is great!

But when i want to save the Record the value is noch saving. Seems like the DropDownList is not bound to the Server Column.

By the way, i am not using the DataSourceID. I am using Items for this. Here is the code for this:

SqlDataAdapter Adapter = new SqlDataAdapter("Select IP FROM Server WHERE CustomerID =" + CustomerID, con as SqlConnection);
DataSet DDLDataset = new DataSet("Products");
Adapter.FillSchema(DDLDataset, SchemaType.Source, "Products");
Adapter.Fill(DDLDataset, "Products");
DataTable DDLTabe = DDLDataset.Tables["Products"];
                     
DropDownList ServerDDL = (DropDownList)item.FindControl("ddlServer");
ServerDDL.Items.Clear();
for (int i = 0; i < DDLTabe.Rows.Count; i++)
{
  ListItem Item = new ListItem();
  Item.Value = Convert.ToString(DDLTabe.Rows[i][0]);
  Item.Text = Convert.ToString(DDLTabe.Rows[i][0]);
 
  ServerDDL.Items.Add(Item);
}
ServerDDL.DataTextField = "Server";
ServerDDL.DataValueField = "Server";
ServerDDL.SelectedValue = DataBinder.Eval(item.DataItem, "Server").ToString();

The ASPX-Code for the DropDownList is the same from your example.

Kind regards
Lukas
0
Accepted
Princy
Top achievements
Rank 2
answered on 23 Jan 2014, 06:21 AM
Hi Lukas,

I tried your code and it works fine. You can bind the dropdownlist in the ItemDataBound event as shown in the previous post, and you can access the Updated values in the UpdateCommand as shown below:

C#:
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
{
   //Your code
    if (e.Item is GridEditableItem && e.Item.IsInEditMode)
    {
        GridEditableItem edit = (GridEditableItem)e.Item;
        DropDownList ServerDDL = (DropDownList)edit.FindControl("ddlServer");
        ServerDDL.Items.Clear();
        for (int i = 0; i < DDLTabe.Rows.Count; i++)
        {
            ListItem Item = new ListItem();
            Item.Value = Convert.ToString(DDLTabe.Rows[i][0]);
            Item.Text = Convert.ToString(DDLTabe.Rows[i][0]);
            ServerDDL.Items.Add(Item);
        }
        ServerDDL.DataTextField = "Server";
        ServerDDL.DataValueField = "Server";
        ServerDDL.SelectedValue = DataBinder.Eval(edit.DataItem, "Server").ToString();
    }
}
 
protected void RadGrid1_UpdateCommand(object sender, GridCommandEventArgs e)
{
    GridEditableItem edit = (GridEditableItem)e.Item;
    DropDownList ServerDDL = (DropDownList)edit.FindControl("ddlServer");
    string ServerDDLvalue = ServerDDL.SelectedValue;
    //Code to update to db
}

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