2016-05-25 40 views
-1

我有按鈕從數據庫中刪除消息,但我無法從repeateritem中的複選框中獲取值。我怎樣才能做到這一點? 這是我btndelete:如何獲取RepeaterItems(ASP.NET)中的輸入類型複選框的值?

protected void btnDelete_Click(object sender, EventArgs e) 
{ 
    foreach (RepeaterItem aItem in rptInbox.Items) 
    { 
     HtmlInputCheckBox chk = (HtmlInputCheckBox)aItem.FindControl("chkRow"); 

     if (chk.Checked == true) 
     { 
      string id = chk.Value.ToString(); 

      SqlConnection con = new SqlConnection(conStr); 
      SqlCommand cmd = new SqlCommand("delete from mailbox where mailboxid='"+id+"'", con); 
      try 
      { 
       con.Open(); 
       cmd.ExecuteNonQuery(); 
      } 
      catch (Exception er) 
      { 

      } 
      finally 
      { 
       con.Close(); 
      } 
     } 
    } 
} 

這是我的HTML:

<asp:Repeater ID="rptInbox" runat="server"> 
    <ItemTemplate> 
     <tr> 
      <td><input type="checkbox" ID="chkRow" value='<%#Eval("mailboxid")%>' runat="server"/></td> 
      <td><%#Eval("from") %></td> 
      <td><a href="InboxDetail.aspx?MailboxId=<%#Eval("mailboxid") %>"><%#Eval("subject") %></a></td> 
      <td><%#Eval("date") %></td> 
     </tr> 
    </ItemTemplate> 
</asp:Repeater> 

回答

0

您可以將值存儲在一個隱藏字段:

<asp:Repeater ID="rptInbox" runat="server"> 
    <ItemTemplate> 
     <tr> 
      <td> 
       <input type="checkbox" ID="chkRow" runat="server"/> 
       <input type="hidden" ID="hiddenMailBoxID" value='<%# Eval("mailboxid") %>' runat="server"/> 
      </td> 
      <td> 
       <%#Eval("from") %> 
      </td> 
      ... 
     </tr> 
    </ItemTemplate> 
</asp:Repeater> 

而在後臺代碼檢索:

HtmlInputHidden hiddenMailBoxID = aItem.FindControl("hiddenMailBoxID") as HtmlInputHidden; 
string id = hiddenMailBoxID.Value;