我在.aspx文件中以下标记:
<asp:Repeater ID="ListOfAssignments" runat="server" OnItemDataBound="ListOfAssignments_ItemDataBound">
<ItemTemplate>
<asp:Label ID="AssignmentID" runat="server" Text="Label"></asp:Label>
<br />
<asp:Label ID="PathToFile" runat="server" Text="Label"></asp:Label>
<br />
<asp:Button ID="RemoveAssignment" runat="server" Text="Remove" OnClick="RemoveAssignment_Click"/>
</ItemTemplate>
</asp:Repeater>
有两个标签,并在一个相当标准的中继器控制按钮。 中继器绑定到一个数据库,并与数据库中的记录填充标签。
这里是我的问题:我有在中继器的每个按钮的click事件。 该RemoveAssignment_Click方法被调用,当用户点击任何按钮。 在单击事件,我想知道与任何按钮,用户点击相关联的两个标签的文本。
我的意思是,在这个方法:
protected void RemoveAssignment_Click(object sender, EventArgs e)
{
//code goes here
}
我希望能够知道相邻的被点击的按钮标签的文本。 我怎么做?
你要找的是Button.OnCommand方法 :
这使您可以在Web页面上创建多个Button控件和编程方式确定哪个按钮控件被点击。
所以里面ListOfAssignments_ItemDataBound
你的CommandArgument分配到该按钮,其中CommandArgument
是文章的ID将被删除:
protected void ListOfAssignments_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
Button delButton = e.Item.FindControl("RemoveAssignment") as Button;
delButton.CommandArgument = //set to the value of AssignmentID
//rest of your code
}
}
现在您的按钮应该说使用新的按需:
<asp:Button ID="RemoveAssignment" OnCommand="RemoveAssignment" runat="server" Text="Remove" />
然后你创建的方法:
protected void RemoveAssignment(object sender, CommandEventArgs e)
{
int articleIDToDelete = 0;
if (Int32.TryParse((string)e.CommandArgument, out articleIDToDelete))
{
//delete the article with an ID = articleIDToDelete
}
}
您可以添加的CommandName和CommandArgument属性按钮标签,并把它们作为一个提示。 然后在你的,甚至处理,你可以做e.CommandName ==“XXX”或e.CommandArgument ==“XXX”。 您还可以使用CommandArgument来通过实际的字符串。 你只需要绑定数据,就像你要做一个标签,文本等做:
<asp:Button ID="RemoveAssignment" runat="server" CommandArgument='<%#Eval("Label1")+","+ Eval("Label2")%>' Text="Remove" OnClick="RemoveAssignment_Click"/>
然后在事件处理程序,你可以这样做:
string[] args = e.CommandArgument.ToString().Split(new char[] {','});
string label1 = args[0];
string label2 = args[1];
这应该让你去。
嗯,我知道C#和WP,但你应该尝试设置一个布尔值,并使用此逻辑:如果是假的,重复将继续进行,如果这是真的,它会退出重复。 尝试这个:
bool exit;
void Button_Click(eventargs stuffy here)
{
exit = true,
}
void Repeat()
{
if (exit == false)
{
Repeat();
//Your code here
}
}