如何从代码后面动态添加动态添加的html选择控件?

时间:2022-11-19 09:09:35

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#中做一个收据生成器,看起来有点像这样:

如何从代码后面动态添加动态添加的html选择控件?

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? Or any other way to dynamically generate and populate DDL?

但是因为我在运行时通过javascript添加控件,所以如果它们是DropDownList控件,我无法从后面的代码访问它们,添加runat =“server”也没用,因为在运行时我无法转动它们进入服务器控件,有没有办法访问控件,所以我可以填充,设置数据源等,就好像它是一个DropDownList?或者以其他方式动态生成和填充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一样。

2 个解决方案

#1


0  

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.

如果没有完全看到你的项目,很难说出最适合你的东西。

#2


1  

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.

您必须负责为使用唯一名称添加的控件命名。

#1


0  

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.

如果没有完全看到你的项目,很难说出最适合你的东西。

#2


1  

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.

您必须负责为使用唯一名称添加的控件命名。