简体   繁体   English

使用C#在ASP.NET中选择动态创建的表行

[英]Selecting a dynamically created table row in ASP.NET with C#

I am writing a web application in Visual Studio 2015 pro using C# and ASP.NET. 我正在使用C#和ASP.NET在Visual Studio 2015 pro中编写Web应用程序。 Right now, I have it set up where the user will click a button and the C# code will go get a bunch of data then display it back to the user in tables. 现在,我在用户单击按钮的位置进行了设置,C#代码将获取一堆数据,然后在表中显示给用户。 I have spent a day of work trying to figure out how to add some form of a clickable event to the table rows but have had no success. 我花了一天的时间试图弄清楚如何向表行中添加某种形式的clickable事件,但是没有成功。 Ultimately, what I want to do is call a method in my C# code when the table row is clicked and send it the row index. 最终,我想做的是单击表行并将其发送给行索引时在C#代码中调用一个方法。

Here is the C# code I am using for generating the tables: 这是我用于生成表的C#代码:

    protected void searchButton_Click(object sender, EventArgs e)
    {
        try
        {
            // Remove the error display
            resultListLabel.Style.Add("Display", "None");

            // Get document groups 
            groups = TableCommands.getGroups(dbConn, "retriever", searchTextOne.Text, searchTextTwo.Text);

            foreach (var dataPair in groups)
            {                    
                // Get data pair group names into list
                List<string> name = dataPair.Key.Split('|').ToList();

                // ====== Make table
                Table resultTable = new Table();
                resultTable.Attributes["class"] = "displayTable";
                resultList.Controls.Add(resultTable);

                // ====== Add table info row
                TableRow groupInfo = new TableRow();
                groupInfo.Attributes["class"] = "groupInfoLabel";

                // add row to table
                resultTable.Rows.Add(groupInfo);                    

                // create cell with information
                TableCell infoCell = new TableCell();
                infoCell.Text = "MRN: "+name[0]+", Name: " + name[1];
                infoCell.ColumnSpan = 3;

                // add cell to row
                groupInfo.Cells.Add(infoCell);

                // ====== Make column label row
                TableRow labelRow = new TableRow();
                labelRow.Attributes["class"] = "columnLabel";

                // add row to table
                resultTable.Rows.Add(labelRow);

                // make an array of column lables
                string[] cellNames = new string[] { "Visit Date", "Document Type", "Doctor ID" };

                // add column lables to row
                foreach (string s in cellNames)
                {
                    TableCell labelCell = new TableCell();
                    labelCell.Text = s;
                    labelRow.Cells.Add(labelCell);
                }

                // Add display names to table
                foreach(var nameList in dataPair.Value)
                {
                    TableRow nameRow = new TableRow();
                    nameRow.Attributes["class"] = "columnInfo";


                    for (int i = 0; i < 3; i++)
                    {
                        TableCell nameCell = new TableCell();
                        nameCell.Text = nameList[i];
                        nameRow.Cells.Add(nameCell);
                    }
                    resultTable.Rows.Add(nameRow);
                }                    
            }       

        }
        catch(Exception ex)
        {
            // Display the error and write to log
            resultListLabel.Style.Add("Display", "Inline-Block");
            writeLog("Failed to generate tables", ex.ToString());
        }

    }

Here's what I had to do: 这是我必须要做的:

  1. Add a global boolean to called tableCall to the C# code 将全局布尔值添加到名为tableCall的C#代码中
  2. Move the code in searchButton_Click into an if(tableCall) statement in the Page_Load method. 将searchButton_Click中的代码移到Page_Load方法中的if(tableCall)语句中。

     protected void Page_Load(object sender ,EventArgs e){ ... if(tableCall){ //Do stuff from searchButton_Click } ... } 
  3. Add tableCall = true; 添加tableCall = true; and Page_Load(sender, e) to searchButton_Click 和Page_Load(sender,e)到searchButton_Click
  4. Modify the for loop to add buttons in the cell like so: // Add display names to table foreach (var nameList in dataPair.Value) { TableRow nameRow = new TableRow(); 修改for循环以在单元格中添加按钮,如下所示://将显示名称添加到表foreach(dataPair.Value中的var nameList){TableRow nameRow = new TableRow(); nameRow.Attributes["class"] = "columnInfo"; nameRow.Attributes [“ class”] =“ columnInfo”;

     // Add display names to table foreach (var nameList in dataPair.Value) { TableRow nameRow = new TableRow(); nameRow.Attributes["class"] = "columnInfo"; for (int i = 0; i < 3; i++) { TableCell nameCell = new TableCell(); nameRow.Cells.Add(nameCell); Button b = new Button(); b.Attributes["class"] = "docButton"; b.Attributes.Add("DWdocid", nameList[3]); b.Text = nameList[i]; b.Click += new EventHandler((s, ea) => test(s, ea, b.Attributes["DWdocid"])); nameCell.Controls.Add(b); } resultTable.Rows.Add(nameRow); } 

This adds a button to each of the three cells in the row, but adds the same document id to each of the buttons in that row, so anywhere the user clicks on that row (except for the 1px borders) will call a method in the C# code while passing it the document ID. 这会向该行中的三个单元格中的每个单元格添加一个按钮,但是会向该行中的每个按钮中添加相同的文档ID,因此用户在该行上单击的任何位置(1px边框除外)都将在C#代码,同时向其传递文档ID。 I'm sure that with better css skills somebody could make the button span all three cells. 我敢肯定,有了更好的CSS技能,有人可以使按钮跨越所有三个单元格。

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

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