I have the following situation:
<asp:Repeater ID="myRepeater" runat="server">
<ItemTemplate>
<asp:ImageButton OnClick="imgSearchResult_Click" BackColor="#333333" ID="imgSearchResult" height="32" width="32" runat="server" ToolTip='<%# Eval("ToolTip") %>' ImageUrl='<%# Eval("ImageUrl") %>'/>
<asp:Label ID="labValue" Text='<%# Eval("Text") %>' runat="server" Width="32" />
</ItemTemplate>
</asp:Repeater>
The Repeater contains ImageButton and Label objects. If I only use one object, I can add them from codebehind through the binding:
Repeater.DataSource = imageList;
Repeater.DataBind();
OR
Repeater.DataSource = labelList;
Repeater.DataBind();
How can I add both to one Repeater?
If you need them in one Repeater, they -are- related in that "view" (loosely used term) of the data. So why not make that view into an explicit object that contains both the image and the label? Either named, or anonymous:
return Enumerable.Zip(imageList, labellist, (image, label) => new {image, label})
Alternatively, you can just pass the images and query for the labels by index:
<asp:Repeater ID="myRepeater" runat="server">
<ItemTemplate>
<asp:Label ID="labValue" Text='<%# GetLabelText(Container.ItemIndex) %>' runat="server" Width="32" />
</ItemTemplate>
</asp:Repeater>
This requires a public GetLabelText(index) method and probably extra state in your code behind to make the ImageList available to that method. Not very nice, but it gets the job done.
Related
I have the following List:
private List<System.Web.UI.WebControls.Image> _searchResultList = new List<System.Web.UI.WebControls.Image>();
This List may contain several Images with different URLs.
I have the following Repeater:
<asp:Panel ID="SearchPanel" runat="server" ScrollBars="Vertical">
<asp:Repeater ID="Repeater" runat="server">
<ItemTemplate>
<asp:Image height="32" width="32" runat="server"/>
</ItemTemplate>
</asp:Repeater>
</asp:Panel>
Using DataSource to display the images doesn't seem to work.
Repeater.DataSource = _searchResultList;
Repeater.DataBind();
What am I doing wrong?
The _searchResultList is not a list of strings so you can't use ImageURL='<%Container.DataItem.ToString()%>'. Because _searchResultList is a list of images you should bind the ImageUrl property. This should works fine for you:
<asp:Repeater ID="Repeater" runat="server">
<ItemTemplate>
<asp:Image ID="Image1" height="32" width="32" runat="server" ImageUrl='<%# Eval("ImageUrl") %>' />
</ItemTemplate>
</asp:Repeater>
In this example Container.DataItem refers to an Image control. This is why we used Eval("ImageUrl") to get the ImageUrl property of each Image control.
<asp:Panel ID="SearchPanel" runat="server" crollBars="Vertical">
<asp:Repeater ID="Repeater" runat="server">
<ItemTemplate>
<asp:Image height="32" width="32" runat="server" ImageURL='<%Container.DataItem.ToString()%>'/>// changes here
</ ItemTemplate>
</asp:Repeater>
</asp:Panel>
I need to use DataList inside another DataList. It works fine for me, but when I'm trying to do something in code behind with this inside one it just doesn't exist for C#. Here is code:
...
<asp:DataList ID="DataListDziennik" runat="server"
DataSourceID="SqlDataSourcePrzedmioty">
<ItemTemplate>
<asp:Label ID="LabelPrzedmiot" runat="server" Text='<%# Eval("przedmiot") %>' />
...
<asp:DataList ID="DataListOceny" runat="server"
DataSourceID="SqlDataSourceOceny"
RepeatDirection="Horizontal"
OnItemCommand="DataListOceny_ItemCommandOceny"
OnEditCommand="DataListOceny_EditCommandOceny">
<EditItemTemplate>
<asp:TextBox ID="TextBoxOcena" runat="server" Text='<%# Bind("lista") %>' />
<td><asp:Button ID="ButtonZapisz" CommandName="Update" runat="server" Text="Zapisz" /></td>
</EditItemTemplate>
<ItemTemplate>
<asp:TextBox Width="20" ID="TextBoxOcena" ReadOnly="true" Text='<%# Eval("lista") %>' runat="server"></asp:TextBox>
<td><asp:Button ID="ButtonEdytuj" CommandName="Edit" runat="server" Text="Edytuj" /></td>
</ItemTemplate>
</asp:DataList>
</td>
</ItemTemplate>
</asp:DataList>
When I write this in code behind:
protected void DataListOceny_EditCommand(object source, DataListCommandEventArgs e)
{
DataListOceny.EditItemIndex = e.Item.ItemIndex;
DataListOceny.DataBind();
}
...Visual Studio tells me that DataListOceny does not exist in current content. I just want to be able edit items on DataListOceny after clicking the "edit" button, it can be placed anywhere on website. Do you know any solution for this problem?
Because DataListOceny is a control inside of another control, you have to make a reference to it by doing something like:
DataList DataListOceny = (DataList)e.Item.FindControl("DataListOceny");
Once you do that, you can use the DataListOceny variable. Hope this helps.
I've got something like that
<asp:ListView ID="lv" runat="server">
<LayoutTemplate>
<asp:Literal ID="litControlTitle" runat="server" />
<label id="test" runat="server">dw</label>
<asp:PlaceHolder ID="itemPlaceHolder" runat="server" />
</LayoutTemplate>
<ItemTemplate>
</ItemTemplate>
</asp:ListView>
Can someone tell me, how should I change label text, using C# code?
Main problem for me is- how to get access to the nested control (label, literal) from c# code?
EDITED:
<SelectedItemTemplate>
<asp:HiddenField ID="NumberEdit" runat="server"
Value='<%# Bind("numbers") %>' />
<label for="NameEdit">Name:</label>
<asp:TextBox ID="NameEdit" Width="160px" runat="server" AutoPostBack="true" OnTextChanged="NameEdit_TextChanged"
Text='<%# Bind("Name") %>' />
<br />
<label for="ShortcutEdit">Shortcut:</label>
<asp:TextBox ID="ShortcutEdit" Width="80px" runat="server"
Text='<%# Bind("Shortcut") %>' />
<br />
and I would like to generate automatically Shortcut text when user will change Name (Shortcut = 2 first letters from NameEdit)? Can you explain me, how should I do it? –
You would want to have an ItemDataBound event handler to get access to the controls for that particular item in your listview. The example on the page I linked should help you out.
First thing is that you need a data source binded with this ListView control, for example SqlDataSource, or any other allowed type you need:
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:YourConnectionString %>"
SelectCommand="SELECT [Title], [id] FROM [Articles]"></asp:SqlDataSource>
<asp:ListView ID="lv" runat="server" DataSourceID="SqlDataSource1" >
// rest of the code
</asp:ListView>
Second thing is that controls from LayoutTemplate template will be rendered only if there is any data to show. So if you have datasource, but it is empty, this tamplate will not be applied. But you can use EmptyDataTemplate to display info when there is nothing from the datasource to display.
And then, when you already have datasource defined and binded to your ListView and there is data that will be displayed, the LayoutTemplate will be rendered. And then you can use FindControl method of the ListView. As for example of getting this literal:
Literal l = (Literal)lv.FindControl("litControlTitle");
It's returning null for you because you have no data to display, so controls are not rendered at all.
((Label)ListView1.FindControl("test")).Text = "Hello!";
I have a gridview which has a few rows (each with a unique rowId), and each line has a FileUpload control, now everything works okay with FileUpload.
(my uploaded file database image can be seen below)
I have the download button, which also works okay, however I want to make this button invisible if no file exists for the corresponding row.
Nothing proper comes to my mind.
My button and FileUpload control:
<asp:TemplateField HeaderText="BatchList">
<EditItemTemplate>
<asp:ImageButton ID="ibt_Download" runat="server" src="Images/Download.png" CommandName="Download" CommandArgument='<%# Container.DataItemIndex %>' ></asp:ImageButton>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="UploadBatchList">
<HeaderTemplate>
<asp:Label ID="lbl_Header" ToolTip="Upload Batch List" runat="server" Text="UBL"></asp:Label>
</HeaderTemplate>
<EditItemTemplate>
<asp:FileUpload ID="fu_UploadBatchList" runat="server" />
<asp:Button ID="btn_Upload" runat="server" Text="Upload" OnClick="btn_Upload_Click" />
</EditItemTemplate>
</asp:TemplateField>
This is how it looks on my gridView
When gridview is first created the green dots must not be visible if a file has been uploaded before.
My file database:
You can check some property of the data item (DocName in your case) if it contains a value (it might not work when copy-pasted, I'm a little bit improvising):
<asp:Button ID="btn_Upload" runat="server"
Text="Upload"
Visible='<% DataBinder.Eval(Container.DataItem, "DocName") == null %>
OnClick="btn_Upload_Click" />
Or you can create a function that will evaluate the visibility. See Mastering ASP.NET DataBinding for more.
I want to show a thumbnail image inside a gridview instead of the text. This is what I am trying:
<asp:TemplateField HeaderText="Image" SortExpression="Image">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Image") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Image ID="thumbnail" runat="server" ImageUrl="<%# Bind("Image") %>" />
</ItemTemplate>
</asp:TemplateField>
What is the syntax i should be using?
Try using Eval instead of Bind for the ImageUrl - this is one way binding.
If you are still having problems, using single quotes instead of double quotes around the property might help: <asp:Image ID="thumbnail" runat="server" ImageUrl='<%# Eval("Image") %>' />