我完成了我的研究,但在 VB 中无法找到有效的方法:
我知道我可以使用ItemCommand选项但是无法按需运行它。
ASP.NET
内部转发器项目
<asp:Button ID="btnSave" RunAt="Server"/>
VB.NET
Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs)
sqlConn.Open()
For Each Item As RepeaterItem In rpt.Items
...
Next
sqlConn.Close()
End Sub
答案 0 :(得分:9)
修改强>
在此处进行了一些研究后,found ItemCommand
以外的其他事件未被Asp:Repeater
捕获,正如FlySwat在他的回答中所说的那样。因此,您需要编写VB.NET
代码,如下所示:
首先,在页面上声明ItemCommand
事件,如下所示:
Protected Sub rpt_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.RepeaterCommandEventArgs) Handles rpt.ItemCommand
If e.CommandName = "Save" Then
'Save
End If
End Sub
然后,在Asp:Button
内的Asp:Repeater
标记上,您必须设置其CommandName
属性,如下所示:
<Asp:Button ID="btnSave" runat="server" CommandName="Save" UseSubmitBehavior="false"/>
查看here以了解有关UseSubmitBehavior
。
试试吧。
答案 1 :(得分:4)
当按钮位于Repeater模板中时,您需要添加OnClick事件,您可以在Repeater控件的ItemDataBound event
上添加事件。
你的.aspx代码看起来像这样:
<asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate>
<asp:Button ID="btnSave" runat="server" Text="SomeText" />
</ItemTemplate>
</asp:Repeater>
代码隐藏
void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == Repeater1.AlternatingItem || e.Item.ItemType == Repeater1.Item)
{
var btn = e.Item.FindControl("btnSave") as Button;
if (btn != null)
{ // adding button event
btn.Click += new EventHandler(btn_Click);
}
}
}
void btn_Click(object sender, EventArgs e)
{
//write your code
}
在vb.net中
Private Sub Repeater1_ItemDataBound(sender As Object, e As RepeaterItemEventArgs)
If e.Item.ItemType = Repeater1.AlternatingItem OrElse e.Item.ItemType = Repeater1.Item Then
Dim btn = TryCast(e.Item.FindControl("btnSave"), Button)
If btn IsNot Nothing Then
' adding button event
btn.Click += New EventHandler(btn_Click)
End If
End If
End Sub
Private Sub btn_Click(sender As Object, e As EventArgs)
'write your code
End Sub