Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

for only Cover Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control error

0.00/5 (No votes)
3 Apr 2008 4  
to fix problem of using DataBind() Function in detailsView Control

Download DataBind_DropDwonList - 4.66 KB

DataBind_DropDwonList

Introduction

this article for sovling the problem of DataBinding DropDownList in the templates of DetailsView Control, and if you try to use DataBind() function the next error will be appear to you "Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control."

Note that:The Error will appear with next scenario "if you try to drag and drop DropDownList in Edit or Insert Template in detailsview control and right click on the DropDownList and select "Edit databinding()" and use Bind() or Eval()function to selectedValue property after that try to assign dataSource to the DropDownList and Use DataBind() function.

The Function For Binding

the code below describes how the binding occur without using DataBind() Function, it is very simple by adding initiate a new instances of ListItem Class with the needed data

public static void FillLookUpCombo(System.Web.UI.WebControls.DropDownList cmb, System.Data.DataTable dt, Insert_NewItem oEnum, string IdValue, string NameValue)

{

cmb.DataSource = null;

cmb.Items.Clear();

switch (oEnum)

{

case Insert_NewItem.All:

cmb.Items.Add(new ListItem("---All---", "-1"));

break;

case Insert_NewItem.Select:

cmb.Items.Add(new ListItem("---Select---", "-1"));

break;

}

foreach (DataRow r in dt.Rows)

{

cmb.Items.Add(new ListItem(r[NameValue].ToString(), r[IdValue].ToString()));

}

}
     

The Event For Calling The Function

The code below is the event ItemCreated that contain the code of finding the DropDown List control from the DetailsView Control and Filling it by using above function FillLookUpCombo()

protected void DetailViewEmployee_ItemCreated(object sender, EventArgs e)

{

DropDownList ddlDepartment = (DropDownList)DetailViewEmployee.FindControl("ddlDepartment");

if (ddlDepartment != null)

Employee.FillLookUpCombo(ddlDepartment, dtDepartment, Employee.Insert_NewItem.Select,"ID","Name");

}

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here