ASP.net datalist中点击button2(text=删除),不能执行datalist1_ItemCommand方法,求帮帮忙,代码如下: 10
<asp:DataListID="DataList1"runat="server"OnItemCommand="datalist1_ItemCommand"OnItemD...
<asp:DataList ID="DataList1" runat="server" OnItemCommand="datalist1_ItemCommand" OnItemDataBound="DataList1_ItemDataBound">
<ItemTemplate>
<table width="711" height="80" border="1">
<tr>
<td height="36"><table width="701" height="37" border="1">
<tr>
<td width="30">
<%# (Container.ItemIndex+1)%>
</td>
<td width="103"><%# Eval("Name")%> </td>
<td width="227"><%# Eval("Address")%> </td>
<td width="158"><%# Eval("Phone")%> </td>
<td width="149"><%# Eval("Code")%> </td>
</tr>
</table></td>
</tr>
<tr>
<td align="left"><table width="499" height="32" >
<tr>
<td class="style7">
<asp:Button ID="Button1" runat="server" Text="修改" Width="130px"
onclick="Button1_Click" />
</td>
<td>
<asp:Button ID="Button2" runat="server" Text="删除" Width="130px"
onclick="Button2_Click" CommandName="insert"/>
</td>
<td>
<asp:Label ID="Address_Id" runat="server" ><%# DataBinder.Eval(Container .DataItem ,"Address_Id")%></asp:Label>
</td>
</tr>
</table></td>
</tr>
</table>
</ItemTemplate>
</asp:DataList>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~后台
UserClass ucObj = new UserClass();
string AddressId;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Bind();
}
}
public void Bind()
{
string name = Session["userneam"].ToString();
if (!IsPostBack)
{
PagedDataSource ps=ucObj.Display_Address(name, DataList1);//连接数据库后返回PagedDataSource
DataList1.DataSource = ps;
DataList1.DataKeyField = "Address_Id";
DataList1.DataBind();
}
}
protected void datalist1_ItemCommand(object source, DataListCommandEventArgs e)
{
if (e.CommandName == "insert")
{
AddressId = ((Label)e.Item.FindControl("Address_Id")).Text;
//ArrayList ar = new ArrayList();
}
} 展开
<ItemTemplate>
<table width="711" height="80" border="1">
<tr>
<td height="36"><table width="701" height="37" border="1">
<tr>
<td width="30">
<%# (Container.ItemIndex+1)%>
</td>
<td width="103"><%# Eval("Name")%> </td>
<td width="227"><%# Eval("Address")%> </td>
<td width="158"><%# Eval("Phone")%> </td>
<td width="149"><%# Eval("Code")%> </td>
</tr>
</table></td>
</tr>
<tr>
<td align="left"><table width="499" height="32" >
<tr>
<td class="style7">
<asp:Button ID="Button1" runat="server" Text="修改" Width="130px"
onclick="Button1_Click" />
</td>
<td>
<asp:Button ID="Button2" runat="server" Text="删除" Width="130px"
onclick="Button2_Click" CommandName="insert"/>
</td>
<td>
<asp:Label ID="Address_Id" runat="server" ><%# DataBinder.Eval(Container .DataItem ,"Address_Id")%></asp:Label>
</td>
</tr>
</table></td>
</tr>
</table>
</ItemTemplate>
</asp:DataList>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~后台
UserClass ucObj = new UserClass();
string AddressId;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Bind();
}
}
public void Bind()
{
string name = Session["userneam"].ToString();
if (!IsPostBack)
{
PagedDataSource ps=ucObj.Display_Address(name, DataList1);//连接数据库后返回PagedDataSource
DataList1.DataSource = ps;
DataList1.DataKeyField = "Address_Id";
DataList1.DataBind();
}
}
protected void datalist1_ItemCommand(object source, DataListCommandEventArgs e)
{
if (e.CommandName == "insert")
{
AddressId = ((Label)e.Item.FindControl("Address_Id")).Text;
//ArrayList ar = new ArrayList();
}
} 展开
2个回答
展开全部
-----前台: ----- <asp:Button ID="Button2" runat="server" Text="删除" Width="130px"
onclick="Button2_Click" CommandName="insert"/>
-----后台: -----
if (e.CommandName == "insert")
{
AddressId = ((Label)e.Item.FindControl("Address_Id")).Text;
}
你前面将 CommandName的值已经定义好了。“insert”。
第一指:你的判断语是多余的,因为你默认的是可以执行删除;
第二指:Addressid,这个是变量。你没有定义它,内存空间是不会给这个变量分配内存的,
试问:没有内存空间的变量(就像没有容器,怎么盛水),怎么能被赋值?
onclick="Button2_Click" CommandName="insert"/>
-----后台: -----
if (e.CommandName == "insert")
{
AddressId = ((Label)e.Item.FindControl("Address_Id")).Text;
}
你前面将 CommandName的值已经定义好了。“insert”。
第一指:你的判断语是多余的,因为你默认的是可以执行删除;
第二指:Addressid,这个是变量。你没有定义它,内存空间是不会给这个变量分配内存的,
试问:没有内存空间的变量(就像没有容器,怎么盛水),怎么能被赋值?
展开全部
你直接给
<asp:Button ID="Button2" runat="server" Text="删除" Width="130px"
onclick="Button2_Click" CommandName="insert" CommandArgument='<%#Eval("Address_Id") %>'/ >
後台。
if (e.CommandName == "insert")
{
string address=e.CommandArgument.tostring().trim();
}
address就是你想删除的值
<asp:Button ID="Button2" runat="server" Text="删除" Width="130px"
onclick="Button2_Click" CommandName="insert" CommandArgument='<%#Eval("Address_Id") %>'/ >
後台。
if (e.CommandName == "insert")
{
string address=e.CommandArgument.tostring().trim();
}
address就是你想删除的值
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询