【问题标题】:Creating DataTable from textbox / dropdownlist values从文本框/下拉列表值创建 DataTable
【发布时间】:2020-08-25 15:38:21
【问题描述】:

我正在尝试创建一个包含“unit_serial”和“testresult”两列的数据表。我有一个面板,其中包含一列带有“unit_serial”的文本框和带有“通过或失败”选项的下拉列表,即“测试结果”。在创建循环时,我经常混淆应该在循环内部和外部做什么,所以我确定这是我的问题,但我已经尝试了我能想到的每一个变化,但我无法获得我正在寻找的数据。这是我写的。我正在使用的示例数据应该至少有 3 行,但我只得到 1 行。

我也有一个单独的问题,我将在这个问题中包含同一个面板,因为我确信它相对简单。除非填充同一表格行中的文本框字段,否则面板中的下拉列表将被禁用。当我进行第一个下拉列表选择(这会导致回发)时,所有下拉列表都会启用。回发后如何保持下拉菜单的禁用状态?

DataTable dt = new DataTable();
            dt.Columns.Add("hbserial");
            dt.Columns.Add("test_result");
            DataRow dr = dt.NewRow();
            var myTextBoxes = TestResults.Controls
                .OfType<TextBox>()
                .Where(tb => !string.IsNullOrWhiteSpace(tb.Text));

            var myDropDownLists = TestResults.Controls
                .OfType<DropDownList>()
                .Where(ddl => ddl.SelectedValue != "--Select--");

            foreach (TextBox tb in myTextBoxes)
            {
                string hbserial = tb.Text;
                dr["hbserial"] = hbserial;
                foreach (DropDownList ddl in myDropDownLists)
                {
                    string testresult = ddl.SelectedValue;
                    dr["test_result"] = testresult;
                }

                dt.Rows.Add(dr);
            }

这是我的 aspx 页面代码:

<asp:Panel ID="TestResults" runat="server" Width="1040px" BorderStyle="Double" BorderWidth="2px" Height="280px">
            <br />
             <table id="HBredotable" style="width: 625px">
                <tr>
                    <td style="width: 189px" align="center">
                        <asp:Label ID="Label2" runat="server" Text="Serial One:"></asp:Label>
                    </td>
                    <td style="width: 248px">
                        <asp:TextBox ID="TextBox1" runat="server" Width="230px" Height="22px" ReadOnly="true"></asp:TextBox>
                    </td>
                    <td>
                        <asp:DropDownList ID="DropDownList1" runat="server" Height="22px" Width="165px" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged" AutoPostBack="true" EnableViewState="true">
                            <asp:ListItem Text="--Select--" Value="2"></asp:ListItem>
                            <asp:ListItem Text="Pass" Value="1"></asp:ListItem>
                            <asp:ListItem Text="Fail" Value="0"></asp:ListItem>
                        </asp:DropDownList>
                    </td>
                    <tr>
                        <td style="width: 189px" align="center">
                            <asp:Label ID="Label3" runat="server" Text="Serial Two:"></asp:Label>
                        </td>
                        <td style="width: 248px">
                            <asp:TextBox ID="TextBox2" runat="server" Width="230px" Height="22px" ReadOnly="true"></asp:TextBox>
                        </td>
                        <td>
                            <asp:DropDownList ID="DropDownList2" runat="server" Height="22px" Width="165px" OnSelectedIndexChanged="DropDownList2_SelectedIndexChanged" AutoPostBack="true" EnableViewState="true">
                                <asp:ListItem Text="--Select--"></asp:ListItem>
                                <asp:ListItem Text="Pass" Value="1"></asp:ListItem>
                                <asp:ListItem Text="Fail" Value="0"></asp:ListItem>
                            </asp:DropDownList>
                        </td>
                    </tr>
                <tr>
                    <td style="width: 189px" align="center">
                        <asp:Label ID="Label4" runat="server" Text="Serial Three:"></asp:Label>
                    </td>
                    <td style="width: 248px">
                        <asp:TextBox ID="TextBox3" runat="server" Width="230px" Height="22px" ReadOnly="true"></asp:TextBox>
                    </td>
                    <td>
                        <asp:DropDownList ID="DropDownList3" runat="server" Height="22px" Width="165px" OnSelectedIndexChanged="DropDownList3_SelectedIndexChanged" AutoPostBack="true" EnableViewState="true">
                            <asp:ListItem Text="--Select--"></asp:ListItem>
                            <asp:ListItem Text="Pass" Value="1"></asp:ListItem>
                            <asp:ListItem Text="Fail" Value="0"></asp:ListItem>
                        </asp:DropDownList>
                    </td>

                </tr>
                <tr>
                    <td style="width: 189px" align="center">
                        <asp:Label ID="Label5" runat="server" Text="Serial Four:"></asp:Label>
                    </td>
                    <td style="width: 248px">
                        <asp:TextBox ID="TextBox4" runat="server" Width="230px" Height="22px" ReadOnly="true"></asp:TextBox>
                    </td>
                    <td>
                        <asp:DropDownList ID="DropDownList4" runat="server" Height="22px" Width="165px" OnSelectedIndexChanged="DropDownList4_SelectedIndexChanged" AutoPostBack="true" EnableViewState="true">
                            <asp:ListItem Text="--Select--"></asp:ListItem>
                            <asp:ListItem Text="Pass" Value="1"></asp:ListItem>
                            <asp:ListItem Text="Fail" Value="0"></asp:ListItem>
                        </asp:DropDownList>
                    </td>
                </tr>
                <tr>
                    <td style="width: 189px" align="center">
                        <asp:Label ID="Label6" runat="server" Text="Serial Five:"></asp:Label>
                    </td>
                    <td style="width: 248px">
                        <asp:TextBox ID="TextBox5" runat="server" Width="230px" Height="22px" ReadOnly="true"></asp:TextBox>
                    </td>
                    <td>
                        <asp:DropDownList ID="DropDownList5" runat="server" Height="22px" Width="165px" OnSelectedIndexChanged="DropDownList5_SelectedIndexChanged" AutoPostBack="true" EnableViewState="true">
                            <asp:ListItem Text="--Select--"></asp:ListItem>
                            <asp:ListItem Text="Pass" Value="1"></asp:ListItem>
                            <asp:ListItem Text="Fail" Value="0"></asp:ListItem>
                        </asp:DropDownList>
                    </td>
                </tr>
                <tr>
                    <td style="width: 189px" align="center">
                        <asp:Label ID="Label7" runat="server" Text="Serial Six:"></asp:Label>
                    </td>
                    <td style="width: 248px">
                        <asp:TextBox ID="TextBox6" runat="server" Width="230px" Height="22px" ReadOnly="true"></asp:TextBox>
                    </td>
                    <td>
                        <asp:DropDownList ID="DropDownList6" runat="server" Height="22px" Width="165px" OnSelectedIndexChanged="DropDownList6_SelectedIndexChanged" AutoPostBack="true" EnableViewState="true">
                            <asp:ListItem Text="--Select--"></asp:ListItem>
                            <asp:ListItem Text="Pass" Value="1"></asp:ListItem>
                            <asp:ListItem Text="Fail" Value="0"></asp:ListItem>
                        </asp:DropDownList>
                    </td>
                </tr>
            </table>
            <br />
            <asp:Label ID="lblREDOerror" runat="server" Text="" ForeColor="red"></asp:Label><br />
            <asp:Button ID="btnAllPass" runat="server" Text="All Tests Pass / Unit Complete"  Width="210px" OnClick="btnAllPass_Click"/>
            <asp:Button ID="btnAddredo" runat="server" Text="Enter Hashboards for REDO" Width="210px" OnClick="btnAddredo_Click" />
            <asp:Button ID="btnconfirmredo" runat="server" Text="Yes, Continue" Width="210px" OnClick="btnconfirmredo_Click" />
            <asp:Button ID="btnreturn" runat="server" Text="No, Go Back" Width="210px" OnClick="btnreturn_Click"  />
            <br />
        </asp:Panel>

