0

Is it possible to send a parameter in an ASP.Net image button OnClick event handler to a code-behind file?

In a DetailsView we have this markup for each day of the week for the Edit template and on top of that another bunch of coding for the insert template:

<EditItemTemplate>
    <asp:ImageButton 
        ID="ImageButtonEditDayOfWeekMonday" 
        runat="server" 
        ImageUrl='<%# getCheckboxImageToDisplay(Eval("DayOfWeekMonday"))%>' 
        Height="15"
        Width="15" 
        OnClick="ImageButtonEditDayOfWeekMonday_Click"
        CausesValidation="False">
    </asp:ImageButton>
</EditItemTemplate>

The handler in the code-behind file:

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

That will amount to a lot of coding.

Is it possible to do create a single handler and call it like this?

OnClick="ImageButtonDayOfWeek_Click("Monday", "Edit")

The only difference between all the handlers is:

imgTheImageButton = DetailsView.FindControl("ImageButtonEditDayOfWeekTuesday")

It would be nice to just use a bunch of if statements inside a single handler and use the appropriate "ID" to place in DetailsView.FindControl.

1 Answer 1

0

In your ImageButton, add a CommandArgument property:

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

Then in your code-behind:

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
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for the quick reply. I used: CommandArgument="Monday, Edit" but when I type e. in the code behind file, I only get e.x and e.y as the possible values and not e.CommandArgument. Did I leave something out?
I changed the parameters of ImageButtonEditDayOfWeekTuesday_Click in the code behind. Try it. I got that from here stackoverflow.com/questions/7957816/…
Hi I get this error when I changed it to (ByVal sender As Object, ByVal e As System.Web.UI.WebControls.CommandEventArgs): Compiler Error Message: BC31143: Method 'Protected Sub ImageButtonEditDayOfWeekMonday_Click(sender As Object, e As System.Web.UI.WebControls.CommandEventArgs)' does not have a signature compatible with delegate 'Delegate Sub ImageClickEventHandler(sender As Object, e As System.Web.UI.ImageClickEventArgs)'.
The error message is real cryptic. Looks like it's possible it can't be done with image buttons.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.