ListView怎么获取绑定行的ID?
ListView中绑定的是照片数据,以下是前台代码
<asp:ListView ID="ListView1" runat="server" DataKeyNames="PId">
<ItemTemplate>
<asp:Image ID="header" runat="server" Height="60px" Width="60px" ImageUrl='<%#Eval("PAddress") %>' /><br />
<asp:TextBox ID="txtPhotoName" runat="server" Text='<%#Eval("PName") %>' ReadOnly="true" BorderStyle="None"></asp:TextBox>
<asp:Button ID="btnQueren" runat="server" Text="确认修改" CommandArgument='<%#Eval("PId") %>' OnClick="Button3_Click" Visible="false" /><br />
<asp:Button ID="btnupdate" runat="server" Text="编辑" CommandArgument='<%#Eval("PId") %>' OnClick="Button2_Click" />
<asp:Button ID="btndelete" runat="server" Text="删除" CommandArgument='<%#Eval("PId") %>' OnClick="Button1_Click" /><br /><br />
</ItemTemplate>
</asp:ListView>
现在想要通过<ItemTemplate>里的删除按钮删除数据库中的某张照片,可是每次都是删除的第一张,下面是我现在删除按钮事件下的代码,问题出在哪儿了?
int pid = int.Parse(((Button)FindControlExtend("btndelete", ListView1.Controls)).CommandArgument.ToString());
string sql = "delete tb_Photos where PId="+pid+"";
if (common.DBCommon.adlData(sql))
{
Response.Write("<script>alert('删除成功!')</script>");
photoBind();
}
else
{
Response.Write("<script>alert('暂时还不能删除哦!')</script>");
}
请大神指教!!
------解决方案--------------------http://blog.csdn.net/heker2007/article/details/1539380
http://www.4guysfromrolla.com/articles/052709-1.aspx
------解决方案--------------------应该是ID取值不对吧
在如下的事件中试试
protected void ListView1_ItemCommand(object sender, ListViewCommandEventArgs e)
{
Button btn = e.CommandSource as Button;
if (btn.ID == "btndelete")
&