Wednesday, 15 February 2012

c# - Binding Data text field, Data value field to asp:Dropdownlist -



c# - Binding Data text field, Data value field to asp:Dropdownlist -

i have created helper function bind drop downwards list in asp.net. see function:

public void bindddl(string query, dropdownlist ddl) { list<issuetype> obj = new list<issuetype>(); issuetype iss = new issuetype(); iss.deptid = 1; iss.issue = "sss"; iss.issuetypeid = 4; obj.add(iss); //balissue bl = new balissue(); //list<issuetype> objsource = null; //objsource = bl.bind_issuetypes(query); ddl.datasource = obj; ddl.datavaluefield = convert.tostring(obj[0]); ddl.datatextfield = convert.tostring(obj[1]); ddl.databind(); }

in way if send query name , dropdownlist id function, drop downwards should binded list of issuetype entity, can see properties of issuetype in code.

but not able set datavaluefield , datatextfield correctly. every time saying index out of range.

datavaluefield , datatextfield should names of field in collection, not values collection.

ddl.datasource = obj; ddl.datavaluefield = "issue"; //example take needed ddl.datatextfield = "issuetypeid"; //example take needed ddl.databind();

the exception thrown because in collection have 1 item, seek set sec item(not exist) in datatextfield. not relevant if prepare code.

c# asp.net

No comments:

Post a Comment