Veregon
asked on
Handling commands in a datalist
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="Additiona lPhotos_It emDataBoun d" OnItemCommand="AdditionalP hotos_Item Command">
<itemtemplate>
<table align="left" cellpadding="0" cellspacing="0" border="0">
<tr>
<td class="formfield"><asp:Lab el runat="server" id="additionalcaption" EnableViewState="false" /></td>
</tr>
<tr>
<td class="formfield"><asp:Ima ge 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:Tex tBox runat="server" id="editadditionalcaption" TextMode="SingleLine" MaxLength="50" class="len5" /></td>
</tr>
<tr>
<td class="formfield"><asp:Che ckBox runat="server" id="deladditional" Text="Delete this image" EnableViewState="false"/>< /td>
</tr>
<tr>
<td class="formfield"><asp:Lin kButton runat="server" id="editadditional" Text="Submit" CommandName="EditAdditiona l"/></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_ItemComma nd(sender As Object, e As System.Web.UI.WebControls. DataListCo mmandEvent Args)
dim id as String
id = GetEncVal("id")
dim strSQL as String
dim cbDelete as CheckBox
cbDelete = e.Item.FindControl("deladd itional")
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("editad ditionalca ption")
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?
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"
<itemtemplate>
<table align="left" cellpadding="0" cellspacing="0" border="0">
<tr>
<td class="formfield"><asp:Lab
</tr>
<tr>
<td class="formfield"><asp:Ima
</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"></
</tr>
<tr>
<td class="formheader5">Image Caption</td>
</tr>
<tr>
<td class="formfield"><asp:Tex
</tr>
<tr>
<td class="formfield"><asp:Che
</tr>
<tr>
<td class="formfield"><asp:Lin
</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_ItemComma
dim id as String
id = GetEncVal("id")
dim strSQL as String
dim cbDelete as CheckBox
cbDelete = e.Item.FindControl("deladd
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("editad
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?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER