从文本框/下拉列表值创建DataTable [英] Creating DataTable from textbox / dropdownlist values

查看:63
本文介绍了从文本框/下拉列表值创建DataTable的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个包含两列的数据表 unit_serial, & 测试结果。我有一个带有表格的面板,该表格包含一列带有 unit_serial的文本框。和带有通过或失败选项的下拉列表,即 testresult。创建循环时,我经常会混淆应该在循环内外执行的操作,因此我确定这是我的问题,但是我尝试了所有我能想到的变体,但无法获得所需的数据。这是我写的。我正在使用的样本数据应该至少有3行,但我只有1行。

I am trying to create a datatable with two columns, "unit_serial" & "testresult". I have a panel with a table containing a column of textboxes with "unit_serial" and dropdownlist with option for Pass or Fail which is the "testresult". I often get mixed up when creating loops as to what should go inside and outside the loop so I'm sure that is my issue but I've tried every variation I could think of and I cannot get the data I'm looking for. Here is what I have written. The sample data I'm working with should have at least 3 rows but I'm getting only 1.

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

I also have a separate issue with the same panel that I will include in this question as I'm sure it's something relatively simple. The dropdownlists in the panel are disabled unless the textbox field in the same table row is filled. When I make the first dropdownlist selection (which causes postback) all dropdown lists become enabled. How can I maintain the disabled status of the dropdowns after postback?

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页面代码:

Here is my aspx page code:

<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>


推荐答案

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

Your problem is probably here in this nested loop:

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);
            }

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

You are looping over all the droplists from your or each textbox.

您真正想做的是获取与文本框匹配的下拉列表控件。看起来像是每次比赛中的数字,因此您可以使用该数字和 FindControl 来找到下拉列表。

What you really want to do is fetch the droplist control matched with the textbox. It looks like the number at each matches so you can use that and FindControl to locate the droplist.

类似这样的东西:

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);
            }  

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

NB: I've not tried the code above.

我确实发现您正在使用Datatable进行本地存储,而不是使用 List<< Array<> 。我建议您以一种恕我直言的更好的方式来存储这些信息。

I do find it interesting that you are using a Datatable for local storage rather than a List<> or an Array<>. I'd recommend you look into those as a, IMHO, better way of storing this information.

这篇关于从文本框/下拉列表值创建DataTable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