How can I pass '<%# Eval("Url") %>' as a CommandArgument to my
protected void ImageButton_Command(object sender, CommandEventArgs e)
{
var link = (string) e.CommandArgument;
}
method? Whenever I do it like below I receive empty e.CommandArgument.
<ItemTemplate>
<td runat="server" style="background-color: #FFFBD6; color: #333333;">
<asp:ImageButton
ID="Column1Label"
runat="server"
CommandArgument='<%#Eval("Url") %>'
OnCommand="ImageButton_Command"
ImageUrl='<%# Eval("Column1") %>'
Width="300"
Height="300"
CssClass="ListItem"
EnableViewState="False"
CommandName="Sort"></asp:ImageButton>
<br/>
</td>
</ItemTemplate>
Thanks in advance.
It's because of this: EnableViewState="False"
Turn View State back on (delete that line) and it should work.
Related
I want to pass the facility_id between pages. However, the link button in the data list does not work.
May I know why it does not work and how to make it work?
Thank you in advance.
Here is the code from the .aspx file:
<asp:DataList ID="DataList1" runat="server" DataSourceID="SqlDataSource1" RepeatColumns="2" RepeatDirection="Horizontal" HorizontalAlign="Center" Width="70%" DataKeyField="facility_id" OnItemCommand="DataList1_ItemCommand">
<ItemTemplate>
<asp:Label ID="facility_idLabel" runat="server" Text='<%# Eval("facility_id") %>' Visible="False" />
<table style="width:100%;">
<tr>
<td style="height: 53px; width: 500px; text-align: right;">
<asp:Label ID="Label2" runat="server" CssClass="ClientFacility2ndTitle" Text='<%# Eval("facility_name") %>'></asp:Label>
</td>
<td class="lblLogin" style="height: 53px; width: 220px">
<asp:LinkButton ID="LinkButton1" runat="server" BorderStyle="None" CssClass="Button" CommandName="BookNow">Book Now</asp:LinkButton>
</td>
</tr>
</table>
</ItemTemplate>
</asp:DataList>
</div>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:WebConfigcs %>" SelectCommand="SELECT [facility_id], [facility_name] FROM [facility]"></asp:SqlDataSource>
Meanwhile for the code in aspx.cs:
public partial class ClientFacility : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
{
if(e.CommandName == "BookNow")
{
MessageBox.Show(e.CommandArgument.ToString());
Response.Redirect("EditFacility.aspx?facility_id=" + e.CommandArgument.ToString());
}
}
}
you have a CommandName for the link button, but no commandArugment?
so:
<asp:LinkButton ID="LinkButton1" runat="server"
BorderStyle="None" CssClass="Button"
CommandName="BookNow"
CommandArgument = '<%# Eval("facility_id") %>'
>Book Now</asp:LinkButton>
So to pick up e.CommandArugment, you need to set that in the link button.
I am building a website right now using DataLists and I want to ask a question:
When using OnLoad on specific control in DataList item, the DataList has 5 items, but the function is called only 4 times (does what it needs to do but not on the last item)
C# code:
protected void ibDeleteAlbum_Load(object sender, EventArgs e)
{
foreach (DataListItem item in DataList1.Items)
{
ImageButton btnDelAlbum = item.FindControl("ibDeleteAlbum") as ImageButton;
btnDelAlbum.Visible = true;
}
}
HTML code:
<asp:DataList ID="DataList1" runat="server" RepeatDirection="Horizontal" RepeatColumns="4" >
<ItemTemplate>
<asp:Label ID="lblAlbumID" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "Id") %>' Visible="false"></asp:Label>
<asp:ImageButton ID="ibDeleteAlbum" type="image" runat="server" style="position:absolute; margin-right: 1.4vw; margin-top: 2.4vh;" Visible="false" ImageUrl="~/Images/ic_delete.png" OnClick="ibDeleteAlbum_Click" ToolTip="מחיקה" OnLoad="ibDeleteAlbum_Load" />
<asp:ImageButton ID="ibShowAlbums"
runat="server"
class="album"
ImageUrl='<%# DataBinder.Eval(Container.DataItem, "coverPhoto") %>'
CommandName="album"
CommandArgument='<%#DataBinder.Eval(Container.DataItem, "ID") %>'
OnCommand="ibShowAlbums_Command" />
<div class="albumNameShow" runat="server"><%#DataBinder.Eval(Container.DataItem, "albumName") %></div>
</ItemTemplate>
</asp:DataList>
Thanks ahead!
I bind my data to listview using eval. like that :
<telerik:RadListView ID="lvDSTableSelection" runat="server" AllowMultiItemSelection="true" OnSelectedIndexChanged="lvDSTableSelection_SelectedIndexChanged" EnableViewState="true" >
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" CssClass="selectedButtons" runat="server" CommandName="Select" Font-Underline="false" >
<p ><%# Container.DataItem %></p>
</asp:LinkButton>
</ItemTemplate>
<SelectedItemTemplate>
<p ><%# Container.DataItem %></p>
</SelectedItemTemplate>
</telerik:RadListView>
How can I get selecteditems data at code behind. I tried this in selecteditemchanged event
lvDSTableSelection.SelectedItems[0].DataItem.ToString() ;
When I debugged code I realizde that my SelectedItems[0] count is 1 but SelectedItems[0].DataItem is null. I suppose it is about with usage of eval.
How can I get selecteditems data from codebehind?
I have tried with your code but not able to reproduce this issue.
Please try with the below code snippet.
ASPX
<telerik:RadListView ID="lvDSTableSelection" runat="server" AllowMultiItemSelection="true" EnableViewState="true">
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" CssClass="selectedButtons" runat="server" CommandName="Select" CommandArgument='<%# Eval("ID") %>' Font-Underline="false" OnClick="lvDSTableSelection_SelectedIndexChanged">
<p ><%# Container.DataItem %></p>
</asp:LinkButton>
</ItemTemplate>
<SelectedItemTemplate>
<p><%# Container.DataItem %></p>
</SelectedItemTemplate>
</telerik:RadListView>
ASPX.CS
protected void lvDSTableSelection_SelectedIndexChanged(object sender, EventArgs e)
{
string str1 = (sender as LinkButton).CommandArgument;
}
It seems I have had a lot of questions today.
What i want to do is save the text for each image.
I'm getting the right text for each picture into the textboxes at the moment.
The code behind:
var car = GarageBLL.LoadCar(Convert.ToInt32(CarId),
Convert.ToInt32(_memberId)); ImageRepeater.DataSource = car.Images;
ImageRepeater.DataBind();
protected void FinalizeNewCar(object sender, EventArgs e) {
Response.Redirect("/amcargarasjen"); }
**Code here for saving each edit into the right ImageId.**
ASP:
<asp:Repeater runat="server" ID="ImageRepeater">
<ItemTemplate>
<table>
<tr>
<td>
<a class="deleteLink" href="#" rel="<%#Eval("ImageId")%>" title="">
<asp:Image runat="server" ImageUrl="/Content/Images/Garage/DeleteButton.png" /></a>
<asp:Image Width="60" Height="45" ID="ImgCar" ImageUrl='<%# String.Format("/garageimages/{0}/{1}.{2}", CarId, Eval("ImageId"), Eval("Extension")) %>' runat="server" />
<asp:TextBox runat="server" Text='<%# Eval("Description") %>' ID="txtText"></asp:TextBox>
</td>
</tr>
</table>
</ItemTemplate>
</asp:Repeater>
<br />
<asp:Button ID="Button1" runat="server" Text="Fullfør" OnClick="FinalizeNewCar" />
Anyone got any ideas on how to do that?
Modify your Repeater HTML markup like below. Note the addition of a Hidden field to keep the reference of the current image ID.
<asp:Repeater runat="server" ID="ImageRepeater">
<ItemTemplate>
<table>
<tr>
<td>
<a class="deleteLink" href="#" rel="<%#Eval("ImageId")%>" title="">
<asp:Image runat="server" ImageUrl="/Content/Images/Garage/DeleteButton.png" /></a>
<asp:Image Width="60" Height="45" ID="ImgCar" ImageUrl='<%# String.Format("/garageimages/{0}/{1}.{2}", CarId, Eval("ImageId"), Eval("Extension")) %>' runat="server" />
<asp:TextBox runat="server" Text='<%# Eval("Description") %>' ID="txtText"></asp:TextBox>
<asp:HiddenField runat="server" Value='<%# Eval("ImageId") %>' ID="txtImageId"></asp:TextBox>
</td>
</tr>
</table>
</ItemTemplate>
</asp:Repeater>
<br />
<asp:Button ID="Button1" runat="server" Text="Fullfør" OnClick="FinalizeNewCar" />
CODE FinalizeNewCar Event Handler
protected void FinalizeNewCar(object sender, EventArgs e)
{
foreach (RepeaterItem item in ImageRepeater.Items)
{
Int32 imageId = Convert.ToInt32(((HiddenField) item.FindControl("txtImageId")).Value);
string description = ((TextBox)item.FindControl("txtText")).Text;
//You will get the imageId description here.
//Write your code to update the datatbase.
}
}
I am using listview to display a list of items and a nested listview to show list of features to each item. Both parent and child listview need to able Insert,Edit and delete operation. It works fine for parent listview. But when I try to edit an child item, The edit button does not take it into Edit mode. Can you please suggest me what I am missing in my code?
<asp:ListView ID="lvParent" runat="server"
OnItemDataBound="lvParent_ItemDataBound"
onitemcanceling="lvParent_ItemCanceling" onitemcommand="lvParent_ItemCommand"
DataKeyNames="ItemID" onitemdeleting="lvParent_ItemDeleting"
oniteminserting="lvParent_ItemInserting" >
<LayoutTemplate>
<asp:PlaceHolder ID="itemPlaceholder" runat="server"></asp:PlaceHolder>
<div align="right">
<asp:Button ID="btnInsert" runat="server" Text="ADD Item" onclick="btnInsert_Click"/>
</div>
</LayoutTemplate>
<ItemTemplate>
<table runat="server" cellpadding="0" cellspacing="0" border="0" width="100%">
<tr>
<td>
<div id="dvDetail">
<span >Description</span>
<asp:TextBox ID="txtDescription" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "Description") %>' TextMode="MultiLine" ></asp:TextBox>
</div>
<div id="dvFeature" >
<span>Feature List</span>
<asp:ListView ID="lvChild" runat="server"
InsertItemPosition="LastItem"
DataKeyNames="FeatureID" OnItemCommand="lvChild_ItemCommand"
OnItemCanceling="lvChild_ItemCanceling" OnItemDeleting="lvChild_ItemDeleting"
OnItemEditing="lvChild_ItemEditing" OnItemInserting="lvChild_ItemInserting" OnItemUpdating="lvChild_ItemUpdating"
DataSource='<%# DataBinder.Eval(Container.DataItem, "FeatureList") %>' >
<LayoutTemplate>
<ul >
<asp:PlaceHolder runat="server" ID="itemPlaceHolder" ></asp:PlaceHolder>
</ul>
</LayoutTemplate>
<ItemTemplate>
<li>
<span class="dvList"><%# DataBinder.Eval(Container.DataItem, "FeatureTitle")%></span>
<div class="dvButton" >
<asp:ImageButton ID="btnEdit" runat="server" ImageUrl="/Images/edit_16x16.gif" AlternateText= "Edit" CommandName="Edit" CommandArgument='<%# DataBinder.Eval(Container.DataItem, "FeatureID") %>' Width="12" Height="12" />
<asp:ImageButton ID="btnDelete" runat="server" ImageUrl="/Images/delete_16x16.gif" AlternateText= "Delete" CommandName="Delete" CommandArgument='<%# DataBinder.Eval(Container.DataItem, "FeatureID") %>' Width="12" Height="12" />
</div>
</li>
</ItemTemplate>
<EditItemTemplate>
<li>
<asp:TextBox ID="txtFeature" Text='<%# DataBinder.Eval(Container.DataItem, "FeatureTitle")%>' runat="server"></asp:TextBox>
<div class="dvButton">
<asp:ImageButton ID="btnUpdate" runat="server" ImageUrl="/Images/ok_16x16.gif" AlternateText= "Update" CommandName="Update" CommandArgument='<%# DataBinder.Eval(Container.DataItem, "FeatureID") %>' Width="12" Height="12" />
<asp:ImageButton ID="btnCancel" runat="server" ImageUrl="/Images/delete_16x16.gif" AlternateText= "Cancel" CommandName="Cancel" Width="12" Height="12" CausesValidation="false" />
</div>
</li>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="txtFeature" runat="server"></asp:TextBox>
<div class="dvButton">
<asp:ImageButton ID="btnInsert" runat="server" ImageUrl="/Images/ok_16x16.gif" AlternateText= "Insert" CommandName="Insert" Width="12" Height="12" />
<asp:ImageButton ID="btnCancel" runat="server" ImageUrl="/Images/delete_16x16.gif" AlternateText= "Cancel" CommandName="Cancel" Width="12" Height="12" CausesValidation="false" />
</div>
</InsertItemTemplate>
</asp:ListView>
</div>
</td>
</tr>
<tr>
<td align="right">
<div id="dvButton" >
<asp:Button ID="btnSave" runat="server" Text="Save"
CommandName="Save"
CommandArgument='<%# DataBinder.Eval(Container.DataItem, "ItemID") %>' />
<asp:Button ID="btnDelete" runat="server" Text="Delete" CssClass="Cancel"
CommandName="Delete"
CommandArgument='<%# DataBinder.Eval(Container.DataItem, "ItemID") %>' />
</div>
</td>
</tr>
</table>
</ItemTemplate>
</asp:ListView>
Code Behind:
protected void Page_Load(object sender, EventArgs e)
{
if (Page.IsPostBack == false)
{
BindData();
}
}
private void BindData()
{
MyDataContext data = new MyDataContext();
var result = from itm in data.ItemLists
where itm.ItemID == iItemID
select new
{
itm.ItemID,
itm.Description,
FeatureList = itm.Features
};
lvParent.DataSource = result;
lvParent.DataBind();
}
protected void lvChild_ItemEditing(object sender, ListViewEditEventArgs e)
{
ListView lvChild = sender as ListView;
lvChild.EditIndex = e.NewEditIndex;
lvChild.DataBind();
}
Edit:
protected void lvChild_ItemEditing(object sender, ListViewEditEventArgs e)
{
ListView lvChild = sender as ListView;
lvChild.EditIndex = e.NewEditIndex;
lvChild.DataBind();
}
If I use "lvChild.DataBind()" in 'ItemEditing' event, the total list of child items goes away if I click 'edit'
protected void lvChild_ItemEditing(object sender, ListViewEditEventArgs e)
{
ListView lvChild = sender as ListView;
lvChild.EditIndex = e.NewEditIndex;
}
if I get rid of 'lvChild.Databind' in ItemEditing event, it goes to Edit mode after clicking the 'edit' button twice . And though it shows textbox control of EditItemTemplate, it appears as a blank textbox (does not bind existing value to edit).
This is an interesting problem. Almost certainly a databinding issue. In order to enter edit mode you must do two things:
1) Set the EditIndex
2) Call DataBind()
In the case of nested repeaters though... when does Render get called? I suspect you will have to call DataBind() on the PARENT in order to render everything correctly. That being the case you may have to then set the EditIndex AGAIN, since you are rebinding the parent.
EDIT:
OK... I just tried this with a nested GridView and I did NOT have to DataBind() the parent to get the sub grid to enter edit mode. Now I have to downvote my own answer. :|
hope that will serve someone, somewhere.
Here is my code to get that to work:
1) I have a Listview wich hold a user control when editing. This User cotnrol has itself a listview inside
<asp:ListView runat=server ID=C_LV_MyObjects DataKeyNames="Id"
OnItemDataBound=DataBoundMyObjects OnItemEditing=ItemEditing
>
<LayoutTemplate>
<table runat=server id="itemPlaceholderContainer">
<tr>
<th>
Description
</th>
</tr>
<tr runat="server" id="itemPlaceholder">
</tr>
</table>
</LayoutTemplate>
<ItemTemplate>
<tr>
<td>
text...
</td>
<td>
<asp:LinkButton runat="server" CommandName="Edit" Text="Edit"></asp:LinkButton>
</td>
<td>
<asp:LinkButton runat="server" CommandName="Delete" Text="Delete"></asp:LinkButton>
</td>
</ItemTemplate>
<EditItemTemplate>
<tr>
<td colspan=3>
<MyTag:MyUC ID=C_UC_MyUserControl runat=server
OnEditing=MyObjectEditing
/>
</td>
</tr>
</EditItemTemplate>
<EmptyDataTemplate>
No results found!
</EmptyDataTemplate>
</asp:ListView>
The code c# for this listview is as follows :
public int EditIndexComposition;
protected void ItemEditing(object sender, ListViewEditEventArgs e)
{
C_LV_MyObjects.EditIndex = e.NewEditIndex;
C_LV_MyObjects.DataBind();
}
protected void MyObjectEditing(object sender, EventArgs e)
{
ListViewEditEventArgs MyEvent = (ListViewEditEventArgs)e;
if (MyEvent != null)
EditIndexComposition= MyEvent.NewEditIndex;
C_LV_MyObjects.DataBind();
}
protected void DataBoundMyObjects(object sender, ListViewItemEventArgs e)
{
MyUC uc = (MyUC)e.Item.FindControl("C_UC_MyUserControl");
if (uc!=null)
{
uc.EditIndex = EditIndexComposition;
ListViewDataItem dataItem = (ListViewDataItem)e.Item;
MyObject obj= (MyObject)dataItem.DataItem;
uc.DataSource=Myservice.GetDatasource(obj.Id);
uc.DataBind();
}
}
and the code of my Usercontrol is as follows :
<asp:PlaceHolder runat="server" ID="C_PH_ObjComposition">
<asp:ListView runat="server" ID="C_LV_AppaltatoreComposizione" DataSource="<% # DataSource %>"
DataKeyNames="Id" OnItemEditing="ItemEditing">
etc...
<ItemTemplate>
<tr>
<td>
<asp:LinkButton runat="server" CommandName="Edit" Text="Edit"></asp:LinkButton>
</td>
</tr>
</ItemTemplate>
<EditItemTemplate>
<tr>
<td>
Edit Mode
</td>
</tr>
</EditItemTemplate>
</asp:ListView>
</asp:PlaceHolder>
with the following code c# :
public int EditIndex
{
get {return C_LV_ObjComposition.EditIndex;}
set { C_LV_ObjComposition.EditIndex=value;}
}
public event EventHandler Editing;
protected void ItemEditing(object sender, ListViewEditEventArgs e)
{
C_LV_ObjComposition.EditIndex = e.NewEditIndex;
if (Editing != null)
Editing(this, e);
}
When clicking on the edit button of the innerlistview, we store the index that was clicked and we trigger a function in the first container user control. This function is going to store in a global value the index cliked and triggers a databind of the outter list. Doing so we get the onitemdatabound, that will recreate our usercontrol with the proper values, we can then before the databinding of the usercontrol assign the index of the editing row.
That's all if you have any questions , please feel free to answer..
ciao!