2012-05-04 55 views
2

.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='"[email protected]_Id+"',name='"[email protected]_Name+"',marks='"[email protected]+"')", 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) 

我怎樣才能執行插入和更新功能從數據庫中檢索數據...以簡單的方式? ?謝謝...

回答

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='"[email protected]_Id+"',name='"[email protected]_Name+"',marks='"[email protected]+"')"; 

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

將數據庫和網格視圖填充代碼移動到單獨的函數總是會更好,以便輕鬆重複按鈕單擊操作。

+0

雷 - 我實現你的指導,但仍然得到錯誤.... **我的問題是編輯**你可以看到我的更新如上....如果可能給我解決方案...感謝您的快速反應 – mack28

0

插入一些財產在你的按鈕標籤: CommandArgument = 「」 的CommandName = 「」

Repeater1.ItemCommand +=new RepeaterCommandEventHandler(Repeater1_ItemCommand); 

創建一個名爲功能保護無效Repeater1_ItemCommand(對象源,RepeaterCommandEventArgs E){一些代碼你想寫.........}

+0

德韋 - 不是爲我工作......你能給我的示例代碼還鎮定,我就面臨的IT問題... – mack28

+0

我編輯我的問題......你可以看到,如果可能的話給我的解決方案...謝謝 – mack28

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() 
+0

聽起來不像'C#'給我! –