Listview is losing data on postback ASP.Net - asp.net

I am trying to update the whole list by once by using a button but in code behind I am losing the data of the list items it returns null although it has the items. Please help me in this manner. my code is below
aspx
<asp:ListView ID="ProjectsList" runat="server" DataKeyNames="ProjectId" DataSourceID="EntityDataSource1" ViewStateMode="Enabled">
<AlternatingItemTemplate>
<tr style="background-color: #FFF8DC;">
<td><asp:Label ID="ProjectIdLabel" runat="server" Text='<%# Eval("ProjectId") %>' /></td>
<td><asp:Label ID="SchoolIdLabel" runat="server" Text='<%# Eval("SchoolId") %>' /></td>
<td><asp:Label ID="FairIdLabel" runat="server" Text='<%# Eval("FairId") %>' /></td>
<td><asp:Label ID="CategoryIdLabel" runat="server" Text='<%# Eval("CategoryId") %>' /></td>
<td><asp:Label ID="TitleLabel" runat="server" Text='<%# Eval("Title") %>' /></td>
<td><asp:Label ID="BalanceLabel" runat="server" Text='<%# Eval("Balance") %>' /></td>
<td><asp:CheckBox ID="FinalistCheckBox" runat="server" Checked='<%# Eval("Finalist") %>' Enabled="true" /></td>
<td><asp:Label ID="ProjectStatusLabel" runat="server" Text='<%# Eval("ProjectStatus") %>' /></td>
</tr>
</AlternatingItemTemplate>
<ItemTemplate>
<tr style="background-color: #DCDCDC; color: #000000;">
<td><asp:Label ID="ProjectIdLabel" runat="server" Text='<%# Eval("ProjectId") %>' /></td>
<td>asp:Label ID="SchoolIdLabel" runat="server" Text='<%# Eval("SchoolId") %>' /></td>
<td><asp:Label ID="FairIdLabel" runat="server" Text='<%# Eval("FairId") %>' /></td>
<td><asp:Label ID="CategoryIdLabel" runat="server" Text='<%# Eval("CategoryId") %>' /></td>
<td><asp:Label ID="TitleLabel" runat="server" Text='<%# Eval("Title") %>' /></td>
<td><asp:Label ID="BalanceLabel" runat="server" Text='<%# Eval("Balance") %>' /></td>
<td><asp:CheckBox ID="FinalistCheckBox" runat="server" Checked='<%# Eval("Finalist") %>' Enabled="true" /></td>
<td><asp:Label ID="ProjectStatusLabel" runat="server" Text='<%# Eval("ProjectStatus") %>' /></td>
</tr>
</ItemTemplate>
<LayoutTemplate>
<table runat="server">
<tr runat="server">
<td runat="server">
<table id="itemPlaceholderContainer" runat="server" border="1" style="background-color: #FFFFFF; border-collapse: collapse; border-color: #999999; border-style: none; border-width: 1px; font-family: Verdana, Arial, Helvetica, sans-serif;">
<tr runat="server" style="background-color: #DCDCDC; color: #000000;">
<th runat="server">ProjectId</th>
<th id="Th1" runat="server">SchoolId</th>
<th id="Th2" runat="server">FairId</th>
<th id="Th3" runat="server">CategoryId</th>
<th runat="server">Title</th>
<th id="Th4" runat="server">Balance</th>
<th runat="server">Finalist</th>
<th runat="server">ProjectStatus</th>
</tr>
<tr id="itemPlaceholder" runat="server">
</tr>
</table>
</td>
</tr>
<tr runat="server">
<td runat="server" style="text-align: center; background-color: #CCCCCC; font-family: Verdana, Arial, Helvetica, sans-serif; color: #000000;"></td>
</tr>
</table>
</LayoutTemplate>
</asp:ListView>
<asp:EntityDataSource ID="EntityDataSource1" runat="server" ConnectionString="name=MyEntity" DefaultContainerName="MyEntity" EnableFlattening="False" EnableUpdate="True" EntitySetName="Projects">
</asp:EntityDataSource>
<asp:Button ID="FinalistButton" runat="server" Text="Finalist" OnClick="FinalistButton_Click" CssClass="imageButtonStyle" />
and my aspx.cs...
protected void FinalistButton_Click(object sender, EventArgs e)
{
//here I can have all the items but there is no value
foreach (ListViewItem item in ProjectsList.Items)
{
if (item.ItemType == ListViewItemType.DataItem)
{
CheckBox final = (CheckBox)item.FindControl("Finalist");
TextBox proidText = (TextBox)item.FindControl("ProjectId");
int proId = Convert.ToInt32(proidText.Text);
if (final.Checked)
{
//some code
}
}
}
}

