Click here to Skip to main content
13,143,374 members (31,044 online)
Click here to Skip to main content
Add your own
alternative version

Stats

39.7K views
11 bookmarked
Posted 3 Apr 2008

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

, 3 Apr 2008
Rate this:
Please Sign up or sign in to vote.
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, along with any associated source code and files, is licensed under The Microsoft Public License (Ms-PL)

Share

About the Author

Kareem.Ammer
Software Developer (Senior) alfanarIT
Egypt Egypt
I'm Kareem Ammer Solution developer @ AlfanarIT Company @ KSA,graduated from Faculty of Comupter And Information Helwan university, Information system department.

You may also be interested in...

Pro

Comments and Discussions

 
GeneralThis is Not a good way Pin
suresh00825-May-08 21:00
membersuresh00825-May-08 21:00 
GeneralRe: This is Not a good way Pin
Kareem.Ammer8-Jun-08 19:53
memberKareem.Ammer8-Jun-08 19:53 
GeneralEither i'm missing something here... Pin
Shog93-Apr-08 17:04
sitebuilderShog93-Apr-08 17:04 
GeneralRe: Either i'm missing something here... Pin
Kareem.Ammer4-Apr-08 2:52
memberKareem.Ammer4-Apr-08 2:52 
GeneralRe: Either i'm missing something here... Pin
Mike Ellison4-Apr-08 5:29
memberMike Ellison4-Apr-08 5:29 
GeneralRe: Either i'm missing something here... Pin
Kareem.Ammer4-Apr-08 19:42
memberKareem.Ammer4-Apr-08 19:42 
GeneralRe: Either i'm missing something here... Pin
Mike Ellison7-Apr-08 7:23
memberMike Ellison7-Apr-08 7:23 
GeneralRe: Either i'm missing something here... Pin
Kareem.Ammer7-Apr-08 10:09
memberKareem.Ammer7-Apr-08 10:09 
GeneralRe: Either i'm missing something here... Pin
Mike Ellison7-Apr-08 18:59
memberMike Ellison7-Apr-08 18:59 
GeneralRe: Either i'm missing something here... [modified] Pin
Kareem.Ammer8-Apr-08 8:06
memberKareem.Ammer8-Apr-08 8:06 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.170915.1 | Last Updated 3 Apr 2008
Article Copyright 2008 by Kareem.Ammer
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid