aspx repeater Do something with the data - asp.net

I have made a webform, and by using a repeater i would like to display the results with the option of approving/disapproving applicants. I have searched high and low, but I am unable to discover how i can use the data of the item in the item repeater. If someone could point me in the right direction as to how i could do this i would appreciate it alot.
So as example. Someone presses the button approve in one of the repeated items. and i would like to do some db query using the email address which is displayed in that item
codebehind:
protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
{
if(((Button)e.CommandSource).Text.Equals("Approve"))
{
//
}
}
and this is the aspx:
<asp:Repeater ID="Repeater1" runat="server" OnItemCommand="Repeater1_ItemCommand">
<ItemTemplate>
<div class="Application">
<table class="auto-style1">
<tr>
<td class="small">Application received:</td>
<td class="big"><asp:Label ID="lbl_App_Date" runat="server" Text='<%# Eval("app_date") %>'></asp:Label></td>
<td class="small">Status:</td>
<td class="small"><asp:Label ID="lbl_App_status" runat="server" Text='<%# Eval("app_status") %>'></asp:Label></td>
</tr>
<tr>
<td class="small">Email Address: </td>
<td class="big"><asp:Label ID="lbl_App_email" runat="server" Text='<%# Eval("app_email") %>'></asp:Label></td>
<td class="small">Age: </td>
<td class="small"><asp:Label ID="lbl_App_age" runat="server" Text='<%# Eval("app_age") %>'></asp:Label></td>
</tr>
<tr>
<td>Country: </td>
<td><asp:Label ID="lbl_App_Country" runat="server" Text='<%# Eval("app_country") %>'></asp:Label></td>
<td>Platform: </td>
<td><asp:Label ID="lbl_App_Platform" runat="server" Text='<%# Eval("app_platform") %>'></asp:Label></td>
</tr>
</table>
<p><b>Motivation to join: </b></p>
<asp:TextBox ID="txt_App_Moti" ReadOnly="true" Text='<%# Eval("app_moti") %>' TextMode="MultiLine" Height="150px" Width="60%" runat="server"></asp:TextBox>
<asp:Panel ID="pan_approval" runat="server">
<table class="auto-style1">
<tr>
<td class="small"></td>
<td class="big"><asp:Button ID="btn_approve" runat="server" Text="Approve" /></td>
<td class="small"></td>
<td class="small"><asp:Button ID="btn_disapprove" runat="server" Text="Disapprove" /></td>
</tr>
</table>
</asp:Panel>
</div>
</ItemTemplate>
</asp:Repeater>

Related

Changing the color of a selected row in an ASP.NET Repeater

