Passing Params From Aspx - asp.net

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)

Related

ID of items in ASP.NET ListView is automatically extended

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?

Databinder inside a "for" loop in an ASPX page

I'm still trying to understand how I can take advantage from DataBinder ( Is there a way to use a DataBinder.Eval statement as an index of a specific array in an ASPX page? ).
I'm currently building tables with the help of repeater and I would like to use a loop defining the Item label dynamically, to allow more interactions.
Currently, this test code is working:
<asp:Repeater id="Fish" runat="server">
<table>
<ItemTemplate>
<tr>
<td><%# Container.DataItem("ITEM")%></td>
<td><%# Container.DataItem("AGG")%></td>
</tr>
</ItemTemplate>
</table>
</asp:Repeater>
But as you can imagine, this type of structure doesn't allow to choose dynamically the columns that are displayed from the columns to be ignored.
I was thinking that by using a "for" loop structure, I would be able to choose dynamically which column could be displayed. And I tried this as a test:
Public Test_id() As String
Public Test_idp As String
<% Test_id = New String() {"id", "Agg"} %>
<asp:Repeater id="Fish" runat="server">
<table>
<ItemTemplate>
<tr>
<% For Each Test_idp as String In Test_id%>
<td><%# Container.DataItem(Test_idp)%></td>
<% Next Test_idp%>
</tr>
</ItemTemplate>
</table>
</asp:Repeater>
which is not working... and is granted by the following error message:
Overload resolution failed because no Public 'Item' is most specific for these arguments:
'Public Overrides ReadOnly Property Item(name As String) As System.Object': Not most specific.
'Public Overrides ReadOnly Property Item(i As Integer) As System.Object': Not most specific.
Any idea?
Edit:
to answer Mike C's question, I have tried DataBinder.Eval(Container.DataItem, Test_idp) instead of Container.DataItem(Test_idp). It still does not work, but the error is different:
System.ArgumentNullException: value cannot be null
Test_Idp is an Object (since it wasn't declared otherwise).
Therefore, the compiler cannot figure out which of those overloads to call.
You need to explicitly declare it As String.
You can use a nested repeater for the columns.
<asp:Repeater id="Fish" runat="server">
<table>
<ItemTemplate>
<tr>
<asp:Repeater id="columns" runat="server">
<ItemTemplate>
<td><%# ((RepeaterItem)Container.Parent.Parent).DataItem("ITEM")%></td>
<td><%# ((RepeaterItem)Container.Parent.Parent).DataItem("AGG")%></td>
</ItemTemplate>
</asp:Repeater>
</tr>
</ItemTemplate>
</table>
</asp:Repeater>

How can I use a FOR loop inside a REPEATER to create <TD>'s in a <TABLE>?

I have a repeater that is bound to a List(of T) object collection. It's a list of Inventory objects. Each Inventory object also contains a List(of T) which is a list of Date / Inventory Count pairs. When the repeater is creating a table, I need to create a TD for each of the Date / Inventory Counts. Since the number of Date / Inventory Counts is not set until runtime (using NumWeeks variable), I need to vary the number of TD's in my repeater. This is essentially what I want:
<asp:Repeater ID="rptReport" runat="server">
<ItemTemplate>
<tr>
<td><%#DataBinder.Eval(Container.DataItem, "Department")%></td>
<td><%#DataBinder.Eval(Container.DataItem, "Description")%></td>
<% For x = 0 To NumWeeks%>
<td><%#DataBinder.Eval(Container.DataItem, "Values")(x).Value()%></td>
<% Next%>
</tr>
</ItemTemplate>
</asp:Repeater>
You need to place another repeater inside this repeater and assign the datasource to that inner repeater in the "ItemDataBound" event of the parent repeater. This should solve your issue.
Hope this is helpful!!
Another option would be that you create a UserControl containing a Repeater for the inner loop. You can assign the "Values" as a property of the UserControl. Something like this:
<asp:Repeater ID="rptReport" runat="server">
<ItemTemplate>
<tr>
<td><%# Eval("Department") %></td>
<td><%# Eval("Description") %></td>
<uc:WeekControl NumWeeks="<%#NumWeeks %>" Values='<%# EVal(Values)%> />
</tr>
</ItemTemplate>
</asp:Repeater>
I needed that for loop I tried another approach and it worked.
in my .cs file I created a public string and used it inside the repeater. It works fine.
public string somethingloop
for (int i = 0; i < dolar; i++)
{
somethingloop += "<i class='fa fa-dollar icon highlighted'></i>";
}
and in repeater inside anywhere in the itemtemplate
<asp:Repeater ID="rptReport" runat="server">
<ItemTemplate>
<%= somethingloop %>
<tr>
<td><%# Eval("Department") %></td>
<td><%# Eval("Description") %></td>
<uc:WeekControl NumWeeks="<%#NumWeeks %>" Values='<%# EVal(Values)%> />
</tr>
</ItemTemplate>
</asp:Repeater>

How to pass custom value to asp.net control on aspx page?

I need to generate the following in ASP.NET page. What's the best, easiest way to do that?
Simplified example. So to custom property I need to pass string which would include index (i) to the property of the control. I could do it from codebehind, but it would be simpler and easier if I could keep it in .aspx file.
<table>
<%
for( var i = 0; i < 10; i++ )
{
%><tr>
<td>
<cc1:CustomControl runat="server" CustomProperty="SomeText[<% i %>]"/>
</td>
</tr>
<% } %>
</table>
Essentially I need to pass a custom, not predetermined value to the asp.net control.
This probably won't work how you expect it to. Instead, add a place holder like this:
<table>
<asp:PlaceHolder id="RowPlaceHolder" runat="server" />
</table>
And then in your code behind:
for (int i = 0;i<10;i++)
{
var tr = new HTMLTableRow();
var td = new HTMLTableCell();
var Custom = (CustomControl)LoadControl("MyControl.ascx");
Custom.id="Custom" + i.ToString();
Custom.CustomProperty = "SomeText[" + i.ToString() + "]";
td.Controls.Add(Custom);
tr.Controls.Add(td);
RowPlaceHolder.Controls.Add(tr);
}
Going deeper, if the number 10 really is hard-coded, you'll find things much easier to deal with in the long run if you just go ahead and copy 10 entries for your control into the aspx markup manually. Dynamic controls in ASP.Net webforms are rife with pitfalls and gotchas.
If the number comes from some legitimate datasource, then you're likely better off actually using that datasource and binding it to a data control like a repeater.
<%= i %>
Should work for you.
You could rock out with a repeater and use the Container.ItemIndex,
<asp:Repeater ID="rpt" runat="server">
<HeaderTemplate>
<table>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<cc1:CustomControl runat="server" CustomProperty="SomeText[<%# Container.ItemIndex %>]"/>
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>

How to dynamically assign datasource to listview

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?

Resources