Click here to Skip to main content
6,295,667 members and growing! (12,377 online)
Email Password   helpLost your password?
Web Development » ASP.NET » General     Beginner License: The Microsoft Public License (Ms-PL)

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

By Kareem.Ammer

to fix problem of using DataBind() Function in detailsView Control
C# (C# 2.0), .NET (.NET 2.0), ASP.NET, Dev
Posted:3 Apr 2008
Views:13,036
Bookmarked:7 times
Unedited contribution
Announcements
Loading...
 
Search    
Advanced Search
printPrint   Broken Article?Report       add Share
  Discuss Discuss   Recommend Article Email
13 votes for this article.
Popularity: 2.23 Rating: 2.00 out of 5
1 vote, 25.0%
1
2 votes, 50.0%
2

3

4
1 vote, 25.0%
5

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)

About the Author

Kareem.Ammer


Member
I'm Kareem Ammer Solution developer @ AlfanarIT Company @ KSA,graduated from Faculty of Comupter And Information Helwan university, Information system department.
Occupation: Web Developer
Location: Egypt Egypt

Other popular ASP.NET articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 10 of 10 (Total in Forum: 10) (Refresh)FirstPrevNext
GeneralThis is Not a good way Pinmembersuresh00822:00 25 May '08  
GeneralRe: This is Not a good way PinmemberKareem.Ammer20:53 8 Jun '08  
GeneralEither i'm missing something here... PinsitebuilderShog918:04 3 Apr '08  
GeneralRe: Either i'm missing something here... PinmemberKareem.Ammer3:52 4 Apr '08  
GeneralRe: Either i'm missing something here... PinmemberMike Ellison6:29 4 Apr '08  
GeneralRe: Either i'm missing something here... PinmemberKareem.Ammer20:42 4 Apr '08  
GeneralRe: Either i'm missing something here... PinmemberMike Ellison8:23 7 Apr '08  
GeneralRe: Either i'm missing something here... PinmemberKareem.Ammer11:09 7 Apr '08  
GeneralRe: Either i'm missing something here... PinmemberMike Ellison19:59 7 Apr '08  
GeneralRe: Either i'm missing something here... [modified] PinmemberKareem.Ammer9:06 8 Apr '08  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 3 Apr 2008
Editor:
Copyright 2008 by Kareem.Ammer
Everything else Copyright © CodeProject, 1999-2009
Web13 | Advertise on the Code Project