I'm just learning ASP.Net, so I hope that you bear with me and my questions. In my program, I have a dataset that contains Url strings that point to various images. My question is, can I use that dataset as a datasource for a Repeater control so that the Repeater uses those Urls to display the images?
Thanks so much for any help and advice.
You most certainly can. You will want to do the binding in the code-behind, probably in the Load method like this:
repeaterControl.DataSource = yourDataSet.Tables[0];
repeaterControl.DataBind();
where 0 is the index of the DataTable you're trying to get to.
Then you'll want to build the markup something like this:
<asp:Repeater ID="repeaterControl" runat="server">
<ItemTemplate>
<asp:Image runat="server"
ImageUrl="<%# DataBinder.Eval(Container.DataItem, "TheFieldName") %>" />
</ItemTemplate>
</asp:Repeater>
where TheFieldName is the name of the field/column in the DataTable that contains the URL. Now, this code may need to be debugged a little bit because I didn't build an entire project around this, but this will get you 99% of the way there, if not all of the way.
Related
Output
Code
<asp:Repeater ID="rptPBM" runat="server">
<ItemTemplate>
<asp:Label runat="server" ID="lblPBM" EnableViewState="false" Text='<%# Container.DataItem.ToString() %>'/>
<br />
</ItemTemplate>
</asp:Repeater>
I'm trying to add labels to a page dynamically using the above repeater, but when they are added it stacks vertically. Anyone ever encounter this before?
You are adding a line break after each label (br tag). Was that intended?
Remember that for each item in your data set it will write out that whole statement between the tags.
Maybe try writing out the html manually until you get it how you like and then put it into the item template.
Found the problem, the DataSource and DataBind() were in a loop so it was adding one letter at a time. Once I moved them out, it works fine.
I would like to insert a string (key) into a '<%$ Resources:resFile, someKey %>' statement dynamically at runtime.
Right now I have this:
<asp:Label runat="server" id="lbl" Text='<%$ Resources:resFile, someKey %>'/>
which is useless to me, because the key "someKey" is static and will always converge to the same x translations.
I've tried doing things like this:
<asp:Label runat="server" id="lbl" Text='<%$ Resources:resFile, '<%#Eval("someProperty")%>' %>'/>
But this doesn't quite compile. I'm not sure if you can have nested <%%> statements, so thats why i'm asking here.
I've also tried a variety of things with GetGlobalResourceObject() but that doesn't do what i want either.
The thing is that i am binding a list to a repeater, and one of the items in the datasource will contain the key that will match a key in the resource files.
If anyone knows a way how to achieve this, be it with nested <%%> statements or be it any other way, i'd be happy to try it out.
You could do this in Code-Behind on the ItemDataBound Event.
Label lv = (Label)e.Item.FindControl("lbl");
ResourceManager resMngr = new ResourceManager(typeof(SupertextCommon.Default));
lv.Text = resMngr.GetObject(someProperty, culture);
This has worked for me in a GridView using a property of its datasource:
<%# GetGlobalResourceObject("Resource", Eval("SomeProperty").ToString()) %>
You can also use concatenation to build the key, e.g.:
<%# GetGlobalResourceObject("Resource", "SomePrefix" & Eval("SomeProperty").ToString()) %>
I have a asp.net application.The requirement is to provide a live search functionality to search a particular list of products.
I searched various live search jquery plugins and used this
http://nakajima.github.com/jquery-livesearch/
In the above mentioned link , the data is in ul li format and then the live search is implemented on that list, so what I did is in the backend, from server side, used a repeater control, like below
<ul id="names">
<asp:Repeater ID="SomeRepeater" runat="server" OnItemCommand="SomeRepeater_ItemCommand" Visible="false">
<ItemTemplate>
<li>
<asp:LinkButton ID="SomeLink" runat="server" CommandName="Load" CommandArgument='<%# Eval("SomeId")%>' Text='<%# Eval("SomeNameName") + " " + "("+ Eval("SomeCount") + ")" %>'></asp:LinkButton>
</li>
</ItemTemplate>
</asp:Repeater>
After this in the code behind, binded the repeater control with a List object for the data.
After all this the live search functionality was completely working fine with the values in repeater control as ul li.
now , the requirement is changed and I need to hide the ul li values which I am binding to repeater control, but want the live search for the same listobject.
For this I searched over the internet and got an idea, instead of using the repeater control, let me use a JSon , I am successful in getting the values as a string from the below code
JavaScriptSerializer jss = new JavaScriptSerializer();
string output = jss.Serialize(newListObject);
but the problem is , I cannot use the jquery plugin which I have mentioned above and I am not sure how to do the same search functionality using above json string.
Please let me know if my approach is correct also, how to go about achieving the same.
It sounds like jQuery Autocomplete will do exactly what you need. It's also fairly easy to integrate with .NET, you just return a list of strings.
Alternatively, you can declare a static list of autocompleteable items. You can also trigger changes/redirect when an item is clicked.
I am working on an asp.net web application where I have predefined panel in my project with
CSS. Now i want to create another panel with same design and CSS at run time at multiple times. I have a button control when i will click that button it will add another panel.
Please help me to add another panel with same criteria.
If it is something that you plan on reusing, I'd suggest you utilize a user control for this. You can them simply add a new instance of the control on your page.
A few things worth looking into:
https://web.archive.org/web/20210707024005/http://aspnet.4guysfromrolla.com/articles/081402-1.aspx
http://aspalliance.com/565
http://msdn.microsoft.com/en-us/library/c0az2h86.aspx
If you wanted to accomplish this with a postback to the page, add this to your event...
//MyControl = Custom User Control
var myControl = (MyControl) Page.LoadControl("MyControl.ascx");
this.ControlContainer.Controls.Add(myControl);
Like RSolberg said, you could write a User Control and add it multiple times:
<my:UserControl id="MyControl1" runat="server" />
<my:UserControl id="MyControl2" runat="server" />
<my:UserControl id="MyControl3" runat="server" />
Of course, your User Control can be as simple or as complex as you like, thus having repeated functionality on your page.
However, depending on your exact needs you might want to consider something like an ASP.NET Repeater, or ListView, or DataGrid control. With something like a Repeater, you can bind data to it, and have that data be displayed in a list/grid, that has a common look and feel. You can give your Repeater a HTML/CSS template for the header, items, and footer sections too to make it look consistent and professional.
<asp:Repeater id="MyRepeater" runat="server">
<HeaderTemplate>
<h1>Products</h1>
</HeaderTemplate>
<ItemTemplate>
<p>
Product name: <%# Eval("ProductName") %>
</p>
</ItemTemplate>
</asp:Repeater>
and in your code just do this:
MyRepeater.DataSource = products;
MyRepeater.DataBind();
There are many ways of doing what you're asking in ASP.NET - be a bit more specific and we'll be able to give you more specifc help.
Couldn't you just create a new panel in the code behind on the button's "On_Click" event? That would be my suggestion. You may need to have a placeholder to add the panel into something so it appears on the page.
I'm trying to list vacancies in a asp:GridView using an XML_feed through an asp:XmlDataSource. This should be kinda simple, but I'm missing something with my XPath-expressions..
This is a short example of what I'm trying to achieve (list title of vacancies):
<asp:XmlDataSource ID="XMLsource" DataFile="http://demo.easycruit.com/export/xml/vacancy/list.xml" XPath="VacancyList/Vacancy" runat="server"/>
<asp:GridView DataSourceID="XMLsource" AutoGenerateColumns="False" runat="server">
<Columns>
<asp:TemplateField>
<HeaderTemplate>Title</HeaderTemplate>
<ItemTemplate><%# XPath( "Version/Title" ) %></ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Both controllers have DataBind() in codebehind. So, if someone knows why this doen't work... :)
If you have a look at your XML (just the first few lines):
<VacancyList generated="2009-08-04T18:43:17"
xmlns="urn:EasyCruit"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.easycruit.com/dtd/vacancy-list.xsd">
<Vacancy id="82034" date_start="2007-04-17" date_end="2009-12-22"
reference_number="CDP-GR3">
<Versions>
<Version language="fr">
<Title>Chef de produit (H/F)</Title>
<TitleHeading/>
You bind your datagrid to the list of <Vacancy> elements - fine so far.
But then in your grid, you reference : <%# XPath( "Version/Title" ) %>
This won't work, since the <Vacancy> does not have a '/inside it - these elements are within a` collection.....
So what you need to reference in your ItemTemplate would be:
<%# XPath( "Versions/Version[#language='fr']/Title" ) %>
That should work.
UPDATE:
there appears to be an additional problem with the ASP.NET 2.0 XmlDataSource not being able to handle default XML namespaces :-(
That's this line here in your XML:
<VacancyList ........
xmlns="urn:EasyCruit"
See this blog post here on the topic: http://jasonf-blog.blogspot.com/2006/08/xmldatasource-xpath-workaround-for.html
There's really two things you could do to fix this:
use an XSLT transformation to strip out the default namespace so that the XmlDataSource can handle the data
load the data from the URL in code, and bind it to the GridView in your code-behind
UPDATE 2:
The method of stripping out the XML namespaces seems to work quite nicely - Bill Evjen proposes this method here.
If you save his XSLT file in the post to a file called "StripNamespaces.xslt" in the web site project, you should get your data if you change the asp:XmlDataSource to be:
<asp:XmlDataSource ID="XMLsource" runat="server"
DataFile="http://demo.easycruit.com/export/xml/vacancy/list.xml"
TransformFile="~/StripNamespaces.xslt"
XPath="VacancyList/Vacancy" />
Note the new "TransformFile" setting - this must reference that XSLT file. With this in place, I am now getting data displayed in the GridView.
Marc