如何在运行时将数据添加到下拉列表并自动在下拉列表中重新绑定 [英] How to add data to dropdownlist at runtime and rebind in dropdownlist automatically
问题描述
我想知道我的要求:
我有一个asp.net网页输入书籍详细信息,用户从下拉列表中选择书籍作者。有一个作者母版页,我们输入所有作者姓名。但是假设有一个新作者不在下拉列表中,那么我想让用户选择从书籍输入页面和该作者输入新的作者姓名插入数据库并自动添加到下拉列表,以便用户可以选择该作者。请建议我如何完成这件事。
我尝试过:
我想我们可以使用web API。但不知道怎么做。
我同意F-ES Sitecore的内容。你可以做的是在你的DropDownList
中添加一个选项Others,一旦选择了该选项,你就可以放置一个TextBox $
面板中的c $ c>和
/按钮
Div
对它们进行分组,然后触发要在页面中显示的Div
。如果您正在使用ASP.NET WebForms,则有两种方法可以执行此操作。
第一个选项是使用服务器端实现,那就是处理DropDownList_SelectedIndexChanged
事件以及该事件,您可以检查其他值并将TextBox
显示到用户可以输入数据的页面,然后在Button_Click
事件,您可以编写代码将该数据插入数据库。您不需要Web API来实现这一点,除非您的数据来自不同的服务器。
第二个选项是使用客户端实现,可能是plainJavaScript
或jQuery
。您需要创建Web API或页面方法或Web服务以通过AJAX调用访问您的数据。以下是使用普通JavaScript
的简单示例:
HTML:
< 选择 名称 = color onchange =' checkvalue(this.value)' >
< < span class =code-leadattribute>选项 > 选择作者< / option >
< 选项 值 = 1 > 您< / option >
< 选项 value = 2 > 我< / option >
< 选项 value = 其他人 > 其他< / option >
< / select >
< < span class =code-leadattribute> div id = divNewEntry style =' display:none' >
< 输入 类型 = text id = txtNewAuthor / >
< 输入 type = 按钮 id = btnSave value < span class =code-keyword> = 保存 / >
< / div >
JavaScript:
function checkvalue(val)
{
if (val === 其他)
document .getElementById(' divNewEntry')。 .display = ' block';
else
document .getElementById(' divNewEntry')。style.display = ' 无跨度>;
}
完成后,您可以使用jQuery AJAX与您的服务器(API)进行通信在Button的Click事件中插入数据库,如下所示:
( document )。ready( function (){
( #btnSave')。click( function (){
Hi,
I want to get an idea for my requirement:
I have a asp.net web page to enter book details where user select book author from dropdownlist. There is a author master page from where we enter all the author names.But suppose there is a new author that is not in the dropdownlist then i want to give the user option to enter the new author name from the book entry page and that author gets inserted in database and added to dropdown automatically so that user can select that author. Please suggest me how to get this done.
What I have tried:
I think we can use web API for this..But no idea how to do this.
I agree with what F-ES Sitecore. What you can do is to add an optione "Others" in yourDropDownList
and once that option is selected, then you can place aTextBox
and aButton
in aPanel
/Div
to group them and then trigger thatDiv
to be shown in the page. You have two options to do that if you are working on ASP.NET WebForms.
First option is using Server-side implementation, and that is to handle theDropDownList_SelectedIndexChanged
event and on that event, you can check for the "Others" value and show yourTextBox
to the page where users can input data and then atButton_Click
event, you can write a code to insert that data into your database. You don't need a Web API to implement this not unless if your data is coming from different server.
The second option is using Client-side implementation, could be plainJavaScript
orjQuery
. And you need to create a Web API or Page Method or Web Service to access your data via AJAX call. Here's a quick example using plainJavaScript
:
HTML:
<select name="color" onchange='checkvalue(this.value)'> <option>Select Authors</option> <option value="1">You</option> <option value="2">Me</option> <option value="others">Others</option> </select> <div id="divNewEntry" style='display:none'> <input type="text" id="txtNewAuthor" /> <input type="button" id="btnSave" value="Save" /> </div>
JavaScript:
function checkvalue(val) { if(val==="others") document.getElementById('divNewEntry').style.display='block'; else document.getElementById('divNewEntry').style.display='none'; }
Once you've done that, you can then use jQuery AJAX to communicate with your server (API) to insert the database at Button's Click event something like this:
(document).ready(function () {
('#btnSave').click(function () {
这篇关于如何在运行时将数据添加到下拉列表并自动在下拉列表中重新绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!