In an asp.net application, I have the follow code in the aspx page:
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<% foreach (var questionPaper in QuestionPapers) { %>
<div style="border-bottom: 1px solid; padding-bottom:20px">
<%= questionPaper.University %><br/>
<%= questionPaper.CourseName %>: <%= questionPaper.CourseCode %><br/>
<%= questionPaper.Type %><br/>
<%= questionPaper.Year %><br/>
<asp:Button ID="View_Paper" runat="server" OnClick="ViewPaper" Text="View Paper"/>
</div>
<% } %>
</asp:Content>
I want to pass questionPaper.ID back to the ViewPaper() event handler on the server side, how do I do that?
public void ViewPaper(object sender, EventArgs e)
{
}
My recommendation is to use a Repeater or a ListView control
Use a Repeater if the data will be used as read-only
Use a ListView if you want to page your results and/or allow the end user to perform CRUD operation over your data
For example, your code would look like this when using a Repeater and an ObjectDataSource controls:
ASPX
<asp:ObjectDataSource ID="ods" runat="server"
SelectMethod="GetQuestionPapers"
TypeName="Your_Namespace.PapersRepository">
</asp:ObjectDataSource>
<asp:Repeater runat="server" DataSourceID="ods" ID="r" OnItemCommand="r_ItemCommand">
<ItemTemplate>
<div style="border-bottom: 1px solid; padding-bottom:20px">
<asp:HiddenField runat="server" ID="paperID" Value='<%# Eval("PaperID") %>'/>
<asp:Label runat="server" ID="university" Text='<%# Eval("University") %>'/><br />
<asp:Label runat="server" ID="courseName" Text='<%# Eval("CourseName") %>'/>:<asp:Label runat="server" ID="courseCode " Text='<%# Eval("CourseCode ") %>'/><br />
<asp:Label runat="server" ID="paperType" Text='<%# Eval("Type") %>' /><br />
<asp:Label runat="server" ID="year" Text='<%# Eval("Year") %>' /><br />
<asp:Button ID="View_Paper" runat="server" Text="View Paper" CommandName="ViewPaperCommand"
/>
</div>
</ItemTemplate>
<SeparatorTemplate>
<hr />
</SeparatorTemplate>
</asp:Repeater>
ASPX code behind
protected void r_ItemCommand(object sender, RepeaterCommandEventArgs e)
{
switch (e.CommandName)
{
case "ViewPaperCommand":
var hidden = e.Item.FindControl("paperID") as HiddenField;
var myPaperID = hidden.Value;
break;
}
}
Papers repository class
namespace Your_Namespace
{
public class PapersRepository
{
public IEnumerable < QuestionPaper > GetQuestionPapers()
{
return QuestionPapers.AsEnumerable();
}
}
}
You could bind it to a control and then reference that control directly?
I know you have multiple questions papers being output, so that in itself is the issue. It's been a while since I have used webforms, but could you not use a repeater control which would give you a row context within which you could get the specific question paper id you are looking for?
Use a repeater. That kind of old skool ASP classic code has no place in ASP.Net projects. Here's an example: http://www.dotnetcurry.com/ShowArticle.aspx?ID=663
It is possible to customise a repeater to make a lightweight DataGrid or ListView equivalent too.
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 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" />
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
change repeater li item class if first or last
Im working on a .net website which I've never worked with before.
I have the following that outputs 4 divs...
<asp:Repeater ID="RT_Ranges" runat="server">
<ItemTemplate>
<div class="item">
<h4>
<%# Eval("Category_Name")%></h4>
<asp:Image ID="Image1" ImageUrl='<%# Eval("Img") %>' runat="server" Height="92" Width="79" />
<p>
<%# Eval("des") %></p>
<asp:HyperLink ID="HL_More" NavigateUrl='<%# Eval("nav") %>' runat="server">View More</asp:HyperLink>
</div>
</ItemTemplate>
</asp:Repeater>
I want to add a class to the last DIV outputted from the above, is this possible and if so how would I do it?
You have your answer here
Make it like:
<asp:Repeater ID="RT_Ranges" runat="server">
<ItemTemplate>
<div class="<%# GetItemClass(Container.ItemIndex) %>">
<h4>
<%# Eval("Category_Name")%></h4>
<asp:Image ID="Image1" ImageUrl='<%# Eval("Img") %>' runat="server" Height="92" Width="79" />
<p>
<%# Eval("des") %></p>
<asp:HyperLink ID="HL_More" NavigateUrl='<%# Eval("nav") %>' runat="server">View More</asp:HyperLink>
</div>
</ItemTemplate>
</asp:Repeater>
and on your code-behind:
protected string GetItemClass(int itemIndex)
{
if (itemIndex == this.ItemCount - 1)
return "CSS_for_last_item";
}
You can do it on the server:
in code behind after data is bound
or on the client:
using pure CSS (see :last-child selector)
using script (see jQuery :last-child selector)
You can't do it. At least this is not easy.
If you need this class for styling reasons then use the :last-child pseudoclass instead and if you need this in javascript then there are other methods of getting the last child.
You could use a Panel instead (which is rendered as div). Then you can use this code to get the last item and find the panel:
RepeaterItem lastRepeaterItem = RT_Ranges.Items[RT_Ranges.Items.Count - 1];
Panel lastPanel = (Panel)lastRepeaterItem.FindControl("PanelID");
lastPanel.CssClass = "yourclass";
Change class attribute value as follows,
<div class="<%# GetItemClass(Container.ItemIndex) %>">
And create GetItemClass method in code behid,
protected string GetItemClass(int itemIndex)
{
if (itemIndex == this.ItemCount - 1)
return "last";
else
return "other";
}
Also refer
Change repeater li item class if first or last
<asp:Repeater ID="RT_Ranges" runat="server">
<ItemTemplate>
<div <%# (Container.DataItemIndex != 0 && (Container.DataItemIndex+1) % 4 == 0) ? #"class='myClass'" : #"class='item'" %>>
<h4>
<%# Eval("Category_Name")%></h4>
<asp:Image ID="Image1" ImageUrl='<%# Eval("Img") %>' runat="server" Height="92" Width="79" />
<p>
<%# Eval("des") %></p>
<asp:HyperLink ID="HL_More" NavigateUrl='<%# Eval("nav") %>' runat="server">View More</asp:HyperLink>
</div>
</ItemTemplate>
</asp:Repeater>
Or using jQuery:
$("table.RT_Ranges div:last").attr("class", "last");
I need to display a list of clients, but display them differently based on a parameter.
To do this, I have a gridvew, and inside there is a user control. That control has an "if" based on the type.
My problems:
If I add a button inside the control, when it is pressed I get a button validation error.
If I disable validation errors (enableEventValidation="false"), I get button commands to work, but I'm not able to change values on the control either with full postbacks or an updatepanel.
<asp:GridView ID="gvClients" runat="server" AutoGenerateColumns="False" >
<Columns>
<asp:TemplateField>
<ItemTemplate>
<xxx:ClientListGridItem ID="ClientListItem1" runat="server" Client='<%# ((Client) Container.DataItem) %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
ClientListGridItem.ascx :
<% if (Client.Style >= 100)
{
%>
<div class="ClientListItem1">
...
<%
}
else
{
%>
<div class="ClientListItem2">
<asp:Button ID="Button2" runat="server" onclick="Button1_Click" Text="Button" />
...
<%
}
%>
I'm sure there is prettier, more object oriented way to do this too...
Changing ClientListGridItem.ascx into:
<asp:Panel id="Div1" CssClass="ClientListItem1" runat="server">
...
</asp:Panel>
<asp:Panel id="Div2" CssClass="ClientListItem2" runat="server">
<asp:Button ID="Button2" runat="server" onclick="Button1_Click" CausesValidation="false" Text="Button" />
..
</asp:Panel>
<script runat="server">
override void OnDataBinding(EventArgs e) {
Div1.Visible = Client.Style >= 100;
Div2.Visible = ! Div1.Visible;
}
</script>
should work.
I am using asp.net 3.5 with c#.I want to invoke button click event inside repeater control.
<asp:Repeater ID="rptFriendsList"
runat="server"
onitemcommand="rptFriendsList_ItemCommand">
<ItemTemplate>
<asp:ImageButton ID="btnSave"
runat="server"
ImageUrl="~/Contents/Images/save_button.png"
CommandName="Schedule"
UseSubmitBehavior="False" />
</ItemTemplate>
</asp:Repeater>
but when i click to a button its giving an error
"Invalid postback or callback
argument. Event validation is enabled
using in
configuration or <%# Page
EnableEventValidation="true" %> in a
page. For security purposes, this
feature verifies that arguments to
postback or callback events originate
from the server control that
originally rendered them. If the data
is valid and expected, use the
ClientScriptManager.RegisterForEventValidation
method in order to register the
postback or callback data for
validation."
my purpose is to execute some code in button click which is placed inside the repeater.Please help me to solve this issue.Thanks in advance.
UseSubmitBehavior="False" this property you have used is not present with the image button have you over ridden imagebutton class and added this property.
<asp:Repeater ID="Repeater1" runat="server" OnItemCommand="Repeater1_OnItemCommand" DataSourceID="SqlDataSource1">
<ItemTemplate>
key1:
<asp:Label ID="key1Label" runat="server" Text='<%# Eval("key1") %>'></asp:Label><br />
key2:
<asp:Label ID="key2Label" runat="server" Text='<%# Eval("key2") %>'></asp:Label><br />
key3:
<asp:Label ID="key3Label" runat="server" Text='<%# Eval("key3") %>'></asp:Label><br />
<asp:TextBox ID="col1" runat="server" Text='<%# Eval("col1") %>'></asp:TextBox>
<asp:TextBox ID="col2" runat="server" Text='<%# Eval("col2") %>'></asp:TextBox>
<br />
<asp:linkbutton ID="Linkbutton1" commandname="Update" runat="server" text="Update" CommandArgument='<%# Eval("key1") +"|"+Eval("key2")+"|"+ Eval("key3") %>' />
<asp:linkbutton ID="Linkbutton2" commandname="Cancel" runat="server" text="Cancel" />
</ItemTemplate>
protected void Repeater1_OnItemCommand(object source, RepeaterCommandEventArgs e)
{
if (e.CommandName == "Update")
{
string col1 = ((TextBox)e.Item.FindControl("col1")).Text;
string col2 = ((TextBox)e.Item.FindControl("col2")).Text;
string allKeys = Convert.ToString(e.CommandArgument);
string[] arrKeys = new string[2];
char[] splitter = { '|' };
arrKeys = allKeys.Split(splitter);
SqlDataSource1.UpdateParameters["col1"].DefaultValue = col1;
SqlDataSource1.UpdateParameters["col2"].DefaultValue = col2;
SqlDataSource1.UpdateParameters["key1"].DefaultValue = arrKeys[0];
SqlDataSource1.UpdateParameters["key2"].DefaultValue = arrKeys[1];
SqlDataSource1.UpdateParameters["key3"].DefaultValue = arrKeys[2];
SqlDataSource1.Update();
Repeater1.DataBind();
}
}
This also happens when you have assigned the datasource and data bound your repeater in the OnLoad event rather than OnInit
I have used this bellow code and runs ok
use this bellow code in .aspx page
<asp:Repeater ID="Repeater1" runat="server" OnItemCommand="Repeater1_ItemCommand">
<HeaderTemplate>
<table>
<tr>
<th>
Edit
</th>
</tr>
</table>
</HeaderTemplate>
<ItemTemplate>
<table>
<tr>
<td align="center">
<asp:LinkButton ID="LinkButton1" runat="server" CommandName="Edit">Edit</asp:LinkButton>
</td>
</tr>
</table>
</ItemTemplate>
</asp:Repeater>
Use this in .cs Make the event Repeater1_ItemCommand
protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
{
switch (e.CommandName)
{
case "Edit":
// Do some stuff when the Edit button is clicked.
break;
// Other commands here.
default:
break;
}
}
You can't use button, because button create postback on click and also itemcommand of repeater called!
But, If you want to use asp:button instead of asp:linkbutton, you have to set UseSubmitBehavior property of button to false. its means, button dont make postback.
<asp:Button ID="btnAccept" runat="server" Text="Accept All" CssClass="vb-default vb-green vb-txt-light" CommandName="Accept" CommandArgument='<%# Eval("UserID") %>' UseSubmitBehavior="false" />
Set page EnableEventValidation="false".
if you're adding items server side, try to assign unique ID to each ImageButton