是否可以将 ASP.Net 图像按钮 OnClick 事件处理程序中的参数发送到代码隐藏文件?
在 DetailsView 中,我们为编辑模板在一周中的每一天都有此标记,最重要的是插入模板的另一组编码:
<EditItemTemplate>
<asp:ImageButton
ID="ImageButtonEditDayOfWeekMonday"
runat="server"
ImageUrl='<%# getCheckboxImageToDisplay(Eval("DayOfWeekMonday"))%>'
Height="15"
Width="15"
OnClick="ImageButtonEditDayOfWeekMonday_Click"
CausesValidation="False">
</asp:ImageButton>
</EditItemTemplate>
代码隐藏文件中的处理程序:
Protected Sub ImageButtonEditDayOfWeekTuesday_Click(sender As Object, e As ImageClickEventArgs)
Dim imgTheImageButton As New ImageButton
imgTheImageButton = DetailsView.FindControl("ImageButtonEditDayOfWeekTuesday")
If imgTheImageButton.ImageUrl = "../../Images/checked.png" = True Then
imgTheImageButton.ImageUrl = "../../Images/unchecked.png"
LabelCheckBoxTuesday.Text = False
Else
imgTheImageButton.ImageUrl = "../../Images/checked.png"
LabelCheckBoxTuesday.Text = True
End If
End Sub
这将相当于大量的编码。
是否可以创建一个处理程序并像这样调用它?
OnClick="ImageButtonDayOfWeek_Click("Monday", "Edit")
所有处理程序之间的唯一区别是:
imgTheImageButton = DetailsView.FindControl("ImageButtonEditDayOfWeekTuesday")
最好在单个处理程序中使用一堆if语句,并使用适当的"ID"放置在DetailsView.FindControl中。
在 ImageButton 中添加 CommandArgument 属性:
<EditItemTemplate>
<asp:ImageButton
ID="ImageButtonEditDayOfWeekMonday"
runat="server"
ImageUrl='<%# getCheckboxImageToDisplay(Eval("DayOfWeekMonday"))%>'
Height="15"
Width="15"
OnClick="ImageButtonEditDayOfWeekMonday_Click"
CommandArgument='<%# Eval("DayOfWeekMonday") %>'
CausesValidation="False">
</asp:ImageButton>
然后在代码隐藏中:
Protected Sub ImageButtonEditDayOfWeekTuesday_Click(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.CommandEventArgs)
//e.CommandArgument should contain the actual value of DayOfWeekMonday
Dim arg As String = e.CommandArgument.ToString()
End Sub