Building Hyperlink Dynamically but need to perform another action also - c#

I am populating a repeater on my asp.net web page (pop up) from a stored procedure call. I have a column where I need to build the hyperlink based on the values dynamically that calls an inquiry back to my original web form. However, the trouble I am having is that when I click on the link, I need to also retrieve some other data and insert this data into the viewstate.
The "GetListOfValues" function that I am calling obviously doesn't work the way I have this coded. Is there a way to accomplish this, and maybe even do it better than I am attempting to do it?
Here's my example:
Default.aspx
<asp:Repeater ID="rptReport" runat="server">
<HeaderTemplate>
<table>
<tr>
<td>Name</td>
<td>ID</td>
<td>Value1</td>
<td>Value2</td>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td><%# DataBinder.Eval(Container.DataItem,"Name") %></td>
<td><%# DataBinder.Eval(Container.DataItem,"ID") %></td>
<td><%# DataBinder.Eval(Container.DataItem,"Value1") %></td>
<td><%# GetValueTwoLink(Eval("Name"),Eval("Value2") %></td>
</tr>
</ItemTemplate>
<FooterTemplate>
<tr>
<%--This area holds totals for columns--%>
</tr>
</table>
</FooterTemplate>
</asp:Repeater>
Default.aspx.cs
protected string GetValueTwoLink(object name, object value2)
{
ViewState["ListOfValues"] = datalayer.GetListOfValues(name, value2);
return string.Format(
"{1}",
name,
Convert.ToInt32(value2).ToString("d"));
}
Any help would be appreciated!

Not exactly sure what you are trying to do with that onclick... but you can do it on the code-front:
<ItemTemplate>
<tr>
<td><%# Eval("Name") %></td>
<td><%# Eval("ID") %></td>
<td><%# Eval("Value1") %></td>
<td>
<a href="#"
onclick="<%# String.Format("window.opener.ViewValues('{0}')", DataBinder.Eval(Container.DataItem,"Name")) %>">
<%# Convert.ToInt32(DataBinder.Eval(Container.DataItem, "Value2")).ToString("d")%>
</a>
</td>
</tr>
</ItemTemplate>
Also, as you see, you can just use
<%# Eval("Name") %>
instead of
<%# DataBinder.Eval(Container.DataItem,"Name") %>
to just show data. You use the DataBinder.Eval when you need to do some manipulation, like in the 4th <td>.

Related

How can I create a table with dynamic columns using asp.net controls?

Background: My data model is a List<PersonDetail>, where PersonDetail contains properties like FirstName, LastName, etc, and the List can be any size.
I want to create a table that can compare these PersonDetails side-by-side. This means for each column I need to take the data in a single PersonDetail and spread it across my 8 rows. Then do the same for the next PersonDetail.
I have a constraint where I can only use an Asp.Net control (ListView, GridView, Repeater, etc.) along with control.DataSource and control.DataBind() to create my table.
The template I want to do is something like:
<LayoutTemplate>
<table>
<tr>
<asp:PlaceHolder runat="server" ID="itemPlaceHolder1"></asp:PlaceHolder>
</tr>
<tr>
<asp:PlaceHolder runat="server" ID="itemPlaceHolder2"></asp:PlaceHolder>
</tr>
<!-- and so on -->
</table>
</LayoutTemplate>
<ItemTemplate1>
<td>
<asp:Label ID="LabelFirstName" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "FirstName")%>'></asp:Label>
</td>
</ItemTemplate1>
<ItemTemplate2>
<td>
<asp:Label ID="LabelLastName" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "LastName")%>'></asp:Label>
</td>
</ItemTemplate2>
<!-- and so on -->
Where the above ItemTemplates would be the only parts that repeat in the table.
I've tried all three control options and it doesn't seem to be supported without complicating the code-behind.
Is there a better data model I could use that can interface with another control? I've seen solutions like DataTable, but not sure if that will solve my problem..
Any ideas?
<table border="1">
<asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate>
<tr>
<td><%# DataBinder.Eval(Container.DataItem, "FirstName") %></td>
<td><%# DataBinder.Eval(Container.DataItem, "LastName") %></td>
</tr>
<tr>
<td><%# DataBinder.Eval(Container.DataItem, "Street") %></td>
<td><%# DataBinder.Eval(Container.DataItem, "City") %></td>
</tr>
</ItemTemplate>
</asp:Repeater>
</table>

Custom user control with repeating data

