Solved

Handling commands in a datalist

Posted on 2004-04-15
2
651 Views
Last Modified: 2012-08-13
Hi everyone, I'm having a problem handling command events from a datalist, more specifically when the command is processed I'm not getting any input the user may have entered.

I have a datalist which is showing some information about an image and providing controls to edit the caption or delete the image:

<asp:DataList runat="server" id="additionalphotos" RepeatDirection="Vertical" RepeatColumns="1" HorizontalAlign="Center" OnItemDataBound="AdditionalPhotos_ItemDataBound" OnItemCommand="AdditionalPhotos_ItemCommand">
<itemtemplate>
  <table align="left" cellpadding="0" cellspacing="0" border="0">
    <tr>
      <td class="formfield"><asp:Label runat="server" id="additionalcaption" EnableViewState="false" /></td>
    </tr>
    <tr>
      <td class="formfield"><asp:Image runat="server" id="showadditional" EnableViewState="false" /></td>
    </tr>
    <tr>
      <label for="addphoto"><td class="formheader5">Image Location</td></label>
    </tr>
    <tr>
      <td class="formfield"><input accept="image/jpeg" type="file" id="additionalphoto" runat="server" class="len5" EnableViewState="false"></td>
    </tr>
    <tr>
      <td class="formheader5">Image Caption</td>
    </tr>
    <tr>
      <td class="formfield"><asp:TextBox runat="server" id="editadditionalcaption" TextMode="SingleLine" MaxLength="50" class="len5" /></td>
    </tr>
    <tr>
      <td class="formfield"><asp:CheckBox runat="server" id="deladditional" Text="Delete this image" EnableViewState="false"/></td>
    </tr>
    <tr>
      <td class="formfield"><asp:LinkButton runat="server" id="editadditional" Text="Submit" CommandName="EditAdditional"/></td>
    </tr>
    <tr>
      <td class="formfield"><br></td>
    </tr>
  </table>
</itemtemplate>
</asp:Datalist>

In the ItemDataBound event I set up the imageURL and the caption label (there is some processing done that's why I don't just use in-place server code to set them).

My first hurdle was getting a button to fire the ItemCommand event, I had been trying with a regular Button but apparently you have to use a LinkButton, not sure why this is the case but I can live with it for now.  Once I switched to using a LinkButton the ItemCommand event was firing and being handled correctly.

Here's my ItemCommand handler:

sub AdditionalPhotos_ItemCommand(sender As Object, e As System.Web.UI.WebControls.DataListCommandEventArgs)
      dim id as String
      id = GetEncVal("id")

      dim strSQL as String

      dim cbDelete as CheckBox
      cbDelete = e.Item.FindControl("deladditional")
      if (not cbDelete is nothing) then
            TRACE.Write("DEBUG", "Found checkbox:" & cbDelete.ClientID)
            if cbDelete.checked then
                  TRACE.Write("DEBUG", "Deleting image '" & e.Item.DataItem("caption") & "'")
                  // CODE TO DELETE IMAGE
            end if
      end if
            
      dim txtCaption as TextBox
      txtCaption = e.Item.FindControl("editadditionalcaption")
      if ( not txtCaption is nothing) then
            TRACE.Write("DEBUG", "Found caption: " & txtCaption.Text)
      end if
end sub

When looking at the TRACE output, the delete checkbox and caption textbox are found, but the checkbox is never considered checked and the caption is always seen as being empty.  Can anyone tell me why this is the case?  Does the command event not count as a submit for some reason?
0
Comment
Question by:Veregon
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 28

Accepted Solution

by:
mmarinov earned 125 total points
ID: 10837650
do you use
If Not IsPostBack Then
 'bind datalist
End If

if you don't then you won't see anything in your controls

B..M
0
 

Author Comment

by:Veregon
ID: 10838182
ahh ok, I believe I'm binding the datalist every time, will confirm tomorrow at the office and respond, thanks!
0

Featured Post

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
VS 2017 18 190
T-SQL and CLR parameter strings 9 64
Check only one toolstripmenu item 12 62
.NET Core supports which cell phone platforms? 3 37
Summary Displaying images in RichTextBox is a common requirement with limited solutions available. Pasting through clipboard or embedding into RTF content only support static images.  This article describes how to insert Windows control objects int…
A basic question.. “What is the Garbage Collector?” The usual answer given back: “Garbage collector is a background thread run by the CLR for freeing up the memory space used by the objects which are no longer used by the program.” I wondered …
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question