To insert or update data, the templates < InsertItemTemplate> and/or < EditItemTemplate> should be included, maybe they're missing. Templates < ItemTemplate> and < AlternateItemTemplate> are just used to show data. To change templates, add some command buttons. And you use Bind instead of Eval
You have a walkthrough at the following link, not very good, though. Maybe you can find a better example.
http://msdn.microsoft.com/en-us/library/bb515103.aspx

Related

aspx repeater Do something with the data

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>

Changing style conditionally on a ListView based on value in another column

I am trying to change the background color on a row if the absolute value of an added column in a table is less than 8. I have tried several different ways, but the latest attempt, although seemingly close, does not perform as I expected.
<asp:ListView ID="ListView2" runat="server" >
<LayoutTemplate>
<table id="Table2" style="border:solid" runat="server">
<tr id="Tr1" runat="server">
<td id="Td1" runat="server">
<table runat="server" id="itemPlaceholderContainer" border="0" >
<tr id="Tr2" runat="server" style='<%# Eval("Alert") %>'>
<th id="Th1" runat="server">Date</th>
<th id="Th2" runat="server">Home</th>
<th id="Th3" runat="server">Score</th>
<th id="Th4" runat="server">Away</th>
<th id="Th5" runat="server">Score</th>
<th id="Th6" runat="server">Quarter</th>
<th id="Th7" runat="server">Link To Game</th>
<th id="Th8" runat="server"></th>
</tr>
<tr runat="server" id="itemPlaceholder"></tr>
</table>
</td>
</tr>
<tr id="Tr3" runat="server">
<td id="Td2" runat="server" style=""></td>
</tr>
</table>
</LayoutTemplate>
<AlternatingItemTemplate>
<tr style="">
<td style="border:solid">
<asp:Label Text='<%# Eval("Date") %>' runat="server" ID="DateLabel2" /></td>
<td style="border:solid">
<asp:Label Text='<%# Eval("HighSchoolName") %>' runat="server" ID="HighSchoolNameLabel2" /></td>
<td style="border:solid">
<asp:Label Text='<%# Eval("HomeTeamScore") %>' runat="server" ID="HomeTeamScoreLabel2" /></td>
<td style="border:solid">
<asp:Label Text='<%# Eval("HighSchoolName1") %>' runat="server" ID="HighSchoolName1Label2" /></td>
<td style="border:solid">
<asp:Label Text='<%# Eval("AwayTeamScore") %>' runat="server" ID="AwayTeamScoreLabel2" /></td>
<td style="border:solid">
<asp:Label Text='<%# Eval("Quarter") %>' runat="server" ID="LabelQuarter" /></td>
<td style="border:solid" id="HomeAudioLink">
<a href="<%# Eval("AudioInternetLink") %>" target="_blank" >
<asp:Label Text='<%# Eval("AudioInternetLink") %>' runat="server" ID="LabelHomeAudioLink" />
</a>
</td>
<td style="border:solid" id="AwayAudioLink">
<a href="<%# Eval("AudioInternetLink1") %>" target="_blank" >
<asp:Label Text='<%# Eval("AudioInternetLink1") %>' runat="server" ID="LabelAwayAudioLink" />
</a>
</td>
</tr>
</AlternatingItemTemplate>
<EmptyDataTemplate>
<table id="Table1" runat="server" style="">
<tr>
<td>No data was returned.</td>
</tr>
</table>
</EmptyDataTemplate>
<ItemTemplate>
<tr id="Game" style="" runat="server" class="" >
<td style="border:solid">
<asp:Label Text='<%# Eval("Date") %>' runat="server" ID="DateLabel2" /></td>
<td style="border:solid">
<asp:Label Text='<%# Eval("HighSchoolName") %>' runat="server" ID="HighSchoolNameLabel2" /></td>
<td style="border:solid">
<asp:Label Text='<%# Eval("HomeTeamScore") %>' runat="server" ID="HomeTeamScoreLabel2" /></td>
<td style="border:solid">
<asp:Label Text='<%# Eval("HighSchoolName1") %>' runat="server" ID="HighSchoolName1Label2" /></td>
<td style="border:solid">
<asp:Label Text='<%# Eval("AwayTeamScore") %>' runat="server" ID="AwayTeamScoreLabel2" /></td>
<td style="border:solid">
<asp:Label Text='<%# Eval("Quarter") %>' runat="server" ID="LabelQuarter" /></td>
<td style="border:solid" id="HomeAudioLink">
<a href="<%# Eval("AudioInternetLink") %>" target="_blank" >
<asp:Label Text='<%# Eval("AudioInternetLink") %>' runat="server" ID="LabelHomeAudioLink" />
</a>
</td>
<td style="border:solid" id="AwayAudioLink">
<a href="<%# Eval("AudioInternetLink1") %>" target="_blank" >
<asp:Label Text='<%# Eval("AudioInternetLink1") %>' runat="server" ID="LabelAwayAudioLink" />
</a>
</td>
</tr>
</ItemTemplate>
</asp:ListView>
And the C# code:
protected void ScheduleButton_Click(object sender, EventArgs e)
{
/**********************************************************************/
/* The code below will initialize the connection to the database. */
/* As the connection string to the SQL database is defined as conn, */
/* the open method from conn will connect to the database, and the */
/* cmd variable will call on the stored procedure GetSchedule. */
/**********************************************************************/
string strcon = WebConfigurationManager.ConnectionStrings["FollowingHSFootballConnectionString"].ConnectionString;
using (SqlConnection conn = new SqlConnection(strcon))
{
SqlCommand cmd = new SqlCommand("CurrentSchedScore", conn);
cmd.CommandType = CommandType.StoredProcedure;
conn.Open();
DataTable ScheduleTbl = new DataTable();
SqlDataAdapter da = new SqlDataAdapter(cmd);
// Fill the partial DataSet into a partial DataTable.
da.Fill(ScheduleTbl);
ListView2.DataSource = ScheduleTbl;
ListView2.DataBind();
// Close Game Logic
ScheduleTbl.Columns.Add("Diff", typeof(int), "HomeTeamScore - AwayTeamScore");
ScheduleTbl.Columns.Add("Alert", typeof(string), "");
for (int i = 0; i < ScheduleTbl.Rows.Count; i++)
{
DataRow drST = ScheduleTbl.Rows[i];
if (Math.Abs((int)drST.ItemArray.GetValue(8)) <= 8)
{
drST.BeginEdit();
drST.ItemArray.SetValue("background-color:red", 9);
drST.AcceptChanges();
drST.EndEdit();
}
}
}
}
Where did I misunderstand how to update the last column?
I found that I had to use a javascript solution that worked.
#Class = ClassSchedDropDown.SelectedValue;
if (RegionSchedDropDown.SelectedValue == "")
{
for (int i = 1; i <= 8; i++)
{
SchedScoreDirect = "CurrentSchedScoreClassReg";
#Reg = i;
}
}
The SchedScoreDirect = "CurrentSchedScoreClassReg code will direct processing to send the red background to the appropriate row.

