I am trying to modernize this site. I have a report table being generated in the old style like:
Dim sTable = "<table class=""stat"">"
sTable = sTable & "<thead><tr><th colspan=20><Status Report: " & Session("ProcessYear") & "</th></tr>"
I am converting this into a Repeater, but I am at a loss as to how to include session data in the <HeaderTemplate>. I have:
<asp:Repeater id="Dashboard" runat="server">
<HeaderTemplate>
<table class="stat">
<thead>
<tr><th colspan="20">Appeal Status Report: ???? </th></tr>
...
Some candidates are asp:PlaceHolder, and something like <%# ((RepeaterItem)Container.Parent.Parent).DataItem %> (see Accessing parent data in nested repeater, in the HeaderTemplate), but I'm at a loss as to what that is referencing.
Not in a position to refactor out the Session dependencies, unfortunately. Any thoughts? I'm fairly new to ASP.NET. My CodeFile for this page is in VB.NET.
You need to look into On Item Data Bound event, so you can do some code-behind work during binding.
You'll want to check the item object, determine if its a header row, then access your session object.
Rough adaptation might look like:
ASPX
<asp:Repeater id="Dashboard" runat="server" OnItemDataBound="ItemDataBound">
<HeaderTemplate>
<table class="stat">
<thead>
<tr>
<th colspan="20">
Appeal Status Report: <asp:Label ID="YourLabel"/>
</th>
</tr>
...
...
...
Code Behind
void ItemDataBound(Object Sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Header)
{
((Label)e.Item.FindControl("YourLabel")).Text = Session["YourSession"];
}
}
You can also do things with the footer, item and alternating items here as well.
Related
I have programmed a simple blog page - http://www.3don.net.br/Blog.aspx (another language, here only to show the structure). I want to use hashtags for pointing to the topics. For example, http://www.3don.net.br/Blog.aspx#19/04/16 should scroll the page to the topic created at 19/04/16.
However, I cannot get it!
The topics of the blog are ItemTemplates of a ListView control. When I define an ID="lblDatum" for the label control of the data of each topic (which is the first control of each topic), then this ID is modified by the NET machine to
id="ctl00_ContentPlaceHolder_lstBlog_ctrl0_ctl01_lblDatum" (you can see it in the source code of the page for the second topic, for example).
So, if I access in the browser www.3don.net.br/Blog.aspx#ctl00_ContentPlaceHolder_lstBlog_ctrl0_ctl01_lblDatum
the page indeed will scroll correctly. I can also programmatically change the ID for each topic differently and it still works for each topic.
However, the hashtag-name "ctl00_ContentPlaceHolder_lstBlog_ctrl0_ctl01_lblDatum" is not nice! Is there a possibility to suppress the ctl00_ContentPlaceHolder_lstBlog_ctrl0_ctl01_-part?
Or another idea for getting it?
Use ClientIdMode="Static" in your ListItem, which removes the autoGenerate ID, you got ID="lblDatum" on client.
ok, I added the clientIDMode="static" property to the Label control of the data:
<table id="table_intern" runat="server" >
<tr id="tr_intern" runat="server">
<td id="td_intern" runat="server">
<asp:Label ID="lblDatum" runat="server" Text='<%# Eval("data") %>' CssClass="rotfettschrift" clientidmode="static"/>
</td>
</tr>
<tr> ... </tr>
<tr> ... </tr>
</table>
... and, in code-behind, set the ID of this control of each topic igual of the text property of this control (the data):
lbl = CType(e.Item.FindControl("lblDatum"), Label)
lbl.ID = lbl.Text
However, the HTML-output is:
<tbody>
<tr id="ctl00_ContentPlaceHolder_lstBlog_ctrl0_ctl00_tr_intern">
<td id="ctl00_ContentPlaceHolder_lstBlog_ctrl0_ctl00_td_intern">
<span id="ctl00_ContentPlaceHolder_lstBlog_ctrl0_ctl00_19/05/16" class="rotfettschrift" clientidmode="static">19/05/16</span>
</td>
</tr>
<tr> ... </tr>
<tr> ... </tr>
</tbody>
Why is the ID of the span element ctl00_ContentPlaceHolder_lstBlog_ctrl0_ctl00_19/05/16 and not just 19/05/16 ???
Ok, i tested here, using reapeater, but with any other control works. You must set the id individually to each control, to make unique.
*I redid, using date.
MARKUP:
<table id="table_intern">
<asp:Repeater runat="server" ID="repeater" OnItemDataBound="repeater_ItemDataBound">
<ItemTemplate>
<tr>
<td>
<asp:Label Text="" runat="server" ID="label" />
</td>
</tr>
</ItemTemplate>
</asp:Repeater>
</table>
BACKEND:
protected void Page_Load(object sender, EventArgs e)
{
var list = new List<string>();
for (int i = 0; i < 10; i++)
{
list.Add(string.Format("Item_{0}", i.ToString().PadLeft(2, '0')));
}
repeater.DataSource = list;
repeater.DataBind();
}
protected void repeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
var label = (Label)e.Item.FindControl("label");
var item = (DateTime)e.Item.DataItem;
label.ID = item.ToShortDateString();
label.Text = item.ToShortDateString();
label.ClientIDMode = ClientIDMode.Static;
}
}
RESULT:
<table id="table_intern">
<tbody><tr>
<td>
<span id="23/05/2016">23/05/2016</span>
</td>
</tr>
<tr>
<td>
<span id="24/05/2016">24/05/2016</span>
</td>
</tr>...
Sorry, for bad formatting, i will improve my answers if necessary.
Daniel,
thank you for dedicating your time.
You are defining the ClientIDMode property of the Label control in the code behind, ok. I do so, but I see that it does not work here:
screenshot
When I go with the mouse pointer over the ClientIDMode at the left it advices: "'ClientIDMode' is not a member of 'Label'"
and over the ClientIDMode at the right, it pops "'ClientIDMode' is not declared. It may be inaccessible due to its protection level."
Is there something undefined in my system?
Ok, i understand you using Framework2.0 in your project. Can you change Framework to 4.0 or upper? Otherwise you need use a .js approach, to scroll like wish.
ClientIdMode is from .net 4.0, how you have said .net4.6 installed, I assumed that the project was also 4.6.
IF, you can't change the version, you need use something like jquery to scroll. OR maybe a more hard solution creating a item in your itemTemplate like:
<span id='<%# Eval("data") %>'></span>
I don't have tested this solution, later I'll be testing;
Daniel,
I always suspected that, but the
"About Visual Studio" window tells another story.
Adicionally, in the registry I can find the v4/Full key and the Version information (at the right side) confirms the (installed and activated?) version 4.6.01055.
???
Or is framework v4 installed but not "activated"? Does this exist? Where can I see this?
So I had have a table populating with data but I was wondering how I could pass two bits of data from a row depending on which link at the end of the row is clicked.
<%WebReceiptSummary[] receipts = GetReceipts();
if (receipts != null)
{
for (int i = 0; i < receipts.Length; i++)
{%>
<tr>
<td><%= receipts[i].Type%></td>
<td><%= receipts[i].PolicyNo%></td>
<td><%= receipts[i].Date%></td>
<td class="c"><%= receipts[i].Amount%></td>
<td class="r"><asp:LinkButton OnCommand="PDFLinkClick"
CommandArgument="<%= receipts[i].PDF %>&<% receipts[i].PolicyNo %>" runat="server">View PDF</asp:LinkButton></td>
</tr>
<% }
}
%>
Obviously my CommandArgument just passes back the string <%= receipts[i].PDF %>&<% receipts[i].PolicyNo %> not the values. What would be the best way of doing this? I was also thinking of using;
<asp:HiddenField ID="hiddenIsCaptchaReadyValidate" runat="server" Value=false/>
But I have the same problem here where the value is placed within quotes and also it means I need to create two hiddenfields for ever row which isn't the most efficient way of doing this. Thoughts?
It is not possible to have <%= %> commands as part of an attribute when added via the mark-up.
Can I recommend that instead of using the for loop in the ASPX, you instead use the <asp:Repeater> control? This will also allow you to set the CommandAttribute value from the code-behind.
An example...
<asp:Repeater runat="server" id="receipts" OnItemDataBound="receipts_ItemDataBound">
<ItemTemplate>
<tr>
<td><%#((WebReceiptSummary)Container.DataItem).Type%></td>
<td><%#((WebReceiptSummary)Container.DataItem).PolicyNo%></td>
<td><%#((WebReceiptSummary)Container.DataItem).Date%></td>
<td class="c"><%#((WebReceiptSummary)Container.DataItem).Amount%></td>
<td class="r"><asp:LinkButton ID="pdfLink" OnCommand="PDFLinkClick" runat="server">View PDF</asp:LinkButton></td>
</tr>
</ItemTemplate>
</asp:Repeater>
In your Init or Load in code behind...
receipts.DataSource = GetReceipts();
receipts.DataBind();
Then...
protected void receipts_ItemDataBound(Object sender, RepeaterItemEventArgs e)
{
((LinkButton)e.Item.FindControl("pdfLink")).CommandArgument =
((WebReceiptSummary)e.DataItem).PDF + ((WebReceiptSummary)e.DataItem).PolicyNo;
}
UPDATE
Thinking about it, rather than using the code-behind setting of the CommandArgument, I think (I haven't tested this yet) you could actually do the following without needing the receipts_ItemDataBound function...
<td class="r"><asp:LinkButton ID="pdfLink" OnCommand="PDFLinkClick" runat="server"
CommandArgument="<%#((WebReceiptSummary)Container.DataItem).PDF + ((WebReceiptSummary)Container.DataItem).PolicyNo%>"
>View PDF</asp:LinkButton></td>
UPDATE 2
All instances of Container.DataItem in the above examples, have been changed into the tight-bound ((WebReceiptSummary)Container.DataItem)
I'm trying to dynamically create a listview. on reports.aspx user selects a bunch of checkboxes. On the next page, user sees reports.aspx, and should see a table with columns of the checkboxes he selected. My idea was to create a listview, then dynamically change the header row of the LayoutTemplate, then change the select statement depending on which columns selected. This is what I have:
<asp:ListView runat="server" ID="ReportListView" DataSourceID="ReportListViewSDS">
<LayoutTemplate runat="server">
<table runat="server">
<tr runat="server">
<%
' Doesn't work because code blocks (<%%>) aren's allowed inside <LayoutTemplate> blocks
'For Each i As String In Request.Form
'Response.Write("<th>" & Request.Form(i) & "</th>")
'Next
%>
</tr>
</table>
<asp:PlaceHolder runat="server" ID="itemPlaceHolder" />
</LayoutTemplate>
...
Problem is that this doesn't work because i can't put a code block (<%%>) inside the LayoutTemplate. Is there a way in the code behind to edit the LayoutTemplate, or another way to cycle through the Request.Form vars and build the table header row with it?
Thanks for any advice/direction!
-Russ
Try using the ItemTemplate for the binding syntax instead of the layout template. I believe the layout template is strictly for layout.
Also, it looks like you're using classic ASP code blocks. ASP.NET code blocks look like this:
For data binding:
<%# Eval("<COLUMN NAME>")%>
For other cases not involving data binding:
<%= Request.QueryString["Hello"] %>
Since the control is already a server side control, try giving the and id and then modifying the header on pre-render:
<asp:ListView runat="server" ID="ReportListView" DataSourceID="ReportListViewSDS">
<LayoutTemplate runat="server">
<table runat="server">
<tr id='trCustomHeader" runat="server">
Then in your code behind, attach this logic to the listview's pre-render
ReportListView_PreRender(...)
{
TableRow tr = ReportListView.FindControl("trCustomerHeader");
TableCell tempCell = new TableCell();
tempCell.Text = ...
tr.Cells.Add(tempCell);
}
I just created a separate table on the page, outside of the listview, that was the simple way of doing it.
<asp:Table ID="HeaderTable" runat="server">
<asp:TableHeaderRow ID="HeaderTableHeaderRow" runat="server" />
</asp:Table>
<asp:ListView ...>
...
</asp:ListView>
Then in the code behind:
For Each i As String In Request.Form
If i.IndexOf("checkbox_") = 0 Then
Dim c As New TableHeaderCell()
Dim l As New LinkButton()
l.Text = i.Substring(Len("checkbox_"))
c.Controls.Add(l)
c.CssClass = "customreport"
HeaderTableHeaderRow.Cells.Add(c)
End If
Next
Pretty simple. So I didn't have to use a LayoutTemplate at all.
I have the following code in the EditItemTemplate of my FormView:
<tr id="primaryGroupRow" runat="server">
<td class="Fieldname">Primary Group:</td>
<td><asp:DropDownList ID="iPrimaryGroupDropDownList" runat="server" DataSourceID="GroupDataSource" CssClass="PageText"
DataTextField="sGroupName" DataValueField="iGroupID" SelectedValue='<%# Bind("iPrimaryGroup") %>'></asp:DropDownList></td>
</tr>
If I remove the runat="server" for the table row, then the iPrimaryGroup field is bound 100% and passed to the business logic layer properly. However in the case of the code above, it is passed with a value of zero.
Can anyone tell me why this is or how to get around it? This is in a control that needs to hide this table row, based on whether or not an administrator or a regular user is editing it. ie: some fields are admin writeable only and I'd like to hide the controls from the view if the user isn't an admin.
If security is a concern perhaps this might work better
<tr>
<td colspan='2'>
<asp:panel runat='server' visible='<%= IsUserAdmin %>'>
<table>
<tr>
<td class="Fieldname">Primary Group:</td>
<td><asp:DropDownList ID="iPrimaryGroupDropDownList" runat="server" DataSourceID="GroupDataSource" CssClass="PageText" DataTextField="sGroupName" DataValueField="iGroupID" SelectedValue='<%# Bind("iPrimaryGroup") %>'></asp:DropDownList>
</td>
</tr>
</table>
</asp:panel>
</td>
If I'm not mistaken any markup within the panel will not be rendered if visible=false
Have a shot at this:
Remove the runat=server attribute
Define a css class
.hidden{ display:hidden;}
Then set the class attribute based on whether or not the user is an admin
<tr class='<%= if(IsUserAdmin) "" else "hidden" %>' >
It appears that this functionality is by design, although that's not exactly confirmed.
http://weblogs.asp.net/rajbk/archive/2009/08/03/formview-binding-gotcha.aspx
When using the FormView object, if you have a nested control, then two-way databinding isn't going to work properly. You can access the controls in code, and you can get at the data, but it's just not going to automatically update the value in the back end of your Business Logic Layer(BLL) like it's supposed to.
Fortunately, there's a workaround. The way to get it working is to create an event for ItemUpdating. It will have a signature like this:
protected void frmProfile_ItemUpdating(object sender, FormViewUpdateEventArgs e)
This gives you access to the FormViewUpdateEventArgs, which in turn allows you to make changes to the ObjectDataSource values while they are in flight and before they hit your BLL code, as follows:
protected void frmProfile_ItemUpdating(object sender, FormViewUpdateEventArgs e)
{
if (frmProfile.FindControl("iPrimaryGroupDropDownList") != null)
{
DropDownList iPrimaryGroupDropDownList = ((DropDownList)frmProfile.FindControl("iPrimaryGroupDropDownList"));
e.NewValues["iPrimaryGroup"] = iPrimaryGroupDropDownList.Text;
}
}
I am having a problem with dynamically assigning datasource to listview.
For example I have list of receivedBonuses(Bonus), receivedLeaves(Leave) and I want listview to display those list items depending on what link button user clicked.
Researching internet and stackoverflow.com i found 3 solutions:
Using repeater inside the listview. But in my case, I could not apply it to my case and i got totally confused
Using nested listviews. I tried to do like this:
<asp:ListView ID = "bonuses" runat="server" DataSource ='<%# Eval("received_bonuses") %>' >
<ItemTemplate>
<tr>
<td><%# Eval("bonus_desc")%></td>
<td><%# Eval("bonus_type")%></td>
</tr>
</ItemTemplate>
<LayoutTemplate>
<table>
<tr>
<th>Bonus Description</th>
<th>Bonus Received Date</th>
</tr>
<tr ID="itemPlaceholder" runat="server" />
</table>
</LayoutTemplate>
<table>
<tr>
<th>Bonus Description</th>
<th>Bonus Received Date</th>
</tr>
<tr ID="itemPlaceholder" runat="server" />
</table>
</LayoutTemplate>
</asp:ListView>
<br />
and on back code I tried to write like this:
protected void dataBound(object sender, ListViewItemEventArgs e)
{
this.DataBindChildren();
}
It didn't give any errors it just didn't work.
Using data pager
I have no idea how to apply it to my case.
Any help is appreciated.
Thanks a lot.
All you have to do on the server side is change the DataSource or DataSourceID property and call DataBind on the ListView.
You have to make sure when using <%# Eval("") %> syntax that the objects you are binding to have those properties that are named in the Eval. So you may have a problem with with switching datasources when your properties are prepended with the typename and underscore.
That being said. There are 2 options you have an changing a data source. In the click event of the button or whatever switching mechanism you are using you can just write something like.
Not using a DataSource in the markup:
List<Bonus> bonusList = GetBonuses();
MyListView.DataSource = bonusList;
MyListView.DataBind();
Using a DataSource in the markup:
//where bonus list would be the id of the datasource in the markup
MyListView.DataSourceID= "BonusList";
MyListView.DataBind();
Do you need to do this dynamically? If you only have "bonuse" and "leave" can you not create two listviews and then just do display logic to visible=true/false the listview based upon the link button clicked?