Using "if" in ItemTemplate - c#

I'm writing a repeating payments migration tool for moving from one payment system to another. All of the users' credit card and bank information is stored on those systems, so I need users to reenter the information, as I don't have it stored in my own database.
Users can either update their information or choose to delete their recurring payment and not migrate it to the new system. My migration data object includes an enum called UpdateAction with values Delete and Update.
I'm writing a review-your-input page and have a ListView control with an item template that displays all the information. When the action is to delete, there's no other information to display, so I have:
<asp:ListView ID="lvReview" runat="server">
<ItemTemplate>
<table>
<tr>
<td colspan="2"><b><%# Eval("PaymentProfileName") %></b></td>
</tr>
<tr><td>Action</td><td><%# Eval("Action").ToString() %></td></tr>
<% if ((UpdateAction)Eval("UpdateAction") == UpdateAction.Update)
{ %>
<!-- Update item bound content here -->
<% } %>
</table>
</ItemTemplate>
</asp:ListView>
This doesn't work because you have to use <%# ... %> in order to use Eval(), in which case, you can't use the if. Is there a way to reference the data item being bound in the ascx file itself? Or must I make a bunch of literal controls and put my if in a OnItemDataBound event handler?

I think you're on the right track with the OnItemDataBound event. Put your Update item bound content in a panel that you can set it's visiblity...
UpdateAction action = DataBinder.Eval(e.Item.DataItem, "UpdateAction");
if (action == UpdateAction.Update)
{
pnlUpdateItems.Visible = true;
}
else
{
pnlUpdateItems.Visible = false;
}

Related

.NET webform Hide a HTML td column in backend server (Update Panel, Content Template)

