0

我描述了这个问题:我有一个 aspx 页面,其中有一个由两行和两列组成的表格。

我需要读取单元格中的值并在数据库中执行插入查询。

但是如何读取行中的值并在数据库中插入 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?
                    }
                }
            }
        }
4

1 回答 1

0
protected void SaveIntoDB_Click(object sender, EventArgs e)
    {
        string Str1 = (string)this.FindControl("Label1");
        SqlCommand sqlCmd = new SqlCommand("UPDATE table SET column1= @para1", sqlConn); 
        sqlCmd.Parameters.AddWithValue("@para1", Str1);
         try
           {
             sqlConn.Open();
             sqlCmd.ExecuteNonQuery();
            }
         finally
         {
            sqlConn.Close();
         }
    }

Label1 是您要存储到 DB 的标签的 id。

于 2013-02-05T23:47:11.107 回答