I have a webform that contains a listview like this:
<asp:ListView ID="SlidesListView" runat="server" OnItemCommand="SlidesListView_ItemCommand">
<LayoutTemplate>
<span runat="server" id="itemPlaceholder" style="display: inline-block" />
</LayoutTemplate>
<ItemTemplate>
<div class="SlideDiv">
<div>
<asp:ImageButton ID="ChangeDescriptionImageButton" runat="server" ImageUrl="~/Image/Change.png" CommandArgument="<%# Eval("ID") %>" CommandName="Change" AlternateText="Change" ToolTip="Change" />
<asp:Image ID="Image1" runat="server" CssClass="MiniSliderPic" ImageUrl='<%#Eval("ImagePath") %>' AlternateText='<%#Eval("Description") %>' ToolTip='<%#Eval("Description") %>' />
<p><%#Eval("Description").ToString().Replace(Environment.NewLine, "<br/>").Replace("\n", "<br />") %></p>
</div>
</div>
</span>
</ItemTemplate>
</asp:ListView>
and codebehind:
protected void SlidesListView_ItemCommand(object sender, ListViewCommandEventArgs e)
{
int ID = 0;
switch (e.CommandName)
{
case "Change":
ID = Convert.ToInt32(e.CommandArgument.ToString());
break;
}
}
but when I try to open this webform , I face this error:
An error occurred during the parsing of a resource required to service this request. Please review the following specific parse error details and modify your source file appropriately.
the server tag is not well formed
and asp:ImageButton tag get red.why?
Change this:
CommandArgument="<%# Eval("ID") %>"
To this:
CommandArgument='<%# Eval("ID") %>'
BTW, I don't think you can use imagebutton this way. It will fire it's won OnCommand rather than ListView's ItemCommand. Consider using a linkbutton with image inside it.
<asp:LinkButton ID="ChangeDescriptionLinkButton" runat="server"
CommandName="Change"
CommandArgument='<%# Eval("ID") %>' >
<img src="~/Image/Change.png" alt="Change" />Change
</asp:LinkButton>
Related
I need the button inside the datalist to open another page. However the response.redirect does not work !
here is the html
<asp:Content ID="Content3" ContentPlaceHolderID="ContentPlaceHolder2" runat="Server">
<div id="content">
<!-- Review -->
<div class="products">
<h3>My Books</h3>
<h4>Items you have purchased</h4>
<br />
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>"></asp:SqlDataSource>
<asp:DataList ID="DataList1" runat="server" DataSourceID="SqlDataSource1" Width="618px" Height="114px">
<ItemTemplate>
<asp:Label ID="bookid" runat="server" Text='<%# Eval ("BookID") %>' Visible=" false"></asp:Label>
<asp:Label ID="bookname" runat="server" Text='<%# Eval ("Title") %>'></asp:Label>
<asp:Button ID="review" runat="server" Text="Review" CommandName="review" />
</ItemTemplate>
</asp:DataList>
<br />
</div>
</div>
below is the cs file
public void DataList1_ItemCommand(Object source, DataListCommandEventArgs e)
{
if (e.CommandName == "review")
{
DataList1.SelectedIndex = e.Item.ItemIndex;
Label bookid = (Label)DataList1.SelectedItem.FindControl("Bookid");
Response.Redirect("Review.aspx");
}
}
You still need to attach ItemCommand event to DataList1.
<asp:DataList OnItemCommand="DataList1_ItemCommand" ...>
...
</asp:DataList>
Try Server.Transfer("Review.aspx", true);
I have a Repeater which contains Eval commands and a Button (Which I can't access) I would like to access this button and when clicked insert one of the Eval commands (Eg. Car Model) into a listbox I have on the same page.
<asp:Button ID="modelButton" CommandArgument='<%# Eval("Model") %>' Text='<%# "Add Model to List:" + Eval("Model") %>' runat="server" />
<asp:Repeater ID="Repeater2" runat="server" DataSourceID="AccessDataSource1" DataMember="DefaultView" OnItemDataBound="Repeater2_ItemDataBound">
<ItemTemplate>
<div>
<p><img src="carImages/<%#Eval("Artwork")%>" /></div>
<div class="col-xs-4">
<h4><%# Eval("Make")%> (<%# Eval("Year") %>)</h4>
<p><%# Eval("Model")%></p>
<p><%# Eval("Colour")%></p>
<p><%# Eval("Type")%></p>
<div>
<asp:Button ID="modelButton" CommandArgument='<%# Eval("Model") %>' Text='<%# "Add Model to List:" + Eval("Model") %>' runat="server" />
</div>
</a>
</div>
</ItemTemplate>
Just add your oncommand argument to the repeated button
<asp:Button ID="modelButton" CommandArgument='<%# Eval("Model") %>' OnCommand="CommandBtn_Click" Text='<%# "Add Model to List:" + Eval("Model") %>' runat="server" />
you can grab the eval in the behind code
void CommandBtn_Click(Object sender, CommandEventArgs e){
var command = e.CommandArgument;
// Do whatever with it here
}
That will get the evaluated model
I have a aspx page called bookscategory.aspx with this markup:
<asp:Repeater ID="Repeater1" runat="server">
<HeaderTemplate>
<p>Books list</p>
</HeaderTemplate>
<ItemTemplate>
<h3><asp:Literal ID="Literal1" runat="server" Text='<%# Eval("Category") %>'></asp:Literal></h3>
<asp:Repeater ID="Repeater1" runat="server" DataSource='<%# Eval("Reviews") %>' ItemType="ELibraryModel.Review">
<ItemTemplate>
<asp:HyperLink ID="HyperLink1" runat="server" Text='<%# Item.Title%>' NavigateUrl='<%# "../books/bookdetails.aspx?bookId=" + Item.Id.ToString() %>'>
</asp:HyperLink><br />
</ItemTemplate>
</asp:Repeater>
</ItemTemplate>
</asp:Repeater>
Code behind:
using (ELibraryEntities entities = new ELibraryEntities())
{
var allbooks = from books in entities.Books.Include("Reviews")
orderby books.Category
select new { books.Category, books.Reviews};
Repeater1.DataSource = allbooks;
Repeater1.DataBind();
}
Now on my Default.aspx page I have a book image:
<div class="prod_img">
<img src="../Images/asp.net_image.jpg" alt="" title="" border="0" /></a>
</div>
<div class="box_center">
<div class="prod_title">ASP.NET Book</div>
<p class="details">ASP.NET Book.</p>
- read more -
<div class="clear"></div>
</div>
Finally, when I click on this image I want to navigate to that exact review:
NavigateUrl='<%# "../books/bookdetails.aspx?ReviewId=" + Item.Id.ToString()
Everything works fine with asp:HyperLink-s but instead I want to use images.
I'm reading Beginning ASP.NET 4.5 in C# and VB and here you can see all reviews http://aspnet45.planetwrox.com/Reviews/All.aspx. I want to use Images instead HyperLinks.
HyperLink control already provides this capabilty - it has ImageUrl property, and when this is set, HyperLink appears as an image:
<asp:HyperLink runat="server" ID="HyperLink1"
NavigateUrl='<%# "../books/bookdetails.aspx?ReviewId=" + Item.Id.ToString() %>'
ImageUrl="~/url/to/image" />
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;
}
Just want to ask how can I find hiddenfield in repeater because my problem I have button and I want to get the associate hiddenfield inside ItemTemplate because I get null value when I try to get hiddenfield value
<asp:Repeater ID="rp_resList" runat="server" OnItemDataBound="rp_resList_ItemDataBound">
<ItemTemplate>
<div class="resourcesResult">
<asp:HiddenField ID="hf_resID" runat="server" Value='<%# Eval("Id") %>' />
<a href='<%# Eval("pageID") %>'><%# Eval("name") %></a>
<br />
<asp:Literal ID="litSummary" runat="server" Text='<%# Eval("summary") %>'></asp:Literal>
<br />
<%-- <asp:Repeater ID="rp_tagsSkill" runat="server">
<ItemTemplate>
<h6>
<%# Eval("Description") %>
</h6>
</ItemTemplate>
</asp:Repeater>--%>
<asp:Repeater ID="rp_tagsTopics" runat="server">
<ItemTemplate>
<h6>
<%# Eval("Description") %>
</h6>
</ItemTemplate>
</asp:Repeater>
<div id="controls">
<asp:ImageButton ID="imgbtnBookmark" runat="server" OnClick="imgbtnBookmark_Click" />
<asp:DropDownList ID="ddlGroup" runat="server" DataSourceID="SqlDS_Groups" DataTextField="name" DataValueField="id" AppendDataBoundItems="True" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged" >
<asp:ListItem Value="-1">Select Group</asp:ListItem>
protected void imgbtnBookmark_Click(object sender, System.Web.UI.ImageClickEventArgs e)
{
Bookmark bm = new Bookmark();
HiddenField hiddenField = rptGroup.FindControl("hf_resID") as HiddenField;
bm.UserID =
Guid.Parse(Membership.GetUser(HttpContext.Current.User.Identity.Name).ProviderUserKey.ToString());
bm.Resoursce.ResourceID = Convert.ToInt32(hiddenField.Value);
Bookmark.Insert(bm);
}
Try this, change you button tag to pass id as CommandArgument value
<asp:ImageButton ID="imgbtnBookmark" runat="server"
OnClick="imgbtnBookmark_Click"
CommandArgument='<%# Eval("Id") %>'/>
in your button click event you can access id using
bm.Resoursce.ResourceID = Convert.ToInt32(e.CommandArgument.ToString());
Change rptGroup.FindControl("hf_resID") as HiddenField; to
e.Item.FindControl("hf_resID")....
I hope this link will help you .
jquery
function showid(dllval) {
var ID = $(dllval).parent().parent().find('[id*="hiddenID"]').val();
alert(ID)
}
asp.net
<asp:DropDownList ID="ddl" runat="server" onclick="showid(this);" >
</asp:DropDownList>
<asp:HiddenField ID="hiddenID" runat="server" Value='<%#Eval("ID")%>' />