Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Getting value of field in Dropdown List datasource within Formview 1

Status
Not open for further replies.

tjherman

Programmer
Joined
Jun 19, 2006
Messages
68
Location
US
I'm trying to get the value of one of the fields in a sqldatasource that fills a dropdown list within a Formview with ASP.NET 2.0 using Visual Studio (vb).

I want to be able to get the value of one of the fields in the datasource that is not the Datatextfield or the Datavalue field. (I need to populate a textbox control with this other value.)

I know how to get the datatextfield and datavaluefield values by doing the following:
Dim ddlCompanyName as DropDownList
Dim txtCompanyName as Textbox
ddlCompanyName = CType(formview1.Row.FindControl("ddlCompanyName"), DropdownList)
txtCompanyName = CType(formview1.row.FindControl("CompanyNameTextbox"),Textbox)
txtCompanyName.text = ddlCompanyName.Items(ddlCompanyName.SelectedIndex).Text

But I need to be able to get at another member field of the datasource to populate a different field

Can anyone give me some suggestions?

Thanks!
 
To be honest, I have maintained my dataset in session whenever I needed to do that. Then I queried my dataset for the value I needed using a filter. I never thought of anything faster and more clever.

Eva
 
Do you have any examples of how you maintain your dataset in session?
 
I just set it to a session variable:

session("dsKeep") = DataSetIWantToKeep
 
If you don't want to store it in session that way, you could use a blank input field and get reference to that as you do the other items then extract the value.
 
I guess my main problem is that I don't know how to extract the value from the dropdownlist sqldatasource if it's not the SelectedValue, SelectedIndex or SelectedText. When you mention using a blank input field--how would I get the value into it--I just don't know how to pull out this field from the datasource. I'm new to this--sorry!
 
Ok so you're filling a DropDownList with some data, and you have a DataValueField as the ID in your SQLDataSource, and the Description as your DataTextField, but there is another field in the row that that data comes from that you need information on.

What I do is write a Function to get me that data if I need it, as only those tow fields are accessbile through the DropDown binding.

(Please speak up anyone if I'm wrong)

So say you need to Address if the Company you slected in your dropdown.

Where you have the following code:
Code:
Dim ddlCompanyName as DropDownList
Dim txtCompanyName as Textbox
ddlCompanyName = CType(formview1.Row.FindControl("ddlCompanyName"), DropdownList)
txtCompanyName = CType(formview1.row.FindControl("CompanyNameTextbox"),Textbox)
txtCompanyName.text = ddlCompanyName.Items(ddlCompanyName.SelectedIndex).Text

You could then do something like (this will be in C# since that is my preferred language, apologies)
Code:
GetCompanyAddress(Convert.ToInt32(ddlComapnyName.Selectedvalue));

and my function would look like:

Code:
protected string GetCompanyAddress(int iCompanyID)
{

  // Do a database call to get the company address

  return strCompanyAddress;
}

Then do with it as you will.

Does this make sense?
 
Yes, it does. I guess I was just hoping that there was some other way, other than a call to the database, to grab the value of the field from within the sqldatasource.

Thanks very much for your time and help on this!
 
Thanks very much! I should be able to use the code in this article!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top