Click here to Skip to main content
Email Password   helpLost your password?

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");

}

You must Sign In to use this message board.
 
 
Per page   
 FirstPrevNext
GeneralThis is Not a good way
suresh008
22:00 25 May '08  
This is Not a good way to bind the values and Text to the drop down list..
The best way is to use Collections and Lists..you can use that

sureshkumaran

GeneralRe: This is Not a good way
Kareem.Ammer
20:53 8 Jun '08  
Dear sureshkumaran,
this article not for binding but for cover a specific error in special case

Kareem Ammer

GeneralEither i'm missing something here...
Shog9
18:04 3 Apr '08  
...or you just demonstrated how to use a loop. Suspicious


Citizen 20.1.01
'The question is,' said Humpty Dumpty, 'which is to be master - that's all.'

GeneralRe: Either i'm missing something here...
Kareem.Ammer
3:52 4 Apr '08  
i just demonstrate how to cover this error "Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control." beacuse using DataBind() function

Kareem Ammer

GeneralRe: Either i'm missing something here...
Mike Ellison
6:29 4 Apr '08  
Actually, Shog is right. What you're doing isn't binding at all. The point of binding is to have the control itself automatically loop through the datasource and populate itself accordingly, rather than have to write the looping code yourself. DataBinding is built-in so developers don't have to write all the looping plumbing like what you've demonstrated in this article.

Of course there's nothing wrong with your approach; you can write your own looping code if you prefer. I just wouldn't expect too many CP users to be that excited by it. Perhaps that's reflected in the ratings you've been getting?
GeneralRe: Either i'm missing something here...
Kareem.Ammer
20:42 4 Apr '08  
thanx for your replay but as i told before to shog that i present a solution for that error "Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control." because if you try to use DataBind() function for the DropDownList Control in the DetailsView the previous error will appear to you,
commonly i dont present a new method for binding but just for solve the error

Kareem Ammer

GeneralRe: Either i'm missing something here...
Mike Ellison
8:23 7 Apr '08  
I really don't understand where you are getting a problem though. On many occasions I've databound a dropdown list in the context of a GridView or DetailsView without ever seeing the error you're describing. Is it that you tried executing DataBind() on the control without first setting its datasource?

Perhaps your article would be improved were you to post the code you created initially which led to the error... then, maybe the solution would make sense. I've been trying to recreate the error under what I would consider normal databinding conditions and have had no such problems. The dropdown list databinds in a detailsView template just fine without custom code.
GeneralRe: Either i'm missing something here...
Kareem.Ammer
11:09 7 Apr '08  
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 Databind() or Eval()function to selectedValue property

after that try to assign dataSource to the DropDownList and Use DataBind() function and if it works send the code to me

thanx

Kareem Ammer

GeneralRe: Either i'm missing something here...
Mike Ellison
19:59 7 Apr '08  
Kareem.Ammer wrote:
after that try to assign dataSource to the DropDownList and Use DataBind() function and if it works send the code to me

Right....

I have no trouble using a dropdownlist in a template in a detailsView control, as I mentioned before. I have no idea what you're using for a datasource. I have no idea why you are running into the error you are, but were I you, I'd be looking into it further to understand better what's going on with your code.

In any event, Shog's original point remains true: your article is an example of bypassing the built-in databinding by performing your own looping code. I'm not saying you can't do it - it's your code, make it work however you want. I return to my suggestion though that this isn't the type of thing that would get the typical CP community member that excited.
GeneralRe: Either i'm missing something here... [modified]
Kareem.Ammer
9:06 8 Apr '08  
please Mike open the attached File of the article and exchange the code of FillLookUpCombo() function to be


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();
cmb.DataSource = dt;
cmb.DataTextField = NameValue;
cmb.DataValueField = IdValue;
cmb.DataBind();


}

and run the application again and click Edit or Insert, by simply way and without suggestions and discussions I solve the error that will appear to you
the article used just in this special case Laugh .

Kareem Ammer
modified on Wednesday, April 9, 2008 3:18 PM


Last Updated 3 Apr 2008 | Advertise | Privacy | Terms of Use | Copyright © CodeProject, 1999-2010