利用CommandArgument 属性获取 datalist的中 控件数据绑定
aspx中的datalist标记语言:
<asp:DataList ID="DataList1" runat="server" Width="500px" Height="1107px">
<ItemTemplate>
<table width="100%">
<tr>
<td rowspan="3" style="width: 38%; height: 8px;" valign="top">
<asp:Image ID="ImgPerson" runat="server" Height="100px" Width="80px" ImageUrl='<%#"ShowUserPhoto.aspx?id="+Eval("message_id") %>'
/></td>
<td style="height: 20px; width: 18%;">
<asp:Label ID="LPerson" runat="server" Font-Names="宋体" Font-Size="Small" Text='<%# Eval("message_author_name") %>'></asp:Label></td>
<td style="height: 20px; width: 18%;" align="center">
<asp:Label ID="LTime" runat="server" Font-Names="宋体" Font-Size="Small" Text='<%# Eval("message_date") %>'></asp:Label></td>
<td style="width: 7%; height: 20px" align="right">
<asp:Button ID="BtnReply" runat="server" Height="22px" OnClick="BtnReply_Click"
Text="回复" Width="42px" CommandArgument='<%# Eval("message_author_name") %>' />
<asp:Button ID="BtnDelete" runat="server" OnClick="BtnDelete_Click" Text="删除" CommandArgument='<%# Eval("message_id") %>' />
</td>
</tr>
<tr>
<td colspan="3" rowspan="2" style="height: 39px; width: 40%;" align="left">
<asp:Label ID="LMessage" runat="server" Font-Names="宋体" Font-Size="Small" Height="60px"
Text='<%# Eval("message_content") %>' Width="369px"></asp:Label></td>
</tr>
<tr>
</tr>
<tr>
<td colspan="4" rowspan="1" valign="top" style="height: 14px">
<br />
</td>
</tr>
</table>
</ItemTemplate>
</asp:DataList></td>
获取datalist中控件的数据绑定,删除记录
aspx.cs删除记录代码:
protected void BtnDelete_Click(object sender, EventArgs e)
{
String connectionString = ConfigurationManager.ConnectionStrings["communciationPlatformConnectionString"].ConnectionString;
String sql = "DeleteMessage";
SqlConnection connection = new SqlConnection(connectionString);
SqlCommand command = new SqlCommand(sql, connection);
command.CommandType = CommandType.StoredProcedure;
SqlParameter paramMessageId = new SqlParameter("@message_id", SqlDbType.Int);
Button b = (Button)sender;
int s = Convert.ToInt32(b.CommandArgument);
paramMessageId.Value = s;
command.Parameters.Add(paramMessageId);
connection.Open();
command.ExecuteNonQuery();
connection.Close();
indexListBind();//重新绑定
}