Below is code that I'm aiming for. I need a custom user control that can be data bound to, but also contain other content (so just a raw repeater wont suffice). The end goal is something along the lines of:
<MyControls:Control1 runat="server" id="Control1">
<headertemplate>
<tr>
<td>ID</td>
<td>Username</td>
</tr>
</headertemplate>
<itemtemplate>
<tr>
<td><%#((User)Container.DataItem).ID %></td>
<td><%#((User)Container.DataItem).Username %></td>
</tr>
</itemtemplate>
</MyControls>
And:
var users = GetUsersList();
Control1.DataSource = users;
Control1.DataBind();
And looks like this:
<%# Control Language="C#" AutoEventWireup="true" CodeBehind="MyControl1.ascx.cs" Inherits="Controls.MyControl1" %>
<asp:PlaceHolder runat="server" ID="Wrapper">
<h2>Results</h2>
<table>
<%=HeaderTemplate%>
<%
if(ItemTemplate.AnyItems()){
foreach(var item in ItemTemplate){
}
}
else
{
%>Nothing here<%
}
%>
</table>
<MyControls:AnotherControl runat="server" />
</asp:PlaceHolder>
I've found a page on ScottGu's Blog that appears to show what I want:
https://weblogs.asp.net/scottgu/Supporting-Templates-with-ASP.NET-User-Controls
But the linked to tutorial 404's now! All other examples I've found don't seem to be well written and very hard to pick apart.
Any help on how to achieve the above would be much appreciated.
It looks like you are getting 2 different Controls mixed up. UserControl and Repeater (I think).
To bind data in to a Control inside a UserControl, you need to make that Control accessible from the parent. You can do this by creating a public property inside the UserControl.
public Repeater myRepeater
{
get
{
return Repeater1;
}
set
{
Repeater1 = value;
}
}
UserControl ascx with the Repeater Control
<table border="1">
<asp:Repeater ID="Repeater1" runat="server">
<HeaderTemplate>
<tr>
<td>ID</td>
<td>Username</td>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td><%# Eval("ID") %></td>
<td><%# Eval("Username") %></td>
</tr>
</ItemTemplate>
</asp:Repeater>
</table>
<br />
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<br />
<asp:Button ID="Button1" runat="server" Text="Button" />
<br />
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
Now you have access to Repeater1 in the parent due to the public property.
Control1.myRepeater.DataSource = users;
Control1.myRepeater.DataBind();
And leave the control on the aspx parent empty.
<MyControls:Control1 runat="server" id="Control1"></MyControls>

How can i access data from parent repeater in nested repeater?

I Use modelbinding to pass a list of Post Objects containing post id,Title.. to my page.aspx
Each Post Objects also contains a list of Comment Objects and these are
the ones i want to access in the inner repeater.
Here the code from my page.aspx
<asp:Repeater runat="server" ID="repeater1" ItemType="OOPBLOG.Model.Classes.Post"
SelectMethod="LoadPosts"
OnCallingDataMethods="postView">
<ItemTemplate >
<table>
<tr>
<td><%#Item.Title %></td>
</tr>
<tr>
<td><%#Item.Body %></td>
</tr>
<tr>
<td><%#Item.Date %></td>
</tr>
<asp:Repeater runat="server" ID="repeater2">
<ItemTemplate>
<table>
<tr>
<td>
<%# ((RepeaterItem)Container.Parent.Parent).ItemType %>
</td>
</tr>
</table>
</ItemTemplate>
</asp:Repeater>
</table>
</ItemTemplate>
</asp:Repeater>
To conclude, what i want, is to access the Item.Comments List in the nested repeater. How can i do this?

How to open a file by clicking on HyperLink

I have this table
I want to click on the link and the file (whatever file) will be opened in a new pop-up window.
Here is my code:
<asp:Repeater ID="dokumente" runat="server">
<ItemTemplate>
<tr>
<td><asp:HyperLink ID="HyperLink4" runat="server" Text='<%# Eval("DokuTyp") %>' NavigateUrl='file://<%# Eval("File") %>'></asp:HyperLink></td>
<td><%# Eval("Description")%></td>
<td><%# Eval("Date") %></td>
<td><%# Eval("File") %></td>
</tr>
</ItemTemplate>
</asp:Repeater>
But it doesn't work with NavigateUrl. Can anyone help me on this or any idea how to do this. Thanks
The file:/// is for resources on your own machine.
To open files on a server, you will have to link to urls on the server.
Use:
HttpContext.Current.Request.ResolveUrl(pathOnServer);
Change your code like this:
<asp:Repeater ID="dokumente" runat="server">
<ItemTemplate>
<tr>
<td><asp:HyperLink ID="HyperLink4" runat="server" Text='<%# Eval("DokuTyp") %>' NavigateUrl='<%# HttpContext.Current.Request.ResolveUrl(Eval("File")) %>'></asp:HyperLink></td>
<td><%# Eval("Description")%></td>
<td><%# Eval("Date") %></td>
<td><%# Eval("File") %></td>
</tr>
</ItemTemplate>
</asp:Repeater>
Where Server
The "file" protocol opens a file in the user computer.
I guess you have to read the file on the server-side and call a Resposne.Write.
If you want find file on server you can use Server.MapPath method; "file://" is not correct url if you want find file on server
NavigateUrl=<%#Server.MapPath(DataBinder.Eval("File"))%>

What is the easiest way to put an index to a repeater control in .NET?

I want an ASP:NET WebForms Repeater control to put an index next to each of its output rows automatically. How can I do that?
Example:
Name
1 John
2 Jack
3 Joe
Try the following:
<asp:Repeater ID="myRepeater" runat="server">
<HeaderTemplate>
<table>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td><%# Container.ItemIndex %></td>
<!-- or maybe -->
<td><%# Container.ItemIndex + 1 %></td>
<td><%# DataBinder.Eval(Container, "DataItem.Name") %></td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>

Categories