การสร้าง DataTable จากค่า textbox / dropdownlist

Aug 25 2020

ฉันกำลังพยายามสร้างข้อมูลที่มีสองคอลัมน์ "unit_serial" & "testresult" ฉันมีแผงที่มีตารางที่มีคอลัมน์ของกล่องข้อความที่มี "unit_serial" และรายการแบบเลื่อนลงพร้อมตัวเลือกสำหรับ Pass หรือ Fail ซึ่งเป็น "testresult" ฉันมักจะสับสนเมื่อสร้างลูปเกี่ยวกับสิ่งที่ควรอยู่ในและนอกลูปดังนั้นฉันแน่ใจว่านั่นเป็นปัญหาของฉัน แต่ฉันได้ลองรูปแบบทุกอย่างที่ฉันคิดได้และฉันไม่สามารถรับข้อมูลที่ฉันกำลังมองหา นี่คือสิ่งที่ฉันเขียน ข้อมูลตัวอย่างที่ฉันใช้ควรมีอย่างน้อย 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>

คำตอบ

SalikRafiq Aug 25 2020 at 16:05

ปัญหาของคุณอาจอยู่ที่นี่ในลูปที่ซ้อนกันนี้:

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

คุณกำลังวนลูปเหนือ droplists ทั้งหมดจากกล่องข้อความของคุณหรือแต่ละกล่อง

สิ่งที่คุณต้องการทำจริงๆคือดึงตัวควบคุม droplist ที่ตรงกับกล่องข้อความ ดูเหมือนว่าจะเป็นตัวเลขในการแข่งขันแต่ละครั้งเพื่อให้คุณสามารถใช้และFindControlค้นหา 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);
            }  

หมายเหตุ: ฉันไม่ได้ลองใช้รหัสด้านบน

ฉันพบว่ามันน่าสนใจที่คุณกำลังใช้ DataTable สำหรับการจัดเก็บในท้องถิ่นมากกว่าหรือList<> Array<>ฉันขอแนะนำให้คุณมองว่าเป็น IMHO วิธีที่ดีกว่าในการจัดเก็บข้อมูลนี้