当前位置: 首页 > 编程语言 > C#

下拉列表选择项文本总是返回第一个项目文本Share

时间:2023-04-10 14:17:07 C#

下拉列表选择项文本总是返回第一个项目文本publicvoidfillcountry(){BLobj=newBL();DataSetds=obj.dss("select*fromCountry");drplistcountry.DataSource=ds;drplistcountry.DataTextField="国家名称";drplistcountry.DataValueField="CountryId";drplistcountry.DataBind();drplistcountry.Items.Insert(0,newListItem("--Select--","0"));我在页面load()事件中使用这个fillcountry()。在按钮Click事件和reselectselected上it.textdrplistcountry总是显示第一个索引文本,如何解决?在.aspx页面中:对于Dropdownlist控件,将EnableViewState属性设置为true。在.aspx.cs页面中:在PageLoad事件中检查以下内容:if(!IsPostBack){fillcountry();}试试这个代码你只需要调用这个函数一次。publicvoidfillcountry(){BLobj=newBL();DataSetds=obj.dss("select*fromCountry");drplistcountry.DataSource=ds;drplistcountry.DataTextField="国家名称";drplistcountry.DataValueField="CountryId";drplistcountry.DataBind();drplistcountry.Items.Insert(0,newListItem("--Select--","0"));drplistcountry.SelectedIndex=drplistcountry.Items.IndexOf(drplistcountry.Items.FindByText("--Select--"));}一种简单的方法,您可以使用转发器在这样的html代码中进行选择'>从数据库中获取数据使用ado.netpublicvoidfillAlbum(){SqlConnectionmyconnection=newSqlConnection("DataSource=.;InitialCatalog=person;IntegratedSecurity=真");SqlDataAdaptermyadapter=newSqlDataAdapter("从类别中选择CatID,标题",myconnection);DataTabledt=newDataTable();myadapter.Fill(dt);rpt.DataSource=dt;rpt.DataBind();}并获取选定的值。选择元素名称是("cat")所以stringcat=Request.Form["Cat"];并简单地存储到数据库SqlConnectionmyconnection=newSqlConnection("DataSource=.;InitialCatalog=person;IntegratedSecurity=True");SqlCommandmycommand=newSqlCommand("插入图片(Title,Pic,CatID)values(@pname,@pic,@cat)",myconnection);mycommand.Parameters.AddWithValue("pname",txtname.Text.Trim());mycommand.Parameters.AddWithValue("图片",路径);stringcat=Request.Form["Cat"];mycommand.Parameters.AddWithValue("cat",cat.ToString());我的连接。打开();我的命令.ExecuteNonQuery();我的连接。关闭();它非常简单但很难找到,请检查您的值Datavaluefield的所有项目不应为空或null。意思是:ddl_Name.DataTextField="NAME";ddl_Name.DataValueField="滚动";如果该值为空,则表示您无法获取第一项。DropdownList选定的项目文本值将像这样工作。字符串值=“印度”;drplist.SelectedIndex=drplist.Items.IndexOf(drplist.Items.FindByText(值));我也面临同样的问题,但在我的情况下,所有项目的下拉值都相同,这导致了问题。因此,请确保正确绑定下拉列表。?以上是C#学习教程:下拉列表选中的itemtext总是返回第一个itemtext共享的全部内容。收藏不代表立场,如涉及侵权,请点击右侧联系管理员删除。如需转载请注明出处: