0

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に配置すると便利です。

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 に答える