I have an ASP.NET repeater with an ImageButton. My ImageButton has an OnCommand event.
My goal: When I click the ImageButton, I would like the color of the selected row to change.
Here is an excerpt from my ASP.NET code. Can anyone help me?
<asp:Repeater ID="RepeaterID" runat="server" OnItemCommand="rpt_ItemCommand">
<HeaderTemplate>
<table cellpadding="0" cellspacing="0" id="table1">
<thead>
<tr>
<th>
<asp:Label ID="lbl_refCode" runat="server"></asp:Label>
</th>
<th style="width: 25px"></th>
</tr>
</thead>
</table>
</HeaderTemplate>
<ItemTemplate>
<tr id="row" runat="server">
<td style="width: 50px;">
<asp:Label ID="Label2" runat="server" Text='<%# Eval("RefCode") %>'</asp:Label>
</td>
<td style="width: 25px;">
<asp:ImageButton ImageUrl="Icons/edit.png" CommandArgument='<%# Eval("ID") %>' CommandName="Edit" ID="ImgEdit" OnCommand="Manage" runat="server">
</asp:ImageButton>
</td>
</tr>
</ItemTemplate>
<AlternatingItemTemplate>
<tr id="row" runat="server">
<td style="width: 50px;">
<asp:Label ID="Label2" runat="server" Text='<%# Eval("RefCode") %>' </asp:Label>
</td>
<td style="width: 25px;">
<asp:ImageButton ImageUrl="Icons/edit.png" CommandArgument='<%# Eval("ID") %>' CommandName="Edit" ID="ImgEdit" OnCommand="Manage" runat="server">
</asp:ImageButton>
</td>
</tr>
</AlternatingItemTemplate>
<FooterTemplate>
</Table>
</FooterTemplate>
</asp:Repeater>
Try using the OnItemCommand event of the Repeater instead of the LinkButton's OnCommand event. The RepeaterCommandEventArgs parameter will give you access to the whole Item instead of just the LinkButton, and you can set the background color of the table row.
<asp:Repeater ID="RepeaterID" runat="server" OnItemCommand="rpt_ItemCommand">
<HeaderTemplate>
<table cellpadding="0" cellspacing="0" id="table1">
<thead>
<tr>
<th>
<asp:Label ID="lbl_refCode" runat="server" </asp:Label>
</th>
<th style="width: 25px"></th>
</tr>
</thead>
</HeaderTemplate>
<ItemTemplate>
<tr id="row" runat="server">
<td style="width: 50px;">
<asp:Label ID="Label2" runat="server" Text='<%# Eval("RefCode") %>' </asp:Label>
</td>
<td style="width: 25px;">
<asp:ImageButton ImageUrl="Icons/edit.png" CommandArgument='<%# Eval("ID") %>' CommandName="Edit" ID="ImgEdit" runat="server">
</asp:ImageButton>
</td>
</tr>
</ItemTemplate>
<AlternatingItemTemplate>
<tr id="row" runat="server">
<td style="width: 50px;">
<asp:Label ID="Label2" runat="server" Text='<%# Eval("RefCode") %>' </asp:Label>
</td>
<td style="width: 25px;">
<asp:ImageButton ImageUrl="Icons/edit.png" CommandArgument='<%# Eval("ID") %>' CommandName="Edit" ID="ImgEdit" runat="server">
</asp:ImageButton>
</td>
</tr>
</AlternatingItemTemplate>
<FooterTemplate>
</Table>
</FooterTemplate>
</asp:Repeater>
protected void rpt_ItemCommand(object source, RepeaterCommandEventArgs e)
{
if (e.CommandName.Equals("Edit"))
{
HtmlTableRow newRow = e.Item.FindControl("row") as HtmlTableRow;
if (newRow != null)
newRow.BgColor = "#CCCCCC";
}
}
Note that in the ItemTemplate the table row has an id so it can be found in the ItemCommand argument.

add CSS to a table row when databinding an asp listview

I am populating a Listview control from my codebehind.
Does anyone know of a way i can assign a css style to a table cell, is say the lowest price is greater than the amazon price?
<asp:ListView ID="lv1" runat="server" ItemPlaceholderID="itemPlaceholder" OnPagePropertiesChanging="ChangePage">
<LayoutTemplate>
<table id="dataTable">
<thead>
<tr>
<th class="t-left">Product Title</th>
<th>SKU</th>
<th>Our Price</th>
<th>Amazon Price</th>
<th>Lowest Price</th>
<th>Actions</th>
</tr>
</thead>
<tbody id="dataResults" >
<tr id="itemPlaceholder" runat="server"></tr>
</tbody>
</table>
</LayoutTemplate>
<ItemTemplate>
<tr id='<%#Eval("sku") %>'>
<td class="t-left" width="360px">
<asp:HyperLink ID="prodLnk" runat="server" NavigateUrl='<%#"~/product/editproduct.aspx?sku=" + Eval("sku") %>' ToolTip="Edit this product listing">
<asp:Label runat="server" ID="lblTitle"><%#Eval("title") %></asp:Label>
</asp:HyperLink>
</td>
<td><asp:Label runat="server" ID="lblSku"><%#Eval("sku") %></asp:Label></td>
<td><asp:Label runat="server" ID="lblTWE">£<%#Eval("twePrice") %></asp:Label></td>
<td class="amzprice-edit"><input type="text" id='<%#"txt" + Eval("sku") %>' value='<%#Eval("amzPrice") %>'> </td></td>
<td>
<asp:Label runat="server" ID="lblLow">£<%#Eval("lowprice")%></asp:Label>
<img src="images/icons/graph-icon.png" alt="Info" title="View Lowprice History" />
</td>
<td><div class="twe-button mini update">Update</div></td>
</tr>
</ItemTemplate>
</asp:ListView>
The simplest way to do this is to add an additional property to your object/data source and then use that in the databinding stage.
For example, create a boolean property called LowestPrice
You can then evaluate this when databinding:
class='<%# Eval("LowestPrice") == true ? "BuyMeNow" : "BuyMeLater" %>'
Your example above would become something like:
<ItemTemplate>
<tr id='<%#Eval("sku") %>' class='<%# Eval("LowestPrice") == true ? "BuyMeNow" : "BuyMeLater" %>'>
<td class="t-left" width="360px">
<asp:HyperLink ID="prodLnk" runat="server" NavigateUrl='<%#"~/product/editproduct.aspx?sku=" + Eval("sku") %>' ToolTip="Edit this product listing">
<asp:Label runat="server" ID="lblTitle"><%#Eval("title") %></asp:Label>
</asp:HyperLink>
</td>
<td><asp:Label runat="server" ID="lblSku"><%#Eval("sku") %></asp:Label></td>
<td><asp:Label runat="server" ID="lblTWE">£<%#Eval("twePrice") %></asp:Label></td>
<td class="amzprice-edit"><input type="text" id='<%#"txt" + Eval("sku") %>' value='<%#Eval("amzPrice") %>'> </td></td>
<td>
<asp:Label runat="server" ID="lblLow">£<%#Eval("lowprice")%></asp:Label>
<img src="images/icons/graph-icon.png" alt="Info" title="View Lowprice History" />
</td>
<td><div class="twe-button mini update">Update</div></td>
</tr>
</ItemTemplate>
Which would apply the class BuyMeNow to the row when LowestPrice is true.

