简体   繁体   English

如何从后面的代码填充动态添加的html select控件?

[英]How to populate dynamically added html select controls from code behind?

I've been programming in asp.net and c# for a week or so, please bear with me. 我已经在asp.net和C#中编程了大约一个星期,请耐心等待。

I'm trying to do a receipt generator in asp c# that looks kinda like this: 我正在尝试在asp c#中创建一个收据生成器,看起来有点像这样:

收据

the blue button adds a new row to the table with this javascript function: 蓝色按钮使用此javascript函数将新行添加到表中:

function nuevo(){
$("#tablaUsuarios").append("<tr>" +
    "<td><select name='listaConceptos' id='listaConceptos' runat='server'></select></td>" +
    "<td><input name='precio' id='precio' type='text' size='15' runat='server' readonly/></td>" +
    "<td><input type='button' value='Eliminar' onclick='eliminar(this)' class='btn btn-danger eliminar'></td>"+
    "</tr>")
}

However since I'm adding the controls via javascript on runtime, I have no access to them from the code behind like I would if they were DropDownList controls, adding runat="server" is also useless since at runtime I can't turn them into server controls, is there any way to access the control so I can populate, set data sources, etc. as if it were a DropDownList? 但是,由于我是在运行时通过JavaScript添加控件的,因此无法像后面的代码那样访问它们,就像它们是DropDownList控件一样,添加runat =“ server”也是没有用的,因为在运行时我无法打开它们进入服务器控件,是否有任何方法可以访问该控件,以便我可以像填充DropDownList一样填充,设置数据源等? Or any other way to dynamically generate and populate DDL? 还是通过其他方式动态生成和填充DDL?

I thought about having a hidden DropDownList populated with all the data I need from my database, and copying all its properties to the other select controls upon creation but I don't know if it would behave like a DDL with its DataValueFields and TextFields. 我曾考虑过要在隐藏的DropDownList中填充数据库中所需的所有数据,并在创建时将其所有属性复制到其他选择控件中,但是我不知道它的行为是否会像具有DataValueFields和TextFields的DDL一样。

Check out the link below. 查看下面的链接。 Accessing client side dynamic controls within ASP.NET codebehind 在ASP.NET代码背后访问客户端动态控件

You should be able to access the form elements by name on the server side like this: 您应该能够在服务器端按名称访问表单元素,如下所示:

var val = Request.Form["YOUR_CONTROLS_NAME"];

You will have to be responsible for naming the controls that you add with unique names though. 但是,您将必须负责使用唯一名称命名添加的控件。

Client side and server side code are two different things. 客户端和服务器端代码是两个不同的东西。 As far as I am aware there is no way to do what you are asking. 据我所知,您无法做任何您想做的事情。

What you can do if you want to populate a drop down list from a database for example, is to load the data into a hidden field or control and then use javascript to populate the data. 例如,如果要填充数据库中的下拉列表,您可以做的是将数据加载到隐藏字段或控件中,然后使用javascript填充数据。

Sounds rather complicated to me, maybe it would be easier to do everything server side and use update panels if you don't want an ugly postback. 听起来对我来说很复杂,如果您不希望进行丑陋的回发,那么在服务器端进行所有操作并使用更新面板可能会更容易。

Hard to tell what is best for you without fully seeing your project. 在没有完全看到您的项目的情况下很难说出什么是最适合您的。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM