ASP.NET Web Controls >> DataValueField - postback

by Mike » Fri, 05 Sep 2003 05:23:38 GMT

I haven't been able to find any posts about this problem.
When I set the DataValueField to be something, the control
loses it's selected index when returning from the
postback. If I set only the DataTextField I have no
problems.

I set the DataTextField and DataValueFields like so:

ddlBU.DataTextField="BU"; -- each value is unique
ddlBU.DataValueField = "ORG_ID"; -- is 1 of 2 values

It seems that the because Org_Id is not unique in the
dataset it is screwing up the index of the control by
setting the SelectedIndex to be the first ListItem it
finds with a DataValueField matching that of the
originally selected item.

Anyone know of a work around ?

ASP.NET Web Controls >> DataValueField - postback

by Alvin Bruney » Mon, 08 Sep 2003 10:51:49 GMT


why don't you manually set the selected item to the value of the item you
are looking for
selectedindex = 9; for 10th item

Similar Threads

1. Extra "DataValueFields" in custom DropDownList

2. SelectionList DataValueField problem

I've got a set of code that is attempting to build a SelectionList.  The
values are not sequential.  I bind the control to a dataset in which the
values being passed are correct.  But the values that are entered into the
selectionlist are simple sequential indexes.

Am I missing something here?  Or is this a bug in data binding with the
SelectionList?

Here's the code:


daUnits.Fill(dsMyList);

selUnit.DataSource = dsUnitList;

selUnit.DataTextField = "sName";

selUnit.DataValueField = "Id";

selUnit.DataBind();



And here is an excerpt of the source data

<dbo_spListUnit>
    <Id>58</Id>
    <sCode>Unit1  </sCode>
    <sName>First Unit</sName>
</dbo_spListUnit>
<dbo_spListUnit>
    <Id>230</Id>
    <sCode>UnitC  </sCode>
    <sName>Second Unit</sName>
</dbo_spListUnit>
<dbo_spListUnit>
    <Id>119</Id>
    <sCode>UnitX  </sCode>
    <sName>Third Unit</sName>
</dbo_spListUnit>

The name appears fine in the list, but the values are consistently 0, 1, 2,
etc... sequential through the list, and do not include the "id" value from
the source data.

Thanks.


3. [expletive deleted] checkboxlist datavaluefield - ASP.NET Web Controls

4. DDL DataValueField does not set properly

Hi, I'm binding a DataSet to a DropDownList control which works fine. What
doesn't work is the DataValueField. Here's my code (C#) on the Page_Load
event:

SomeClass A = new SomeClass();
DataSet myDS = new DataSet();

myDS = A.GetEmployeesDS();      // the A object has a method which returns a
DataSet object
myDDL.DataSource = myDS;
myDDL.DataMember="EmployeesTable";            // the table name from which
data originates (do I need this?)
myDDL.DataTextField = "EmployeeName";         // column which contains text
to display
myDDL.DataValueField = "EmployeeID";            // column which contains a
unique ID
myDDL.DataBind();

The result is that the DDL's DataTextField is populated with the data from
the DataSet, but the DataValueField holds the value "EmployeeID" for all
items in the DDL. I'm not sure why it treats EmployeeID as a literal and
EmployeeName as a column/data. Even if I remove the line of code where I set
the DataMember, it displays the EmployeeName. Am I missing anything?

Thank you for you help.
Peter


5. View DataValueField - ASP.NET Web Controls

6. Listbox DataValueField

Hi,

Is it possible to do some action on the value that will be the
DataValueField of a listbox?

I have a listbox with a list of item. But I would like to encode the value
ID that the client will see in the html.
There is no way to do that on the database, it has to be done on the .net
side.

For instance:

I have
   SearchID.DataSource         = oReader;
   SearchID.DataValueField     = "Value";
   SearchID.DataTextField      = "Text";
   SearchID.DataBind            ();

And I would like to have something like:

   SearchID.DataSource = oReader;
   SearchID.DataValueField = EncodeID("Value");
   SearchID.DataTextField  = "Text";
   SearchID.DataBind();

EncodeID being whatever you want, just a simple function.

I dont want to loop through anything and would like if possible to keep the
databinding stuff.

Another option would be to response.write the listitem, but again, I want to
keep the four lines there and apply a function to it.

Thanks for your help.

Gerald



7. Checked List box - datavaluefield - ASP.NET Web Controls

8. Advantage of DataValueField

Dear All,

   I am actually a bit confused about DataValueField. I know what is the use 
of
   DataTextField.But dont know anything about DataValue Field.Is it necessary
   to use DataValue field whenever we bind any Dropdownbox or listBox.What
   if we dont use DataValue field at all and stick with only DataTextField.Has
   any 1 encountered any problems when not using DataValueField.What are the
   advantages and disadvantages of  DataValueField. Any help would be greatly
   appreciated.

   Cheers,
   Sunil Sabir