本文介绍了如何在下拉列表中检索值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我必须审核数据库中的更改。在大多数页面上,这是完美的,因为只有文本框 - 但是在我正在处理的页面上,使用下拉列表,我的代码不起作用。 < td > < asp:DropDownList ID = DropDownList4 runat = server DataSourceID = SqlDatacountry DataTextField = country_name DataValueField = country_id > ; < asp:SqlDataSource ID = SqlDatacountry runat = server ConnectionString = <% $ ConnectionStrings:songtypecons%> SelectCommand = SELECT * FROM [country_detail] > < / td > 代码落后: string sql1 = selectcust_fname,cust_mname,cust_lname,cust_birthdate,cust_gender,cust_address,cust_contact_num,cust_country,cust_state,cust_city,cust_zip from cust_detail where cust_id =' + ds。表[ filldata]。行[ 0 ]。ItemArray [ 0 ]。ToString()+ '; SqlDataAdapter adpt1 = new SqlDataAdapter(sql1,con); DataSet ds1 = new DataSet(); adpt1.Fill(ds1, custdata); if (ds1.Tables [ custdata ]。Rows.Count > 0 ) { for ( int d = 0 ; d < DropDownList4.Items.Count; d ++) { if (ds1.Tables [ custdata]。行[ 0 ]。ItemArray [ 7 ]。ToString()== DropDownList4.Items [d] .Text) { DropDownList4.Items [d] .Selected = true ; break ; } } } 解决方案 ConnectionStrings:songtypecons%> SelectCommand = SELECT * FROM [country_detail] > < / td > code背后: string sql1 = selectcust_fname,cust_mname,cust_lname,cust_birthdate,cust_gender,cust_address,cust_contact_num,cust_country,cust_state,cust_city,cust_zip from cust_detail where cust_id =' + ds.Tables [ filldata]。行[ 0 ]。项目数组[ 0 ]。ToString()+ ' ; SqlDataAdapter adpt1 = new SqlDataAdapter(sql1,con); DataSet ds1 = new DataSet(); adpt1.Fill(ds1, custdata); if (ds1.Tables [ custdata ]。Rows.Count > 0 ) { for ( int d = 0 ; d < DropDownList4.Items.Count; d ++) { if (ds1.Tables [ custdata]。行[ 0 ]。ItemArray [ 7 ]。ToString()== DropDownList4.Items [d] .Text) { DropDownList4.Items [d] .Selected = true ; break ; } } } 使用DropDownList的SelectedValue属性。 < asp:scriptmanager id = ScriptManager1 runat = server xmlns:asp = #unknown / > < asp:updatepanel runat = server id = UpdatePanel1 xmlns:asp = #unknown > < contenttemplate > < asp:dropdownlist runat = server id = ddlCaseFilesNew datasourceid = dsCaseFiles > DataTextField =显示DataValueField =FileIDOnPreRender =ddl_PreRenderWidth =300px AutoPostBack =trueOnSelectedIndexChanged =ddlCaseFilesNew_SelectedIndexChangedVisible =False> < asp:listitem > 项目1 < / asp:listitem > ; < / asp:dropdownlist > < / contenttemplate > < 触发器 > < asp:asyncpostbacktrigger controlid = ddlCaseFilesNew eventname = SelectedIndexChanged / > < / triggers > < / asp:updatepanel > < script runat = 服务器 > protected void ddlCaseFilesNew_SelectedIndexChanged(object sender,EventArgs e) { hidNewCaseFile.Value = ddlCaseFilesNew.SelectedItem.Value; } < / script > I must audit the changes into a database. On most pages this works perfect, as there were just textboxes - however on a page I am working on right now, with dropdownlists, my code isn''t working.<td> <asp:DropDownList ID="DropDownList4" runat="server" DataSourceID="SqlDatacountry" DataTextField="country_name" DataValueField="country_id"> <asp:SqlDataSource ID="SqlDatacountry" runat="server" ConnectionString="<%$ ConnectionStrings:songtypecons %>" SelectCommand="SELECT * FROM [country_detail]"></td>code behind:string sql1 = "selectcust_fname,cust_mname,cust_lname,cust_birthdate,cust_gender,cust_address,cust_contact_num,cust_country,cust_state,cust_city,cust_zip from cust_detail where cust_id ='" + ds.Tables["filldata"].Rows[0].ItemArray[0].ToString() + "' "; SqlDataAdapter adpt1 = new SqlDataAdapter(sql1, con); DataSet ds1 = new DataSet(); adpt1.Fill(ds1, "custdata"); if (ds1.Tables["custdata"].Rows.Count > 0) { for (int d = 0; d < DropDownList4.Items.Count; d++) { if (ds1.Tables["custdata"].Rows[0].ItemArray[7].ToString() == DropDownList4.Items[d].Text) { DropDownList4.Items[d].Selected = true; break; } } } 解决方案 ConnectionStrings:songtypecons %>" SelectCommand="SELECT * FROM [country_detail]"></td>code behind:string sql1 = "selectcust_fname,cust_mname,cust_lname,cust_birthdate,cust_gender,cust_address,cust_contact_num,cust_country,cust_state,cust_city,cust_zip from cust_detail where cust_id ='" + ds.Tables["filldata"].Rows[0].ItemArray[0].ToString() + "' "; SqlDataAdapter adpt1 = new SqlDataAdapter(sql1, con); DataSet ds1 = new DataSet(); adpt1.Fill(ds1, "custdata"); if (ds1.Tables["custdata"].Rows.Count > 0) { for (int d = 0; d < DropDownList4.Items.Count; d++) { if (ds1.Tables["custdata"].Rows[0].ItemArray[7].ToString() == DropDownList4.Items[d].Text) { DropDownList4.Items[d].Selected = true; break; } } }Use SelectedValue property of DropDownList.<asp:scriptmanager id="ScriptManager1" runat="server" xmlns:asp="#unknown" /> <asp:updatepanel runat="server" id="UpdatePanel1" xmlns:asp="#unknown"> <contenttemplate> <asp:dropdownlist runat="server" id="ddlCaseFilesNew" datasourceid="dsCaseFiles"> DataTextField="Display" DataValueField="FileID" OnPreRender="ddl_PreRender" Width="300px" AutoPostBack="true" OnSelectedIndexChanged="ddlCaseFilesNew_SelectedIndexChanged" Visible="False"> <asp:listitem>Item 1</asp:listitem> </asp:dropdownlist> </contenttemplate> <triggers> <asp:asyncpostbacktrigger controlid="ddlCaseFilesNew" eventname="SelectedIndexChanged" /> </triggers> </asp:updatepanel><script runat="server"> protected void ddlCaseFilesNew_SelectedIndexChanged(object sender, EventArgs e) { hidNewCaseFile.Value = ddlCaseFilesNew.SelectedItem.Value; }</script> 这篇关于如何在下拉列表中检索值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
09-03 01:58