|
code
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Would like to get two items from a dropdownlistI 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, eric Eric,
> I do not see (easily) how I might get the cust id to display to my text there are two ways to look at this. you either do this using a> box after the dropdownlist is clicked,etc. 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 Eric W. Holzapfel wrote: Show quoteHide quote > 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, > eric 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 Show quoteHide quote "Mike MacMillan" wrote: >Eric, > >> I do not see (easily) how I might get the cust id to display to my text >> box after the dropdownlist is clicked,etc. > > 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 > >Eric W. Holzapfel wrote: >> 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, >> eric >
programmatically copy table to another application
What functionality does Infragistics controls offer, which don't come with VS .NET 2005? grid postback Code to select item in dropdown populated using TableAdapter Referencing controls inside Wizard control newbie to creating web controls Custom Treenodes in Treeview Closing window/page Problem in Setting SelectedValue for DropdownList in Form Adding a Directive to a dynamically created Template Column on gri |
|||||||||||||||||||||||