Listview Layout Template Error

I am querying a column from my db and when I test the query with the sqldatasource the numbers shows up fine. However, when I run the application, all the numbers in the column add two zeros at the end of it.
For example,
Instead of 1 it is 100
Instead of 2 it is 200
I dont know what the cause of this may be.
Here is my LayoutTemplate:
<LayoutTemplate>
<table runat="server">
<tr runat="server">
<td runat="server">
<table id="itemPlaceholderContainer" runat="server" border="1" style="background-color: #FFFFFF;border-collapse: collapse;border-color: #999999;border-style:none;border-width:1px;font-family: Verdana, Arial, Helvetica, sans-serif;">
<tr runat="server" style="background-color: #E0FFFF;color: #333333; ">
<th runat="server"></th>
<th runat="server"> FormTitle</th>
<th runat="server">FormSection</th>
<th runat="server">SubSection</th>
<th runat="server">SectionItem</th>
<th runat="server">SortOrder</th>
</tr>
<tr id="itemPlaceholder" runat="server">
</tr>
</table>
</td>
</tr>
<tr runat="server">
<td runat="server" style="text-align: center;background-color: #5D7B9D;font-family: Verdana, Arial, Helvetica, sans-serif;color: #FFFFFF">
<asp:DataPager ID="DataPager1" runat="server" PageSize="7">
<Fields>
<asp:NextPreviousPagerField ButtonType="Button" ShowFirstPageButton="True" ShowLastPageButton="True" />
</Fields>
</asp:DataPager>
</td>
</tr>
</table>
</LayoutTemplate>
ItemTemplate:
<ItemTemplate>
<tr style="background-color: #E0FFFF;color: #333333;">
<td>
<asp:Button ID="DeleteButton" runat="server" CommandName="Delete" Text="Delete" />
<asp:Button ID="EditButton" runat="server" CommandName="Edit" Text="Edit" />
</td>
<td>
<asp:Label ID="FormTitleLabel" runat="server" Text='<%# Eval("FormTitle") %>' />
</td>
<td>
<asp:Label ID="FormSectionLabel" runat="server" Text='<%# Eval("FormSection") %>' />
</td>
<td>
<asp:Label ID="SubSectionLabel" runat="server" Text='<%# Eval("SubSection") %>' />
</td>
<td>
<asp:Label ID="SectionItemLabel" runat="server" Text='<%# Eval("SectionItem") %>' />
</td>
<td>
<asp:Label ID="SortOrder" runat="server" Text='<%# Eval("SortOrder") %>' />
</td>
</tr>
</ItemTemplate>

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.