how to show header row either listview has data or not?

I have a list view and i want to add a header row in it which will come when the listview has data or not. in each and every condition i want it there. I have used LayOut Template to do so but its showing the Header row only if the listview has data.Then i tried EmptydataTemplate, Its showing the header row but now the button in it is not working.
My code is
<asp:UpdatePanel ID="upViewSchedule" runat="server">
<ContentTemplate>
<asp:ListView ID="lstuser" runat="server" ItemPlaceholderID="trItem" DataKeyNames="id"
OnItemDataBound="lstuser_ItemDataBound">
<LayoutTemplate>
<table cellspacing="0">
<tr class="hdrRowColor1">
<td align="left">
name
</td>
<td align="left">
salary
</td>
<td align="left">
address
</td>
<td align="left" style="border-right: 1px solid #6398cc">
Actions
</td>
</tr>
<tr class="OddRowColor">
<td align="left">
<asp:DropDownList ID="drphdrname" runat="server" Width="120px">
</asp:DropDownList>
<ajaxCtrl:CascadingDropDown ID="csddrphdrName" runat="server" TargetControlID="drpname"
Category="Name" ServicePath="~/Resources/WebService.asmx" ServiceMethod="GetName">
</ajaxCtrl:CascadingDropDown>
</td>
<td>
<asp:TextBox ID="txtaddress" runat="server" Width="110px"></asp:TextBox>
</td>
<td width="50px" class="last">
<asp:ImageButton ID="imgBtnHdrAdd" runat="server" ImageUrl="~/App_Themes/ThemeNew/Images/add_new.png"
OnClick="imgBtnHdrAdd_Click" ToolTip="Add user." />
</td>
</tr>
<tr id="trItem" runat="server">
</tr>
</table>
</LayoutTemplate>
<ItemTemplate>
<tr class='<%# Convert.ToBoolean(Container.DataItemIndex % 2) ? "OddRowColor" : "EvenRowColor" %>'>
<td align="left" width="138px">
<asp:DropDownList ID="drpname" runat="server" Width="120px">
</asp:DropDownList>
<asp:Label ID="lblCreatedBY" Text='<%# Eval("CreatedBy") %>' runat="server" Visible="false"></asp:Label>
<asp:Label ID="lblId" runat="server" Visible="false"></asp:Label>
<ajaxCtrl:CascadingDropDown ID="casddrpContacts" runat="server" TargetControlID="drpname"
Category="name" ServicePath="~/Resources/WebService.asmx" ServiceMethod="Getuser">
</ajaxCtrl:CascadingDropDown>
</td>
<td align="left" width="138px">
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Eval("salary") %>' Width="110px"></asp:TextBox>
</td>
<td align="left" width="138px">
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Eval("address") %>' Width="110px"></asp:TextBox>
</td>
<td align="left" class="last">
<asp:ImageButton ID="imgBtnEdit" runat="server" ImageUrl="~/App_Themes/ThemeNew2/images/update.png"
CommandArgument='<%# Eval("id") %>' OnClick="imgBtnEdit_Click" ToolTip="Update user" />
<asp:ImageButton ID="imgBtnDelete" runat="server" ImageUrl="~/App_Themes/ThemeNew/Images/delete.png"
CommandArgument='<%# Eval("id") %>' OnClientClick="return confirm('Are you sure you want to delete this user ?');"
ToolTip="Delete user" OnClick="imgBtnDelete_OnClick" />
</td>
</tr>
</ItemTemplate>
<EmptyDataTemplate>
<table cellspacing="0">
<tr class="hdrRowColor1">
<td align="left">
name
</td>
<td align="left">
salary
</td>
<td align="left">
address
</td>
<td align="left" style="border-right: 1px solid #6398cc">
Actions
</td>
</tr>
<tr class="OddRowColor">
<td align="left">
<asp:DropDownList ID="drphdrname" runat="server" Width="120px">
</asp:DropDownList>
<ajaxCtrl:CascadingDropDown ID="csddrphdrName" runat="server" TargetControlID="drpname"
Category="Name" ServicePath="~/Resources/WebService.asmx" ServiceMethod="GetName">
</ajaxCtrl:CascadingDropDown>
</td>
<td>
<asp:TextBox ID="txtaddress" runat="server" Width="110px"></asp:TextBox>
</td>
<td width="50px" class="last">
<asp:ImageButton ID="imgBtnHdrAdd" runat="server" ImageUrl="~/App_Themes/ThemeNew/Images/add_new.png"
OnClick="imgBtnHdrAdd_Click" ToolTip="Add user." />
</td>
</tr>
<tr id="trItem" runat="server">
</tr>
</table>
</EmptyDataTemplate>
</asp:ListView>
</ContentTemplate>
</asp:UpdatePanel>
my Code behind code is
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BIndAddress();
// BindRoleDrop();
drpAddress.Items.Insert(0, new ListItem("Select Area", ""));
drpRoom.Items.Insert(0, new ListItem("Select Room", ""));
ViewState["sortCol"] = "tblUser.id";
ViewState["sortDir"] = "Desc";
ViewState["nmbr"] = 1;
BindData(ViewState["sortCol"].ToString(), ViewState["sortDir"].ToString(), Convert.ToInt32(ViewState["nmbr"]), 7999);
}
else
{
lblMessage.Visible = false;
}
}
And one thing more please let me know how can i apply validations on these.
I did some testing locally. I found that if I have a button in the EmptyDataTemplate it's OnClick event will fire as expected as long as you do not modify the state of the ListView before the event fires.
I'm guessing therefore that you have some code that modifies the state (re-binds it for instance) of the ListView which runs before the imgBtnHdrAdd_Click event handler for the button in question.
The Page_Load method would have been the primary suspect, but based on your code it is properly checking for !IsPostBack.
I'm assuming that it is the call to BindData that is used for setting databinding the ListView. Look for other places in you code behind where that method is called and verify that none of those places are being run before the event handler for the button.

