简体   繁体   English

在C#和ASP.NET中将数组(类型:var)传递给_Click事件

[英]deliver an array (type: var) to an _Click-event in C# and ASP.NET

I'm getting really frustrated as I'm trying to get an array to my _Click-Event in my ASP.NET-Application. 当我试图在我的ASP.NET应用程序中将数组添加到_Click-Event时,我感到非常沮丧。

Here's the thing: I have an dynamicly created "form", which is a todolist. 事情是这样的:我有一个动态创建的“表单”,它是一个待办事项列表。 So for every todo, I have an description (from the database), a note-field and a checkbox. 因此,对于每个待办事项,我都有一个描述(来自数据库),一个注释字段和一个复选框。

Since their could be 0-n todos, I can't work with the asp:-Tag in the ascx-File. 由于它们可能是0-n个待办事项,因此我无法使用ascx-File中的asp:-Tag。

That's why I created an class for each todo, which looks like this: 这就是为什么我为每个待办事项创建一个类,原因如下:

private class todoContainer 
    { 
        public int? id 
        { 
            get; 
            set; 
        } 

        public CheckBox activeted 
        { 
            get; 
            set; 
        }
[...]

The list gets created with an foreach-loop and every toDo goes into the array "todoArray". 该列表由一个foreach循环创建,每个toDo进入数组“ todoArray”。 Here's how I work with the Controls inide the loop: checkboxArea.Controls.Add(todoArray[todoArrayInt].activeted); 这是我如何在循环中使用控件的方法:checkboxArea.Controls.Add(todoArray [todoArrayInt] .activeted);

Since I now want to send the list to the submit-event with all the changes I made (for examples if I want to add a note to toDo X and want to cross off toDo Y), I have to get the whole array in my _Click-method. 由于我现在想将所有所做的更改发送到提交事件列表(例如,如果我想给toDo X添加注释并想与toDo Y交叉),我必须在我的整个数组中_Click方法。 Unfortunately I can't find a way to accomplish this... 不幸的是,我找不到实现此目标的方法...

I tried this: 我尝试了这个:

  • Get the whole array into an asp:HiddenField-Controll, which doesn't work at all 将整个数组放入一个asp:HiddenField-Controll中,这根本不起作用
  • Get the array into a session, but than I can't find a way how to track changes... 将数组放入会话中,但是我找不到如何跟踪更改的方法...

If anybody could help me, I would be realy thankfull. 如果有人可以帮助我,我将非常感激。

Best regards Richard 最好的问候理查德

If you need a collection of checkboxes each one associated with a different TODO, try using a CheckBoxList control. 如果您需要一组复选框,每个复选框都与一个不同的TODO相关联,请尝试使用CheckBoxList控件。

CheckBoxList 复选框列表

Using this control you can associate each TODO in your array (no need to store a reference to the checkbox) with a checkbox item using the Value property of each item to store the id. 使用此控件,您可以使用每个项目的Value属性来存储数组中的每个TODO(无需存储对复选框的引用)与复选框项目。

Retrieving the TODO's again in the onclick event would be as simple as iterating over the CheckBoxList Items collection (CheckBoxes) and checking the Value property to determine which CheckBox belonged to which TODO. 在onclick事件中再次检索TODO就像遍历CheckBoxList Items集合(CheckBoxes)并检查Value属性以确定哪个CheckBox属于哪个TODO一样简单。

how about using a asp:repeater control? 如何使用asp:repeater控件?

on your submit button click event you should be able to loop through every item in your asp:repeater 在您的“提交”按钮单击事件上,您应该能够遍历asp:repeater中的每个项目

foreach(RepeaterItem item in repeater1.Items) foreach(repeater1.Items中的RepeaterItem项目)
{ {

  TextBox box = (TextBox)item.FindControl("Value"); string b = box.Text; } 

//do stuff here after looping through repeater //在通过中继器循环之后在这里做一些事情

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

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