問題を説明します。2 つの行と 2 つの列で構成されるテーブルを含む aspx ページがあります。
セルの値を読み取り、DB で挿入クエリを実行する必要があります。
しかし、行の値を読み取り、DB に 2 行を挿入するにはどうすればよいですか?
ページ .aspx:
<asp:table id="Tbl1" cellpadding="3" cellspacing="3" runat=server
Width="301" Height="76" BorderColor="Black" BorderWidth="1" BorderStyle="None"
GridLines="Both">
<asp:TableRow>
<asp:TableHeaderCell ColumnSpan="2"><label class="LabelCampi">Taglie</label></asp:TableHeaderCell>
</asp:TableRow>
<asp:TableRow>
<asp:TableHeaderCell><label style="font:Arial, Helvetica, sans-serif; font-size:14px; font-weight: bold;">Valore Effettivo</label></asp:TableHeaderCell>
<asp:TableHeaderCell><label style="font:Arial, Helvetica, sans-serif; font-size:14px; font-weight: bold;">Valore Etichetta</label></asp:TableHeaderCell>
</asp:TableRow>
<asp:TableRow>
<asp:TableCell>
<asp:DropDownList ID="DropDownListl" runat="server">
<asp:ListItem>37</asp:ListItem>
<asp:ListItem>38</asp:ListItem>
<asp:ListItem>39</asp:ListItem>
</asp:DropDownList>
</asp:TableCell>
<asp:TableCell>
<asp:TextBox ID="txt1" runat="server"></asp:TextBox>
</asp:TableCell>
</asp:TableRow>
<asp:TableRow>
<asp:TableCell>
<asp:DropDownList ID="DropDownList2" runat="server">
<asp:ListItem>37</asp:ListItem>
<asp:ListItem>38</asp:ListItem>
<asp:ListItem>39</asp:ListItem>
</asp:DropDownList>
</asp:TableCell>
<asp:TableCell>
<asp:TextBox ID="txt2" runat="server"></asp:TextBox>
</asp:TableCell>
</asp:TableRow>
</asp:table>
<asp:Button ID="SaveIntoDB" runat="server" Text="Save into DB"
onclick="SaveIntoDB_Click" />
コードビハインド:
protected void SaveIntoDB_Click(object sender, EventArgs e)
{
Table table = (Table)Page.Form.FindControl("Tbl1");
if (table != null)
{
foreach (TableRow row in table.Rows)
{
//here you can access the property of each row
foreach (TableCell cell in row.Cells)
{
//here query insert?
}
}
}
}