asp.net - .NET C# DropDownList pass parameter to function -
i don't know how word this...i have drop down list queries database , displays list of descriptions based on ids. want user able select description, push button , call getqueryresults() function id being passed , not description. getqueryresults(dropdownlist1.selectedvalue) doesn't work because of conflicting types. else can use?
protected void page_load(object sender, eventargs e) { dataconnector dc = new dataconnector(); dropdownlist1.datasource = dc.getcodetypes(); dropdownlist1.datavaluefield = "id"; dropdownlist1.datatextfield = "description"; dropdownlist1.databind(); } protected void button1_click(object sender, eventargs e) { dataconnector dc = new dataconnector(); gridview2.datasource = dc.getqueryresults(dropdownlist1.selectedvalue); //error: cannot convert string int gridview2.databind(); } public list<codedesc> getqueryresults(int searchterm) { try { string query = "select id, code, descr code_desc code_type_id = :searchterm"; //more stuff
my manager not want me change query string. (previously had used "select id, code, descr code_desc code_type_id = (select id code_desc descr = :searchterm);" searchterm string description eliminating need id)
start wrapping dropdownlist databinding inside ispostback
check.
if (!page.ispostback) { dropdownlist1.datasource = dc.getcodetypes(); dropdownlist1.datavaluefield = "id"; dropdownlist1.datatextfield = "description"; dropdownlist1.databind(); }
i not, every time load page data bound again , dropdownlist goes it's first value.
then can call getqueryresults
selectedvalue
converting value int
.
gridview2.datasource = dc.getqueryresults(convert.toint32(dropdownlist1.selectedvalue));
finally, google parameratized queries.
Comments
Post a Comment