0

是否可以将 ASP.Net 图像按钮 OnClick 事件处理程序中的参数发送到代码隐藏文件?

在 DetailsView 中,我们为 Edit 模板在一周中的每一天都有这个标记,并且在此之上还有另一个用于 insert 模板的编码:

<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 中。

4

1 回答 1

0

在您的 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
于 2013-01-28T18:57:00.657 回答