Groups | Blog | Home
all groups > asp.net webcontrols > december 2005 >

asp.net webcontrols : Would like to get two items from a dropdownlist


Eric W. Holzapfel
12/30/2005 1:25:22 PM
Hello Webcontrol Group members,

I am using Visual Studio .net (2003) and webcontrols.
I have a dropdownlist webcontrol that uses the "datatextfield" and "
"datavaluefield" .

the text field is a company name, and the datavalue field is an index
(like 1,2,3,etc).

I can display the company name in the dropdown fine (loading from a
stored procedure using the dataset and dataadapter things, etc, from sql
server). Currently I am using a datareader to load the items
(code below)
while (dreader.Read())
{
ddSelCompany.Items.Add(new ListItem(dreader[1].ToString()));

if (firstrow)
{
custno.Text = dreader[0].ToString(); firstrow = false;
}
}
dreader.Close();
srConn.Close();

element 0 is the record number (or customer id), and element 1 is the
company name.


I would like to display both the company name and the id on my form,
using the dropdown for company name, and another textfield for the cust
id. I need the new id and the name to load in the database after the
user selects the company name, etc.

I do not see (easily) how I might get the cust id to display to my text
box after the dropdownlist is clicked,etc.

Can anyone offer some info here? (in Access, this was "easy").

Thanks,
Mike MacMillan
12/30/2005 5:45:11 PM
Eric,

[quoted text, click to view]

there are two ways to look at this. you either do this using a
postback, or not. i would recommend doing this on the frontend, using
javascript...as it won't require a postback. ill assume your
Dropdownlist's listItems have their name and value set to the company
name and its index. using javascript you can get the selected index on
the dropdownlist like this:

<script language="javascript">
function populateId(objDdl) {
var objTxt, strId;

//** grab the textbox
objTxt = document.getElementById("txtId");

//** get the id
strId = objDdl.options[objDdl.selectedIndex].value;

//** set the text
objTxt.value = strId
}
</script>

<select id="ddlStuff" onChange="populateId(this);">
<option value="">--
<option value="1">Item One
<option value="2">Item Two
<option value="3">Item Three
<option value="4">Item Four
<option value="5">Item Five
</select>
<input type="text" id="txtId">


if you need to handle it via postback, then just handle the
SelectedIndexChanged event of the DropDownList. it would be something
like:
public void OnSetId(object sender, EventArgs e) {
string strId;

//** get the selected items id
strId = ddlStuff.SelectedItem.Value.ToString();

//** set the text of the id textbox
txtTheId.Text = strId;
}

let me know if this helps,
Mike MacMillan

[quoted text, click to view]
Eric
1/1/2006 9:23:39 AM
Hello Mike,
Thanks for the tip. I have been not using .net for a while (and
asp/asp.net and javascript,etc) and am rusty to say the least. This
will help. I do not need to do a postback until the user clicks a
button to load all the data into the database.

Thanks again,

eric


[quoted text, click to view]



--
Sent via .NET Newsgroups
AddThis Social Bookmark Button