【问题讨论】:

    标签: c# asp.net datatable


    【解决方案1】:

    您的问题可能在这个嵌套循环中:

    foreach (TextBox tb in myTextBoxes)
                {
                    string hbserial = tb.Text;
                    dr["hbserial"] = hbserial;
                    foreach (DropDownList ddl in myDropDownLists)
                    {
                        string testresult = ddl.SelectedValue;
                        dr["test_result"] = testresult;
                    }
    
                    dt.Rows.Add(dr);
                }
    

    您正在遍历您或每个文本框中的所有下拉列表。

    您真正想要做的是获取与文本框匹配的下拉列表控件。它看起来像每个匹配项的数字,因此您可以使用它和 FindControl 来定位下拉列表。

    类似这样的:

    foreach (TextBox tb in myTextBoxes)
                {
                    string hbserial = tb.Text;
                    dr["hbserial"] = hbserial;
                    var num = tb.ControlID.Substring("TextBox".Length-1);
                    var droplistForTextBox = TestResults.FindControl<DropDownList>("DropDownList"+num);
                    string testresult = ddl.SelectedValue;
                    dr["test_result"] = testresult;
                    dt.Rows.Add(dr);
                }  
    

    注意:我没有尝试过上面的代码。

    我确实觉得有趣的是,您使用 Datatable 进行本地存储,而不是 List&lt;&gt;Array&lt;&gt;。恕我直言,我建议您将其视为存储此信息的更好方法。

    【讨论】:

    • 我最终将创建 DataTable,因此我可以执行以下操作:int rowcount = dt.AsEnumerable().Where(x =&gt; x["testresult"].ToString() == "0").ToList().Count; 我在存储过程中使用rowcount 作为参数。我可以这样使用List&lt;&gt;Array&lt;&gt;吗?
    • 我也在使用 DataTable 将“unit_serial”和“testresult”值传递给存储过程。
    • 这也可以使用ListArray 来完成,但这对你来说是任何工作。请尝试上面的代码,如果对您有帮助,请标记为答案。
    • 这是我的错,因为我用 asp.net 标记了这个问题,但我使用的是 WinForms 并得到“文本框不包含 ControlID 的定义”以及此代码中的一些其他错误。您能否修改您对 WinForms 的建议?非常感激!! :)
    • 您提供了 ASPX 布局并将其与 WinForms 混淆了?可悲的是我不知道 WinForms。我建议你用适当的标签提出一个新问题,也许删除这个问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多