读取行Asp:表并将单元格值插入DB

时间:2013-02-05 23:07:47

标签: c# asp.net row

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

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

但是如何读取行中的值并在DB中插入2行?

Page .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?
                    }
                }
            }
        }

1 个答案:

答案 0 :(得分: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。