c# asp.net repeater - create button based on condition - c#

Basically what i would like to do is have a button which is only created for entries that have been flagged in a separate table for sending. So for example if i edit a record, i flag this other table and would like a send button to appear next to that record via the repeater below - is this possible? Can i do a check by calling a method for example unsure :(
<asp:Repeater ID="DepartmentsList" runat="server">
<HeaderTemplate>
<table id="grouptable" class="table table-bordered table-striped">
<thead>
<tr>
<th>Send</th>
<th>ID</th>
<th>Name</th>
<th>Last Modified</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<input type="checkbox" name="sendSelect[]" value="<%# Eval("Dept_ID") %>"</input></td>
<td><%# Eval("Dept_ID") %></td>
<td><%# Eval("DESC") %> </td>
<td><%# Eval("CHG_DATE") %></td>
<td><a class="btn btn-info" href="<%# Eval("gURL") %>"><i class="icon-pencil icon-white"></i> Edit</a><asp:Button ID="Button1" runat="server" Text="Send" /></td>
</tr>
</ItemTemplate>
<FooterTemplate>
</tbody>
</table>
</FooterTemplate>
</asp:Repeater>

You can do a check and inspect the current data record being bound to the repeater by using the DepartmentsList_OnItemDataBound event. It gives you all the power you need to dynamically change what's being bound and created in the repeater's item.

Related

Asp.Net adding an extra td

I'm trying to use an ASP:DataList to display data from my data source. Everything works ok, except I'm getting ASP to add an extra column when there isn't one. The Header Template and Item Template are shown below
<asp:DataList runat="server" ID="xTable" DataKeyField="PK_PurchaseID" DataSourceID="SqlDataSource1" RepeatLayout="table" CssClass="table">
<HeaderTemplate>
<td>PuchaseID</td>
<td>Last Name</td>
<td>First name</td>
<td>Address1</td>
<td>Email Sent</td>
</HeaderTemplate>
<ItemTemplate>
<td><%# Eval("PK_PurchaseID") %></td>
<td><%# Eval("LastName") %></td>
<td><%# Eval("FirstName") %></td>
<td><%# Eval("Address1") %></td>
<td><%# Eval("[Email Sent]") %></td>
</ItemTemplate>
</asp:DataList>
And is the the DOM object created. I would expect 5 columns in the table yet it is rendered as 6
<table class="table" id="xTable" style="border-collapse: collapse;" cellspacing="0">
<tbody><tr>
<td>
<td>PuchaseID</td>
<td>Last Name</td>
<td>First name</td>
<td>Address1</td>
<td>Email Sent</td>
</tr><tr>
<td>
<td>3</td>
<td>Albdddim</td>
<td>James </td>
<td>1----63rd Ave.</td>
<td>true</td>
</tr>
I can not figure you why an initial column is being created. I changed the CSS, even removed it and still the same result. I have verified the SQL only returns the 5 columns (even if it returned more or less that shouldn't make a difference)
Thanks Chris
The HeaderTemplate and ItemTemplate are meant to be td elements so it automatically creates the td. If you do something like this
<asp:DataList runat="server" ID="xTable" DataKeyField="PK_PurchaseID" DataSourceID="SqlDataSource1" RepeatLayout="table" CssClass="table">
<HeaderTemplate>
PuchaseID
<td>Last Name</td>
<td>First name</td>
<td>Address1</td>
<td>Email Sent</td>
</HeaderTemplate>
<ItemTemplate>
<%# Eval("PK_PurchaseID") %>
<td><%# Eval("LastName") %></td>
<td><%# Eval("FirstName") %></td>
<td><%# Eval("Address1") %></td>
<td><%# Eval("[Email Sent]") %></td>
</ItemTemplate>
</asp:DataList>
It would put the purchase ID in the first td that is created and then fill out the table like you want.

Dynamically change repeater's data item

I have an <asp:Repeater> control that loads its data from a database.
To specify which column will be used where in the .aspx file, I use inside the Repeater's ItemTempate
<%# DataBinder.Eval(Container.DataItem, "column1")%>
Now when I need to make changes eg. I want another column instead of column1, I have to open Visual Studio, make the changes, publish the project again, and upload it to the server.
How can this control be more adjustable without having to change the source code each time?
Is there any other control that will do what I need better?
ASPX markup:
<table class="uk-table">
<thead>
<tr>
<th>User Name</th>
<th>Date 1</th>
<th>Date 2</th>
<th>State</th>
</tr>
</thead>
<tbody>
<asp:Repeater runat="server" ID="Repeater2">
<HeaderTemplate></HeaderTemplate>
<ItemTemplate>
<tr class="uk-table-middle">
<td><%# DataBinder.Eval(Container.DataItem,"column1")%></span></td>
<td><%# DataBinder.Eval(Container.DataItem,"column2")%></td>
<td><%# DataBinder.Eval(Container.DataItem,"column3")%> </td>
<td><%# DataBinder.Eval(Container.DataItem,"column4")%></span></td>
</tr>
</ItemTemplate>
<FooterTemplate></FooterTemplate>
</asp:Repeater>
</tbody>
</table>
You need to define a condition like the following and save your condition either in the database or an XML file or even in the web.config (on yours) and do the following. Don't forget to retrieve your condition out of the repeater if you wanna apply to all rows:
<%
var myCondition = true; // whatever it could be...
%>
<asp:Repeater runat="server" ID="Repeater2">
<HeaderTemplate></HeaderTemplate>
<ItemTemplate>
<tr class="uk-table-middle">
<% if (mycondition){ %>
<td><%# DataBinder.Eval(Container.DataItem,"column1")%></span></td>
<td><%# DataBinder.Eval(Container.DataItem,"column2")%></td>
<% }else{ %>
<td><%# DataBinder.Eval(Container.DataItem,"column2")%></td>
<td><%# DataBinder.Eval(Container.DataItem,"column1")%></span></td>
<% } %>
<td><%# DataBinder.Eval(Container.DataItem,"column3")%> </td>
<td><%# DataBinder.Eval(Container.DataItem,"column4")%></span></td>
</tr>
</ItemTemplate>
<FooterTemplate></FooterTemplate>
</asp:Repeater

Implement accordion within table and asp:repeater

I create table inside a repeater
and the data appears correctly, it will be two records looks like in the image below.
what i want: how to make accordion when user clicks on the deposit number(Example 16), new table will appear and contain data.
this is the repeater the first repeater code:
<asp:Repeater ID="rptDep" runat="server" >
<HeaderTemplate>
<table class="table table-hover table-striped table-condensed table-bordered table-responsive">
<tr>
<th>Deposit No.</th>
<th>Custom Declaration No.</th>
<th>Category</th>
<th>Location</th>
<th>Goods Description</th>
<th>Units Balance</th>
<th>WT Balance</th>
<th>Goods Balance Amount(LC)</th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td> <%#Eval("depNo") %></td>
<td><%#Eval("customDec") %></td>
<td><%#Eval("category") %></td>
<td><%#Eval("location") %></td>
<td><%#Eval("goodDesc") %></td>
<td><%#Eval("unitsBal") %></td>
<td><%#Eval("wtBal") %></td>
<td><%#Eval("lcBal") %></td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
I know that it will be a second repeater, but how to implement that, or how to use foreach statement?
I got it, I found the solution by reading this article:
Implement Nested Repeater (Repeater inside Repeater) with example in ASP.Net using C# and VB.Net

ASP.NET UpdatePanel Set value for Checkbox

I am using a ListView that contains a checkbox. I would like the checkbox to automatically update a bit field in my database from False to True (or visa versa) when the box is checked.
I am not familiar with the update panel and I'm struggling to find a way to achieve this.
My List View
<asp:ListView ID="ListView1" runat="server" DataKeyNames="NotificationID" DataSourceID="SqlDataSource1">
<EmptyDataTemplate>
<table runat="server" style="">
<tr>
<td>No notifications exist.<br /> NOTE: You must be logged in to see notificaitons</td>
</tr>
</table>
</EmptyDataTemplate>
<ItemTemplate>
<tr class="odd gradeX">
<td><span style="display: none;"><%# ("00000000" + Eval("DateDescNumber")).TxtStrRight(8) %> - </span><%# GlobalUtilities.GetDiffDate(Convert.ToDateTime(Eval("DateTimeLogged"))) %></td>
<td><%# Eval("UserName") %></td>
<td><%# Eval("NotificationTitle") %></td>
<td class="center"><%# Eval("NotificationMessage") %></td>
<td class="center">
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:updatepanel id="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:CheckBox ID="CheckBoxMarkedAsRead" runat="server"
Checked='<%# Eval("MarkedAsRead") %>'
AutoPostBack="true"
Visible="true"
Text =" Not Selelected"
oncheckedchanged="CheckBox_MarkedAsRead_CheckedChanged" />
</ContentTemplate>
</asp:updatepanel>
</td>
</tr>
</ItemTemplate>
<LayoutTemplate>
<table cellpadding="0" cellspacing="0" border="0" class="responsive dynamicTable display table table-bordered" width="100%">
<thead>
<tr>
<th style="width:80px">Date</th>
<th>From</th>
<th>Notice</th>
<th>Description</th>
<th class="ch" style="width:53px;">Read</th>
</tr>
</thead>
<tbody>
<asp:PlaceHolder ID="itemPlaceholder" runat="server"></asp:PlaceHolder>
</tbody>
</table>
</LayoutTemplate>
</asp:ListView>
I have gone as far as creating the CheckBox_MarkedAsRead_CheckedChanged event in my Code behind (at least I think this is what I should do) but my attempts to script a solution in the code behind has been fail after fail.

How to get Dynamic Span id in jquery?

Iam using jquery in asp.net
I have one user control in which i have div and in div table and in table tr and in tr td and in td i have lables.
ASCX :
<div ID="Container" runat="server" class="dvContainer">
<table ID="Table" class = "Tablecs">
<thead><tr>
<td colspan="2">
<asp:Label ID="lbl1" Text="Swiss" runat="server" /></td>
</tr>
</thead>
<tr>
<td>ABC</td>
<td>DEF</td>
</tr>
<tr>
<td><asp:Label ID="lblPA" Text="SUN 12/21 05:04" runat="server" /></td>
<td ><asp:Label ID="lblPD" Text="SUN 12/21 19:00" runat="server" /></td>
</tr>
<tr>
<td><asp:Label ID="lblAA" Text="SUN 12/21 05:04" runat="server" /></td>
<td ><asp:Label ID="lblAD" Text="SUN 12/21 19:00" runat="server" /></td>
</tr>
</table>
i want to bind data dynamically to these user control. i.e., binding data to lables in user contol.
my Jquery
$div.find(".Table").text(oPorts[iCount].Name); // my result is an array of oPorts and i have filed as Name
But this is not working fine.
when i checked into code dynamically its generating SPAN for each and every lable
How to find that SPAN id dynamiccaly in a loop and bind data to lables??
Any help is appreciated. Thanks in Advance.
suppose you have a usercontrol with a markup like given below
<%# Control Language="C#" AutoEventWireup="true"
CodeBehind="WebUserControl1.ascx.cs" Inherits="DOTNET_FORMS.WebUserControl1" %>
<div id="Container" runat="server" class="dvContainer">
<table id="Table" class="Tablecs">
<tr>
<td>
<asp:Label ID="lblPA" Text="SUN 12/21 05:04" runat="server" />
</td>
<td>
<asp:Label ID="lblPD" Text="SUN 12/21 19:00" runat="server" />
</td>
</tr>
</table>
</div>
and you have registered this usercontrol on your .aspx page like this:
<%# Register TagPrefix="UC" TagName="Test" Src="~/WebUserControl1.ascx" %>
and used it like this:
<UC:Test ID="uc1" runat="server" />
then, when you run your page, your elements get rendered something like
<div id="uc1_Container" class="dvContainer">
<table id="Table" class="Tablecs">
<tr>
<td>
<span id="uc1_lblPA">SUN 12/21 05:04</span>
</td>
<td>
<span id="uc1_lblPD">SUN 12/21 19:00</span>
</td>
</tr>
<tr>
<td>
<span id="uc1_lblAA">SUN 12/21 05:04</span>
</td>
<td>
<span id="uc1_lblAD">SUN 12/21 19:00</span>
</td>
</tr>
</table>
</div>
see how ids of your labels and other elements (elements with runat="server" attribute ) got changed i.e.
lblPA > uc1_lblPA
lblPD > uc1_lblPD
Container > uc1_Container
so, you have to look out for these changes, because only then you can grab these elements using jQuery, cause jQuery is a client side language, it executes, after the server side code (runat="server") has executed.
However, if you do not want to look out for modified id, you can do following
remove runat="server" attribute, and make sure your ids are unique, all of them
let the runat="server" attribute be their, place an attribute clientidmode="static" on all of your server side controls. and Ids wont change.
use ClientId i.e. in your jQuery selector, grab an element like this: $('#"+'<%= lblPA.ClientID %>');
now, since your IDs are unique, you don't need to find, directly grab the elements like this:
$('#lblPA').text();
or if you want to loop through all the tds of your table with class Tablecs, do this:
$('.Tablecs tr td').each(function(index, item){
alert($(item).text());
});

Categories

Resources