I wanted to use a GridView for binding a picture list. I can already do this, but I need to control how wide the grid goes.
If it was a table it would be easy like this:
<table>
<tr>
<td>Image 1</td>
<td>Image 2</td>
<td>Image 3</td>
</tr>
<tr>
<td>Image 4</td>
<td>Image 5</td>
<td>Image 6</td>
</tr>
<tr>
<td>Image 7</td>
<td>Image 8</td>
<td>Image 9</td>
</tr>
</table>
I want the "gridview" to not have any real columns or anything, just simply pictures with a text descriptor below them from my database. Is the repeater control better for this?
I would use a DataList control. The DataList control has very useful properties called RepeatColumns, and RepeatLayout which allow you to do what you need.
Markup:
<asp:DataList ID="DataList1" runat="server" RepeatColumns="3">
<ItemTemplate>
<img src="<%#Eval("url") %>" />
</ItemTemplate>
</asp:DataList>
Sample Class:
public class Sample
{
public string url { get; set; }
}
Binding DataList:
protected void Page_Load(object sender, EventArgs e)
{
List<Sample> samples = new List<Sample>();
samples.Add(new Sample() { url = "http://www.google.com/images/srpr/logo4w.png" });
samples.Add(new Sample() { url = "http://www.google.com/images/srpr/logo4w.png" });
samples.Add(new Sample() { url = "http://www.google.com/images/srpr/logo4w.png" });
samples.Add(new Sample() { url = "http://www.google.com/images/srpr/logo4w.png" });
samples.Add(new Sample() { url = "http://www.google.com/images/srpr/logo4w.png" });
samples.Add(new Sample() { url = "http://www.google.com/images/srpr/logo4w.png" });
this.DataList1.DataSource = samples;
this.DataList1.DataBind();
}
Result: A HTML table with 2 rows with 3 columns each showing the google image.
<table id="Table1" cellspacing="0" style="border-collapse: collapse;">
<tr>
<td>
<img src="http://www.google.com/images/srpr/logo4w.png" />
</td>
<td>
<img src="http://www.google.com/images/srpr/logo4w.png" />
</td>
<td>
<img src="http://www.google.com/images/srpr/logo4w.png" />
</td>
</tr>
<tr>
<td>
<img src="http://www.google.com/images/srpr/logo4w.png" />
</td>
<td>
<img src="http://www.google.com/images/srpr/logo4w.png" />
</td>
<td>
<img src="http://www.google.com/images/srpr/logo4w.png" />
</td>
</tr>
</table>
From my understanding of your question, you want to out put the images in a form other than a table.
A DataList controls has built-in features that would make your life easier. Namely, RepeatColumns, and RepeatLayout. #Hanlet's answer explains this very well. The problem with his answer, from my understanding of your question, is that he only shows how to output as a table. Which, as I explained earlier is not what you are looking for. The DataList can display the output in a different way: RepeatLayout="Flow". The potential problem with this approach is that the layout is rigid. It will not allow you to change how it is displayed. It uses <span> and <br /> tags to display the data. While this can be styled using CSS, it is still somewhat limiting. Here is how the output from Hanlet's answer would look if you choose Flow instead of Table :
<div>
<span id="DataList1">
<span>
<img src="http://www.google.com/images/srpr/logo4w.png" />
</span>
<span>
<img src="http://www.google.com/images/srpr/logo4w.png" />
</span>
<span>
<img src="http://www.google.com/images/srpr/logo4w.png" />
</span>
<br />
<span>
<img src="http://www.google.com/images/srpr/logo4w.png" />
</span>
<span>
<img src="http://www.google.com/images/srpr/logo4w.png" />
</span>
<span>
<img src="http://www.google.com/images/srpr/logo4w.png" />
</span>
</span>
</div>
All that being said, if you want the unlimited control over the layout that a Repeater provides, there is a way to do this. Using two Repeaters:
aspx code:
<asp:Repeater ID="rptRows" runat="server" OnItemDataBound="rptRows_ItemDataBound">
<ItemTemplate>
<asp:Repeater ID="rptItems" runat="server">
<ItemTemplate>
<img width="30px" src="<%#Eval("url") %>" />
</ItemTemplate>
</asp:Repeater>
</br>
</ItemTemplate>
</asp:Repeater>
code-behind:
public class ImageEntity
{
public string url { get; set; }
}
public partial class DataListSample : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
List<Tuple<ImageEntity, ImageEntity, ImageEntity>> listTuples = new List<Tuple<ImageEntity, ImageEntity, ImageEntity>>();
listTuples.Add(new Tuple<ImageEntity, ImageEntity, ImageEntity>(new ImageEntity() { url = "http://www.google.com/images/srpr/logo4w.png" }, new ImageEntity() { url = "http://www.google.com/images/srpr/logo4w.png" }, new ImageEntity() { url = "http://www.google.com/images/srpr/logo4w.png" }));
listTuples.Add(new Tuple<ImageEntity, ImageEntity, ImageEntity>(new ImageEntity() { url = "http://www.google.com/images/srpr/logo4w.png" }, new ImageEntity() { url = "http://www.google.com/images/srpr/logo4w.png" }, new ImageEntity() { url = "http://www.google.com/images/srpr/logo4w.png" }));
listTuples.Add(new Tuple<ImageEntity, ImageEntity, ImageEntity>(new ImageEntity() { url = "http://www.google.com/images/srpr/logo4w.png" }, new ImageEntity() { url = "http://www.google.com/images/srpr/logo4w.png" }, new ImageEntity() { url = "http://www.google.com/images/srpr/logo4w.png" }));
listTuples.Add(new Tuple<ImageEntity, ImageEntity, ImageEntity>(new ImageEntity() { url = "http://www.google.com/images/srpr/logo4w.png" }, new ImageEntity() { url = "http://www.google.com/images/srpr/logo4w.png" }, new ImageEntity() { url = "http://www.google.com/images/srpr/logo4w.png" }));
this.rptRows.DataSource = listTuples;
this.rptRows.DataBind();
}
protected void rptRows_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
{
Repeater rptItems = (Repeater)e.Item.FindControl("rptItems");
Tuple<ImageEntity, ImageEntity, ImageEntity> items = (Tuple<ImageEntity, ImageEntity, ImageEntity>)e.Item.DataItem;
rptItems.DataSource = new List<ImageEntity>() { items.Item1, items.Item2, items.Item3 };
rptItems.DataBind();
}
}
}
Basically, what we are doing here is splitting up the list of URLs into a List of Tuples. The length of the List is the number of "rows" in the outer Repeater. The number of entities in the Tuple is the number of "columns" in the inner Repeater.
Each time we bind an item to the outer Repeater (rptRows_ItemDataBound) , we set the datasource of the inner Repeater to a newly generated list of the URL entities (ImageEntity).
Let me know if this is clear enough, I would be happy to expand and clarify any points that are unclear.
you can use a data list or repeater inside the item template of a listview and and in listview's layout template you can make any layout, div based , table or unsorted list
Related
I am not getting label id in jquery code , i use anchor tag , it is working , the text changes to today's date on page load , but when i apply it to label control of asp.net , it doesn't work , search may pages on web , but doesn't get proper solution how to change text of label using java script or jquery , is here anybody can solve it.
here is the code to look out .
<div>
<div>
<table class="foruploadtable">
<tr>
<td>
<span class="foruploadtabletitle" >Share From Here .</span>
<hr />
</td>
</tr>
<tr>
<td>Max Size 1MB :</td><td>
<asp:FileUpload ID="FileUpload1" ToolTip="Select File By Clicking Browse ." CssClass="foruploadtableupload" runat="server" /></td>
</tr>
<tr>
<td>
<asp:Button ID="b1" runat="server" CssClass="foruploadtablebutton" ToolTip="Click Here To Upload." Text="Upload." OnClick="b1_Click" />
</td>
<td>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
<a id="me">hello</a>
</td>
</tr>
</table>
</div>
</div>
<script>
$(document).ready(function () {
var say = $('#me').text();
var date = new Date();
$('#me').text(date.getDate());
$('#Label1').text(date.getDate());
// alert(date.getDate());
});
</script>
Try this:
change label add attribute ClientIDMode="Static"
<asp:Label ClientIDMode="Static" ID="Label1" runat="server" Text="Label"></asp:Label>
$('#Label1').text(date.getDate());
or
$('#<%= Label1.ClientID %>').text(date.getDate());
You can try this.
$(document).ready(function () {
var say = $('#me').text();
var date = new Date();
$('#me').text(date.getDate());
$('#<%= Label1.ClientID %>').text(date.getDate());
// alert(date.getDate());
});
Make sure that your page does not have master page. If it is so then you can use Client ID as below
<script>
$(document).ready(function () {
var say = $('#me').text();
var date = new Date();
$('#me').text(date.getDate());
$('#<%=Label1.ClientID%>').text(date.getDate());
// alert(date.getDate());
});
You can try this,
$('#<%=Label1.ClientID%>').text(date.getDate());
This helps to grab the id generated by ASP.Net for the id parameter..
I am new to asp.net (web forms) and want to get some information from database and then display this information in the form of posts , like heading , paragraph and an image. If there are three rows in database there would be three posts. I have done this using asp.net code in .aspx file
<%
while (reader.Read())
{ %>
<article class="col1">
<h2><%= reader.GetValue(0).ToString()%></h2>
<p class="pad_bot1"><%= reader.GetValue(1).ToString()%></p>
<img src="<%= reader.GetValue(2).ToString() %>" class="img" alt="">
</article>
<% } reader.Close(); %>
But I read somewhere that the code must be separated and it should be written in .aspx.cs file. What's the way to do this ? Assigning id's to the h and p tags and then assigning them values like this :
while (reader.Read())
{
h.InnerText = reader.GetValue(0).ToString();
p.InnerText = reader.GetValue(1).ToString();
}
But this does not solve the problem , because the posts will not be displayed in a loop i.e. 3 times.
I want to know a most suitable solution , thanks in advance
This is my code you can modify as you wish
Code behind
// Here's your object that you'll create a list of
private class Products
{
public string ProductName { get; set; }
public string ProductDescription { get; set; }
public string ProductPrice { get; set; }
}
// Here you pass in the List of Products
private void BindItemsInCart(List<Products> ListOfSelectedProducts)
{
// The the LIST as the DataSource
this.rptItemsInCart.DataSource = ListOfSelectedProducts;
// Then bind the repeater
// The public properties become the columns of your repeater
this.rptItemsInCart.DataBind();
}
Aspx code
<asp:Repeater ID="rptItemsInCart" runat="server">
<HeaderTemplate>
<table>
<thead>
<tr>
<th>Product Name</th>
<th>Product Description</th>
<th>Product Price</th>
</tr>
</thead>
<tbody>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td><%# Eval("ProductName") %></td>
<td><%# Eval("ProductDescription")%></td>
<td><%# Eval("ProductPrice")%></td>
</tr>
</ItemTemplate>
<FooterTemplate>
</tbody>
</table>
</FooterTemplate>
</asp:Repeater>
Using web forms, that would be easiest to solve using a Repeater control.
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.repeater.aspx
<script>
function ShowCommentBox() {
$("#dialog").dialog({ modal: true });
}
function GrabDetails() {
var obj = jQuery.parseJSON('{"name":"John"}');
$("#item").val(obj.name);
}
</script>
<div id="dialog" title="Comments" style="display:none;">
<table class="detailstable FadeOutOnEdit">
<tr>
<th>Item</th>
</tr>
<tr>
<td><asp:Label ID="ItemIdLabel" Text="item" runat="server"/></td>
</tr>
</table>
</div>
<input id="SubmitCommentsToInvoice" type="button" value="Comments" onclick="ShowCommentBox()" />
In my asp.net project when the user clicks the 'Comments' button the div appears containing the label.
I'm trying to use JSON to display the string "John" - stored in them #item object in the 'GrabDetails()'
Then in the label text="" How do I pull across the value stored in the #item object.
Thanks
#item is an ID selector in jQuery, which there is no element here with ID "item". Also, <asp:Label /> renders from the server as html in a different way. However, it appears you are not using this label on the server side at all? if this is the case i would just make an html element like
<td id="WhereNameGoes"></td>
then
function GrabDetails() {
var obj = jQuery.parseJSON('{"name":"John"}');
$("#WhereNameGoes").text(obj.name);
// this still needs to be called somewhere, perhaps in ShowCommentBox()?
}
jQuery $.val() is more for <input /> elements
On button click I want to get the value of a dropdown but for somereason when I go into the code it fails with error 'Exdd' does not exist in the current context. The ExTypeDD is in my listview as a dropdown and I'm just trying to get the value from it. The server side is generating this code and not my browser.
$('.Updt')
.click(function() {
var parent = $(this).parent().prev();
var TypeNode = parent.children("#<%=Exdd.ClientID %>").first();
<asp:ListView runat="server" id="ListView1" >
<LayoutTemplate>
<table id="tablesorter" style="border:solid 1px black;width:55%;">
<thead>
<tr>
<th>
Address
</th>
</tr>
</thead>
<tbody>
<tr id="itemPlaceholder" runat="server" />
</tbody>
<tfoot>
</tfoot>
</table>
</LayoutTemplate>
<ItemTemplate>
<tr>
<td>
<select id="Exdd"
class="Nbr1" style="width: 90px" >
<option value=""><%# Eval("Type")%></option>
<option value="0">Home</option></select>
</td>
<td>
<input type="button" id="btn_update" class="Updt" value="Update" />
</td>
</tr>
</ItemTemplate>
</asp:ListView>
You are in a ListView. You have as ExTypeDD as you have rows.
So you can not reference ExTypeDD successfully outside of the item template (there are more than one)
You may try ( after wrapping that up in a $(document).ready )
$('.Updt')
.click(function() {
var tr = $(this).closest('tr');
var TypeNode = tr.find("select.Nbr1").first();
//...
});
This is because you are trying to access an HTML element on the server side as a server side control. You could make your select element into a server side control by simply adding runat="server" to it like such:
<select id="ExTypeDD" runat="server" class="Nbr1" style="width:90px">
But after that you would still have the issue that this control, although accessible on the server side now, it does not have the id of ExTypeDD because it is dynamically created in the ItemTemplate of your ListView which generates a unique ID value for each newly created control . You can verify this by viewing source on the page rendered in the browser and checking the ID value on the select element.
To do what you're trying to do on the server side you would need to do something along the lines of this:
Add an onItemCreated event handler to your ListView:
<asp:ListView runat="server" id="ListView1" onItemCreated="ListView1ItemCreated">
Replace this:
<input type="button" id="btn_update" class="Updt" value="Update" />
With this:
<asp:button id="btn_update" text="Update" runat="server" />
Add the event handler to the code behind:
protected void ListView1ItemCreated(object sender, ListViewItemEventArgs e){
var myselect = (HtmlSelect)e.Item.FindControl("ExTypeDD");
var mybutton = (Button)e.Item.FindControl("btn_update");
mybutton.OnSubmit += (o,e) => {
// do something with myselect selected value
var selectedvalue = myselect.Value;
};
}
NOTE: the above code is not tested and probably won't compile, you will need to make necessary corrections, but it should give you the idea for a way to solve your problem. Hope it helps.
I've inherited an ASP.NET project that has poorly designed HTML; in one section, the <TR> tags are wrapped with an tag to allow for "click the row to view the information" functionality. The code is:
<asp:LinkButton ID="ViewArticle" runat="server" CommandName="Navigate" CommandArgument='<%# DataBinder.Eval(Container.DataItem, "id") %>' >
<tr runat="server" class="list_item">
<td>some data</td>
<td>some data</td>
<td>some data</td>
</tr>
</asp:LinkButton>
I'd like to do something like:
<tr runat="server" class="list_item" onclick="doRowPostbackFunction('<%# DataBinder.Eval(Container.DataItem, "id") %>');">
<td>some data</td>
<td>some data</td>
<td>some data</td>
</tr>
How do I go about tying a JavaScript onclick event to the codebehind?
Using jQuery you can do the following and trigger a postback:
<table>
<tr id="1">
<td>AAA</td>
<td>BBB</td>
<td>CCC</td>
</tr>
<tr id="2">
<td>AAA</td>
<td>BBB</td>
<td>CCC</td>
</tr>
<tr id="3">
<td>AAA</td>
<td>BBB</td>
<td>CCC</td>
</tr>
</table>
<script type="text/javascript">
$(function () {
$("table tr").click(function (e) {
//alert the row index that was clicked
alert($("table tr").index(this));
//alert the id of the row
alert($(this).attr("id"));
//submit the form...
});
});
</script>
OR use the onclick event of the row...
<tr onclick="doPostBack('<%#DataBinder.Eval(Container.DataItem, "id") %>')">
...and trigger a javascript function.
<script type="text/javascript">
function doPostBack(id)
{
//submit form
}
</script>
You could make your page (or control, if that's inside a control) implement IPostBackEventHandler interface and use ClientScriptManager.GetPostBackEventReference method (using Page.ClientScript propery to get a reference to ClientScriptManager) to get a js-method-call-string which will make a postback to your page. As a control argument to GetPostBackEventReference method use your page.
Then set onclick property of tr to that js-method-call-string.
Example
In aspx:
<table>
<tr onclick="<%= _jsPostBackCall %>;">
<td>a</td>
<td>b</td>
<td>c</td>
</tr>
</table>
<h1>
<asp:Literal ID="litMessage" runat="server" />
</h1>
In codebehind:
public partial class _Default : System.Web.UI.Page, IPostBackEventHandler
{
protected String _jsPostBackCall;
protected void Page_Load(object sender, EventArgs e)
{
// "myTr" will be passed as an argument to RaisePostBackEvent method
_jsPostBackCall = ClientScript.GetPostBackEventReference(this, "myTr");
}
public void RaisePostBackEvent(string eventArgument)
{
switch (eventArgument)
{
case "myTr":
HandleTrClick();
break;
// you can add other controls that need postback processing here
default:
throw new ArgumentException();
}
}
private void HandleTrClick()
{
litMessage.Text = "Tr clicked.";
}
}
You can simply use
__doPostBack(eventName, eventArgs);
Here eventName is code-behind event handler