如何动态插入/编辑中继器的值?

时间:2012-05-04 09:24:00

标签: c# asp.net repeater add insert-update

.aspx页面类似这样的内容

<form id="Form1" runat="server">  
<asp:Repeater ID="Repeater1" runat="server">
    <HeaderTemplate>
            <table border="0" width="600px" cellpadding="2" cellspacing="1" style="border: 1px solid maroon;">
        <tr bgcolor="maroon">
            <th>    Subject_Id    </th>
            <th>    Subject_Name    </th>
            <th>    Fill_Marks    </th>
        </tr>
</HeaderTemplate>

<ItemTemplate>
    <tr>
        <td width="100">
                <asp:TextBox ID="Subject_Id" runat="Server" Text='<%#Eval("Subject_Id")%>'></asp:TextBox>
        </td>
        <td>
                <asp:TextBox ID="Subject_Name" runat="Server" Text='<%#Eval("Subject_Name")%>'></asp:TextBox>
        </td>
        <td>
                <asp:TextBox ID="Marks" runat="server"></asp:TextBox>
        </td>
    </tr>
</ItemTemplate>

    <FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>

    <asp:Button ID="btnInsert" runat="server" onclick="btnInsert_Click" Text="Insert" CommandArgument="" CommandName=""/>
    <asp:Button ID="btnUpdate" runat="server" onclick="btnUpdate_Click" Text="Update" CommandArgument="" CommandName=""/>    

    </form>

C# - 代码背后......

  protected void btnInsert_Click(object sender, EventArgs e)
  {
   cn = new SqlConnection(ConfigurationManager.ConnectionStrings["DbConnect"].ConnectionString);
   cn.Open();

  foreach (RepeaterItem item in Repeater1.Items)
  {
      TextBox Subject_Id = (TextBox)item.FindControl("Subject_Id");
      TextBox Subject_Name = (TextBox)item.FindControl("Subject_Name");
      TextBox Marks = (TextBox)item.FindControl("Marks");

      SqlCommand cmd = new SqlCommand("Insert into result VALUES (id='"+@Subject_Id+"',name='"+@Subject_Name+"',marks='"+@Marks+"')", cn);

      Repeater1.DataSource = cmd.ExecuteReader();
      Repeater1.DataBind();

      cn.Close();
      cmd.Connection.Close();
      cmd.Connection.Dispose();
  }
}

现在我想将这些数据插入下表.....

result_table

id    varchar(10)
name  varchar(20)
marks varchar(3)

如何执行插入和更新功能以便从数据库中检索数据...用简单的方法? ?感谢...

3 个答案:

答案 0 :(得分:0)

在按钮标记中插入一些属性: CommandArgument =“”CommandName =“”

Repeater1.ItemCommand +=new RepeaterCommandEventHandler(Repeater1_ItemCommand);

创建一个名为protected的函数void Repeater1_ItemCommand(object source,RepeaterCommandEventArgs e){你要写的一些代码.........}

答案 1 :(得分:0)

使用以下

实现 btnInsert_Click 功能

**根据您的新功能进行编辑

  protected void btnInsert_Click(object sender, EventArgs e)
  {
    String connectionString = ConfigurationManager.ConnectionStrings["DbConnect"].ConnectionString;
    String queryString = "";
     using (SqlConnection connection = new SqlConnection(connectionString))
    {
        foreach (RepeaterItem item in Repeater1.Items)
       {
        TextBox Subject_Id = (TextBox)item.FindControl("Subject_Id");
        TextBox Subject_Name = (TextBox)item.FindControl("Subject_Name");
        TextBox Marks = (TextBox)item.FindControl("Marks");

        queryString = "Insert into result VALUES (id='"+@Subject_Id+"',name='"+@Subject_Name+"',marks='"+@Marks+"')";

        SqlCommand command = new SqlCommand(queryString, connection);

        // execute the query to update the database
        cmd.ExecuteNonQuery();
        }
    }
    //call the function to load the gridview if it is lost on postback.
  }

最好将数据库和网格视图填充代码移动到单独的功能中,这样就可以轻松地重复按钮点击操作。

答案 2 :(得分:0)

Dim permissionList As New List(Of OPASWCFApp.PROJECT_NOTE_CONTACT_MAP)
  For Each rItem As RepeaterItem In rptProducts.Items
     Dim qty1 As Integer
     Dim qty2 As Integer
     Dim hdnvalue As Integer
     Dim txtBox1 As TextBox = DirectCast(rItem.FindControl("numAdmin1"), TextBox)
     Dim txtBox2 As TextBox = DirectCast(rItem.FindControl("numAdmin2"), TextBox)
     Dim hdnf As HiddenField = DirectCast(rItem.FindControl("hdnContacttype"), HiddenField)
     qty1 = Convert.ToInt32(txtBox1.Text)
     qty2 = Convert.ToInt32(txtBox2.Text)
     hdnvalue = Convert.ToInt32(hdnf.Value)

     If qty1 > 0 Or qty2 > 0 Then
        Dim _d As OPASWCFApp.PROJECT_NOTE_CONTACT_MAP = 
                     New OPASWCFApp.PROJECT_NOTE_CONTACT_MAP()
        _d.PROJECT_NOTE_CONTACT_MAP_CONTACT_TYPE_ID = hdnvalue
        _d.PROJECT_NOTE_CONTACT_MAP_DURATION = CInt((qty1 * 60) + qty2)
        _d.PROJECT_NOTE_CONTACT_MAP_CONTACT_TYPE_ID = hdnvalue
        permissionList.Add(_d)
     End If
 Next
 ProcessDataResult.ContactMap = permissionList.ToArray()