I have an instance, it shows a listView.
the listview is in the update panel, it needs to response to each data source binding and show/hide a column in the listView table by checking a Session.
I cannot simply add
<% if((int)Session["v1"] ==1) { %> <td>Hi</td> <%}%>
as the exception throw saying that the update panel cannot update when <% %> exists.
(but it works for the control outside of the update panel)
I think I can do it by javascript but I just want to make sure if there is a smarter way to do in the backend.
class='<%# HiddenClass %>'
and changing this parameter during onload (change HiddenClass to empty string if the column should show.
It works for the item's column (in the ItemTemplate) but it doesn't work for the item's header column LayoutTemplate (i think it is because data source binding only re-rendered the fields in the ItemTemplate and not included LayoutTemplate.
currently, I was able to hide it to set the runat="server" Visible to false if for each data source binding of the listview. But it looks very complicated when I need to hide more columns (need to create more ID and asp.net cannot set visible of fields by class).
Wrap the <td></td> with a PlaceHolder and set it's Visibility property from code behind.
<asp:PlaceHolder ID="PlaceHolder1" runat="server" Visible="false">
<td>Hi</td>
</asp:PlaceHolder>
And then in code behind
if ((int)Session["v1"] == 1)
{
PlaceHolder1.Visible = true;
}

Show updated record without refresh

I have 2 page.
When double click row data in first page will be go to second page.
second page is for update purpose.
when successful update in second page and i want back to first page, the data show not updated in first page but when refresh the data will show updated record.
How can i get the updated record show in first page without click refresh?
below is first page aspx code:
<asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate>
<tr style="cursor: hand;" onmouseover="this.style.backgroundColor='#E6F0FF'"
onmouseout="this.style.backgroundColor='#FFFFFF'"
onclick="javascript:window.location.href='V_Updated.aspx?mail=<%#Eval("mail") %>'">
<td class="line_table_td" style="text-align: center;"><%#Eval(" mail ")%> </td>
<td class="line_table_td" style="text-align: center;"><%#Eval(" remarks ")%> </td>
</tr>
</ItemTemplate>
</asp:Repeater>
second page aspx button back code:
<td class="btn_bg" onclick = "history.back();">Back</td>
Better to use AJAX for updating data, if it is suitable for your project. This way you do not have to use two different pages.
http://www.webblogsforyou.com/ajax-introduction-how-to-use-ajax-in-asp-net-with-example/
Thanks!
Don't go back using the browser history (e.g. using history.back()). Different browsers handle caching differently, so it's difficult to control if your page would get refreshed. Provide a proper link to the previous page instead.
Simply put a link in second page like below :
<a href="firstpage.aspx" > Back </a>

Problem with dynamic table in ascx control

I have the following table in an ascx user controll:
<tr runat="server" id="rowChangeSerNo">
<td colspan="2">
<table id="tblChangeSerNo" runat="server">
</table>
</td>
</tr>
<tr id="row" runat="server">
<td>
<asp:Button ID="btChangeSerNo" runat="server" Text="Update" OnClick="btChangeSerNo_onClick" />
</td>
</tr>
I create the tblChangeSerNo dynamically with text boxes prefilled with the current values in the db. The idea of the control is to allow the user to update the values of the DB with new values. The problem is that when the btChangeSerNo_onClick method is called:
The table is not rendered, since I do it on pre-render
Even if I rendered the table on Page_Load I could not access updated values of the user because they are lost.
How can I solve this problem?
The best practice way will be to use Grid control instead.
If you prefer to stick with your own code, follow those steps:
Store all the text boxes in global fields, e.g. public List<TextBox> m_tableTextboxes = new List<TextBox>(); and when creating add to that list.
Have the code creating the dynamic controls in the Page_Load but execute it only when not is PostBack: if (!Page.IsPostBack) { ... }
In the button click event, read the values from your global field.
Done something similar in the past, so the concept should work.

How can I use the ScrollTo jQuery plugin to scroll to particular row in a repeater in ASP.NET?

I have a simple repeater that looks like:
<asp:Repeater runat="server" ID="rptOptions" OnItemDataBound="rptOptions_ItemDataBound">
<HeaderTemplate>
<thead>
<tr>
<td class="GridHeader">Account</td>
<td class="GridHeader">Margin</td>
<td class="GridHeader">Symbol</td>
<td class="GridHeader">Usymbol</td>
</tr>
</thead>
</HeaderTemplate>
<ItemTemplate>
<tbody>
<tr runat="server" ID="trOption">
<td class="GridRow"><asp:Label runat="server" ID="lblOptionAccount"></asp:Label></td>
<td class="GridRow"><asp:Label runat="server" ID="lblOptionMargin"></asp:Label></td>
<td class="GridRow"><asp:Label runat="server" ID="lblOptionSymbol"></asp:Label></td>
<td class="GridRow"><asp:Label runat="server" ID="lblOptionUsymbol"></asp:Label></td>
</tr>
</tbody>
</ItemTemplate>
</asp:Repeater>
Now, in my code-behind I have an event which is fired that is supposed to add/insert a row into the database. After this happens, I re-grab the new list of options from the database and re-bind them to the repeater. This takes place inside an update panel so the list refreshes right away for the user.
protected void lbtnAddOptionSave_Click(object sender, EventArgs e)
{
SelectedOption = new Option()
{
Account = txtAddOptionAccountNumber.Text,
Margin = chkAddOptionMargin.Checked,
Symbol = txtAddOptionSymbol.Text,
Usymbol = txtAddOptionUsymbol.Text,
};
Presenter.OnAddOption(); // Insert new option into database
RefreshOptions(); // Re-get list of options, bind them to repeater
}
Now, what I would ~love~ to do, is use the jQuery ScrollTo plugin to scroll straight to the newly added row.
What would be the best way to call the ScrollTo() method in the jQuery plugin so I scroll to that particular row that was just added? Is there anyway I can mark my rows in my ItemTemplate so I can easily select an element to scroll to via jQuery?
Ideally, right after RefreshOptions() I would like to execute the ScrollTo function to scroll down to the new row.
If you know the client side Id of the row (which you can get), its relatively painless to simply call
$(document).scrollTo("#<row-id-here>", 800);
When you add the object to the database (or just after that), grab the id of the newly inserted object. Modify the repeater to include a Label with Visible="false" so that it doesn't get rendered to the client. Hook into the ItemDataBound event and check each label against the id you've grabbed. When you find the matching row, you can get the id of the row and then you'll be able to use for the scrolling parameter.
Note: Other data-bound controls have a DataKey property which could be used for the id of the object and make this a bit simpler. Not sure if you're tied to the Repeater at this point, but a GridView or ListView could be worth looking into.

Add controls dynamically to a listview

I have an asp.net listview. The data that populates it is sorted into groups, and i'm using some code in the listview item template, that essentially checks if the grouping field has changed in the data, and if so it prints a new row, with the heading, so it looks a bit like this:
<ItemTemplate>
<%# AddGroupingRowIfWPHasChanged() %>
<tr id="row" class="GridViewRow" runat="server" >
<td valign="top" align="left" ><%# Eval("item1") %></td>
.......
</itemTemplate>
This works great. However now I need to add a button the the group heading field, and I can't seem to get this to work. Obvioulsy this button only needs adding if the heading has changed, but the AddGroupingRowIfWPHasChanged() method does not seem to have access to the current ListviewItem, so I can't add a control.
Any suggestions on how I can add a control to a list view dynamically, depandant on whether its a group heading or not?
It may be easier to override the ItemInserting event in the codebehind and do it from there
Use a placeholder?
.aspx
<asp:Placeholder id="plhControlHolder" runat="server" />
code-behind
' ** use FindControl if instead a databound event, otherwise you could skip.
Dim plcControlHolder as PlaceHolder = e.Row.FindControl("plcControlHolder")
Dim btnDynamic As New Button
btnDynamic.Id = "MyButton"
plcControlHolder.Controls.Add(btnDynamic)
Try using the GroupTemplate and GroupSeparatorTemplate instead of trying to fit it all in the ItemTemplate.
Create a UserControl to include in place of the AddGroupingRowIfWPHasChanged() method call. Override Render(). Use the logic that's currently in AddGroupingRowIfWPHasChanged() to render the control if the group has changed; otherwise, render an empty string.

Categories