Get the id of parent listview in child listview checkbox change event

On chkSubModuleView_CheckedChanged() event iwant to get the id of chkModule.Please tell me how to get the id or reference of parent checkbox on click of child checkbox in server side.How to distinguish between different modules .
enter code here
<asp:ListView ID="lvModule" runat="server">
<LayoutTemplate>
<table width="600px" border="0" cellpadding="0" cellspacing="0"
class="ListViewtable">
<tr>
<th style="width: 10%;">
Modules
</th>
</tr>
<asp:PlaceHolder ID="itemPlaceholder" runat="server">
</asp:PlaceHolder>
</table>
</LayoutTemplate>
<ItemTemplate>
<tr>
<td>
<asp:CheckBox ID="chkModule" runat="server"
CausesValidation="false" AutoPostBack="true"
OnCheckedChanged="chkModule_CheckedChanged" >
</asp:CheckBox><asp:HiddenField ID="hfEntityName"
Value='<%# Eval("EntityName") %>' runat="server" />
<%# Eval("Title")%>
<asp:HiddenField ID="hfID" Value='<%# Eval("ID") %>'
runat="server" />
<asp:Label ID="label" runat="server"></asp:Label>
</td>
</tr>
<tr>
<td colspan="2" style="padding-left: 20px;">
<asp:ListView ID="lvSubModule" runat="server">
<LayoutTemplate>
<table width="100%" cellspacing="0" border="0"
class="ListViewtableLayer2">
<tr>
<th style="width: 20%;">
Sub Module
</th>
<th style="width: 20%;">
<asp:CheckBox ID="chkSubModuleView"
Checked="true" runat="server" AutoPostBack="true"
OnCheckedChanged="chkSubModuleView_CheckedChanged" />
View
</th>
</tr>
<asp:PlaceHolder ID="itemPlaceholder"
runat="server"></asp:PlaceHolder>
</table>
</LayoutTemplate>
<ItemTemplate>
<tr>
<td>
<%# Eval("Title")%>
<asp:HiddenField ID="hfMenuID" Value='<%#
Eval("MenuID") %>' runat="server" />
<asp:HiddenField ID="hfName" Value='<%#
Eval("HeaderID") %>' runat="server" />
</td>
<td>
<asp:CheckBox ID="chkRead" runat="server"
AutoPostBack="true" Checked="true" CausesValidation="false"
OnCheckedChanged="chkRead_CheckedChanged"></asp:CheckBox>
</td>
</tr>
</ItemTemplate>
</asp:ListView>
</td>
</tr>
</ItemTemplate>
</asp:ListView>
change your checkbox property to
<asp:CheckBox ID="chkSubModuleView"
Checked="true" runat="server" AutoPostBack="true" Key='<%# Eval("ID")%>'
OnCheckedChanged="chkSubModuleView_CheckedChanged" />
In your .cs
protected void chkSubModuleView_CheckedChanged(object sender, EventArgs e)
{
CheckBox chk = (CheckBox)sender;
string yourID = chk.Attributes["Key"].ToString();
}

Resources