Connect Text Box to Table

I'm new in ASP.net and started my first Website Project. I want to connect the text box to my table which is "Details" please see the code below:
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<p class="style2">
<br />
DETAILS FORMS</p>
<p>
<table style="width: 100%; height: 117px;">
<tr>
<td class="style4">
<asp:Label ID="Label1" runat="server" Text="ID :"></asp:Label>
</td>
<td class="style3">
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</td>
<td class="style3">
</td>
</tr>
<tr>
<td class="style5">
<asp:Label ID="Label2" runat="server" Text="Date Recieved :"></asp:Label>
</td>
<td>
<asp:TextBox ID="DRInput" runat="server"></asp:TextBox>
</td>
<td>
</td>
</tr>
<tr>
<td class="style5">
<asp:Label ID="Label3" runat="server" Text="Date Commision :"></asp:Label>
</td>
<td>
<asp:TextBox ID="DCINput" runat="server"></asp:TextBox>
</td>
<td>
</td>
</tr>
<tr>
<td class="style5">
<asp:Label ID="Label4" runat="server" Text="Sender"></asp:Label>
</td>
<td>
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True">
<asp:ListItem>Angeles</asp:ListItem>
<asp:ListItem>Aurora</asp:ListItem>
<asp:ListItem>Bataan</asp:ListItem>
<asp:ListItem>Bulacan</asp:ListItem>
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:Database1ConnectionString1 %>"
SelectCommand="SELECT [Sender] FROM [Details]"></asp:SqlDataSource>
</td>
<td>
</td>
</tr>
<tr>
<td class="style10">
</td>
<td class="style11">
<asp:Button ID="Button1" runat="server" Text="Button" />
</td>
<td class="style11">
</td>
</tr>
<tr>
<td class="style6">
</td>
<td class="style7">
</td>
<td class="style7">
</td>
</tr>
</table>
</p>
</asp:Content>
And this is the Code behind it:
public partial class Data : System.Web.UI.Page
{
protected void DRInput_DataBinding(object sender, EventArgs e)
{
}
What would be my code to bind the textbox to save details in my table "Details" and when I click the button it will be save on table "Details" . Hoping anyone could help. Thanks in advance.
In you particular case it doesn't look like you're displaying a list of data but instead the details of an item.
The simple approach would be to assign the text property of the textbox as part of the page request eg within page load event of the page.
DRInput.Text = detailsModel.DRInput;
and then when the page is submitted assign the textbox's text value back to your object.
detailsModel.DRInput = DRInput.Text;
Since you're new to ASP.Net; perhaps best to start with tutorial sites for example w3schools.com/aspnet/aspnet_demo_intro.asp or asp.net/web-forms then come back and ask your questions

How to set radio button Id property based on database value?

I have grid view which contains five radio buttons per row. Out of these five only one can be checked at a time. To achieve this I tried to keep Id property of radio button corresponding to database value. But it gives me an error in Id field. .Net does not allow to use eval function in ID property. The rows will depend on number of records in database. I want to keep 5 radio button in database and out of these five, only one can be checked. How to achieve this task?
//My code of .aspx page is as below.
<%# Page Title="Applicant Rating Form" Language="C#" MasterPageFile="~/Interviewer/interviewerMasterPage.master" AutoEventWireup="true" CodeFile="AppRatingForm.aspx.cs" Inherits="Interviewer_AppRatingForm" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<table cellpadding="2" width="100%">
<tr>
<td colspan="2">
</td>
</tr>
<tr>
<td align="center" class="tdtitle" colspan="2">
Interview Rating Form</td>
</tr>
<tr>
<td colspan="2">
</td>
</tr>
<tr>
<td width="25%">
Name of Applicant</td>
<td>
<asp:Label ID="nameLbl" runat="server" Text="Label"></asp:Label>
</td>
</tr>
<tr>
<td>
Post Applied For</td>
<td>
</td>
</tr>
<tr>
<td>
Department</td>
<td>
</td>
</tr>
<tr>
<td colspan="2">
</td>
</tr>
<tr>
<td align="center" class="tdtitle" colspan="2">
Assessment</td>
</tr>
<tr>
<td align="left" colspan="2">
<asp:GridView ID="assesmentGrid" runat="server" AutoGenerateColumns="False"
onpageindexchanging="assesmentGrid_PageIndexChanging">
<Columns>
<asp:BoundField HeaderText="ID" DataField="SecId"/>
<asp:BoundField HeaderText="Traits" DataField="SecName"/>
<asp:TemplateField>
<HeaderTemplate>
A
</HeaderTemplate>
<ItemTemplate>
<asp:RadioButton ID='<%# Eval("SecId") %>' runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>
B
</HeaderTemplate>
<ItemTemplate>
<asp:RadioButton ID='<%# Eval("SecId") %>' runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>
C
</HeaderTemplate>
<ItemTemplate>
<asp:RadioButton ID='<%# Eval("SecId") %>' runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>
D
</HeaderTemplate>
<ItemTemplate>
<asp:RadioButton ID='<%# Eval("SecId") %>' runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>
E
</HeaderTemplate>
<ItemTemplate>
<asp:RadioButton ID='<%# Eval("SecId") %>' runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>
Comments
</HeaderTemplate>
<ItemTemplate>
<asp:TextBox ID="commentBox" runat="server" TextMode="MultiLine"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</td>
</tr>
<tr>
<td align="center" class="tdtitle" colspan="2">
</td>
</tr>
<tr>
<td align="center" colspan="2">
</td>
</tr>
<tr>
<td align="center" colspan="2">
</td>
</tr>
</table>
</asp:Content>
You need to set the GroupName property of all RadioButtons in the same group/row. This will only allow one to be selected at a time. I wouldn't suggest trying to set the IDs dynamically using Eval, just make sure they are all unique and it should work fine.

Resources