简体   繁体   English

如何在asp.net中保存下拉列表选择的索引?

[英]How to save the drop down list selected index in asp.net?

i want that after clicking a button saves the index i have selected in order to storage that number to my database. 我希望单击按钮后保存我选择的索引,以便将该数字存储到我的数据库中。 My drop-down-list is in a comment below. 我的下拉列表在下面的评论中。

The drop-down-list is filled by using a query. 通过使用查询来填充下拉列表。 And so far what it does, is that after clicking the button it storages the first index (which is 0), even when i want to save another index. 到目前为止,它所做的是,单击按钮后,即使我要保存另一个索引,它也会存储第一个索引(为0)。

I have done some research, and so far i cannot solve it, hope someone can help me out. 我已经做过一些研究,但到目前为止我还无法解决,希望有人可以帮助我。

Code for Dropdown list:- 下拉列表代码:-

<asp:DropDownList ID="ddlCategory" runat="server" Height="25px" Width="428px" Font-Size="Large"> </asp:DropDownList> 

here's how i fill the drop-down-list 这是我填写下拉列表的方式

ClsUtil clsutil = new ClsUtil(); 
DataSet ds = clsutilerias.FillDDL(Category); 
ddlCategory.DataTextField = ds.Tables[0].Columns["Name"].ToString(); 
ddlCategory.DataValueField = ds.Tables[0].Columns["Category_ID"].ToString();
ddlCategory.DataSource = ds.Tables[0]; 
ddlCategory.DataBind(); 

The DropDownList has properties. DropDownList具有属性。 You need to set SelectedIndex , SelectedItem or SelectedValue properly (and in the correct event in relation to when you do the binding etcetera). 您需要正确设置SelectedIndexSelectedItemSelectedValue (并且在与绑定等有关的正确事件中)。

Edit : Maybe you updated your question, or maybe I misunderstood, but either way it seems now that you don't want to set the SelectedIndex perhaps as much as get the SelectedIndex . 编辑 :也许您更新了您的问题,或者也许我误解了,但是无论哪种方式,现在看来您不想设置 SelectedIndex可能与获取 SelectedIndex一样多。 Even so, if you read the documentation and look at the examples provided in the links, you should have enough information to know what to do. 即使这样,如果您阅读文档并查看链接中提供的示例,则您应该具有足够的信息来知道该怎么做。

Basically, one of the <option> elements in the <select> on the client (the HTML code) will be selected when the data is posted back to the server. 基本上,当数据回传到服务器时,将选择客户端上的<select>中的<option>元素之一(HTML代码)。 If your <asp:DropDownList> is AutoPostBack="True" you can go for a callback in the OnSelectedIndexChanged event, but there are other ways too, with or without auto-post-back. 如果您的<asp:DropDownList>AutoPostBack="True" ,则可以在OnSelectedIndexChanged事件中进行回调,但是也可以使用其他方式,无论是否具有自动回发。 (You don't really need the view state, or even the <asp:DropDownList> at all, but can trigger a POST by submitting the form data in any way that suits your needs or preferences, and then read any values your interested in server-side.) (您实际上根本不需要视图状态,甚至根本不需要<asp:DropDownList> ,但是可以通过以适合您需要或偏好的任何方式提交表单数据来触发POST,然后读取您感兴趣的任何值服务器端。)

It seems that you found your solution in checking IsPostback in the Page , and not re-populate the list if the value is true . 看来你找到了你的解决方案在检查IsPostbackPage ,如果该值不重新填充列表true That's good. 那很好。 If you don't need to data-bind every time you render the page (even after a POST), that's a viable and common solution. 如果您不需要每次渲染页面时都进行数据绑定 (即使在POST之后),那么这是一种可行且常见的解决方案。

Another option, of course, would be to read the posted data in any event that happens before you do the data-binding . 当然,另一种选择是在执行数据绑定之前发生的任何情况下读取已发布的数据。 You may also want to handle the value of SelectedIndex to re-select the correct option in your drop-down after having populating it again , if you don't like having ASP.NET doing it for you. 如果您不想让ASP.NET为您完成此操作,则可能还需要处理SelectedIndex的值,以便在再次填充后在下拉列表中重新选择正确的选项。

在将数据源绑定到组合时,请为组合框提及DataMemberDataValue属性,以便从组合中选择元素,您将获得selectedValue作为DataValue

Well i have solved my problem, my problem was that when i clicked the button, the pageLoad method executed again, and there was where i had the code to fill my drop-down-list. 好吧,我已经解决了我的问题,我的问题是,当我单击按钮时,再次执行pageLoad方法,并且在那里有代码填充我的下拉列表。 so i just added: 所以我刚刚补充说:

if(!IsPostBack)
{
 //here goes the code to fill the Drop down list
}

When you are clicking the button, the entire page is refreshing that why you got the first index all the time The best solution is : 当您单击按钮时,整个页面都在刷新,这就是为什么您始终获得第一个索引的最佳解决方案是:

if(IsPostBack == false)
{
//Save your data code here
}

You have other tricky solution which is : put the dropboxmenu inside an update panel control 您还有其他棘手的解决方案:将dropboxmenu放在更新面板控件中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用asp.net中的另一个下拉列表更改下拉列表选择的索引和可见性? - How to change drop down list selected index and visibility using another drop down list in asp.net? asp.net中下拉列表的选定索引更改事件 - Selected Index change event of drop down list in asp.net asp.net:设置为下拉列表选择的默认值 - asp.net : set default value selected for a drop down list 无法在asp.net的下拉列表中显示选定的值 - Not able to display selected value in drop down list in asp.net 索引更改不起作用 asp.net 中的下拉列表 - On index change not working drop down list in asp.net 如何根据ASp.Net Gridview的Selected列填充下拉列表? - How to populate Drop down list depending on the Selected column of a ASp.Net Gridview? 如何获取转换后的布尔值以在asp.net gridview的可编辑下拉列表中显示为选定值? - How to get converted boolean value to show as selected value in the editable drop down list in asp.net gridview? 如何访问ASP.net模板字段下拉列表中的选定值 - How to access selected values in ASP.net template field drop down list 如何在asp.net mvc3中获取下拉列表的选定值? - how to get selected value of drop down list in asp.net mvc3? ASP.Net中的下拉列表,如何将其绑定到列表的属性 - Drop Down List in ASP.Net, how to bind it to a property of a list
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM