Remove row in a datatable using listview itemdeleting event - c#

I want to delete a row in the data table when the user clicked the button with the item deleting event. Am I missing something? When i click the Linkbutton lnkdelete, the row is still there
aspx code:
<table class="table table-bordered table-hover table-responsive">
<thead>
<tr>
<th>Product ID</th>
<th>Product Name</th>
<th>Quantity</th>
<th></th>
</tr>
</thead>
<tbody>
<asp:ListView ID="lvSODetails" runat="server"
onitemdeleting="lvSODetails_ItemDeleting" >
<ItemTemplate>
<tr>
<td><asp:Label ID="lblID" runat="server" Text='<%# Eval("ProductID") %>' /></td>
<td><%# Eval("ProductName") %></td>
<td><%# Eval("Quantity") %></td>
<td>
<asp:LinkButton ID="lnkDelete" runat="server"
class="glyphicon glyphicon-remove" CommandName="Remove" />
</td>
</tr>
</ItemTemplate>
<EmptyDataTemplate>
<tr>
<td colspan="4"><h5><center>Select a product first then click "Add Product".</center></h5></td>
</tr>
</EmptyDataTemplate>
</asp:ListView>
</tbody>
</table>
c#
void GetSODetails()
{
lvSODetails.DataSource = SODetails;
lvSODetails.DataBind();
}
protected void lvSODetails_ItemDeleting(object sender, ListViewDeleteEventArgs e)
{
SODetails = (DataTable)Session["sodetails"];
string lblID = (lvSODetails.Items[e.ItemIndex].FindControl("lblID") as Label).Text;
DataRow[] drr = SODetails.Select("ProductID=' " + lblID + " ' ");
foreach (var row in drr)
row.Delete();
SODetails.AcceptChanges();
GetSODetails();
}

I think you're using the wrong property on your LinkButton for your situation.
Remove all the command-related stuff and just subscribe to the event you already have.
<asp:LinkButton ID="lnkDelete"
runat="server"
class="glyphicon glyphicon-remove"
OnItemDeleting="lvSODetails_ItemDeleting" />

Related

How get data from database column as a heading using Repeater in ASP.NET

image
The Upper portion of present my database table structure and lower portion that in which form i want to show data.
<table class="table table-bordered table-hover">
<thead>
<tr class="text-center">
<th>Course Code</th>
<th>Subject</th>
<th>Cr.Hours</th>
<th>Grade</th>
</tr>
<tr>
<td colspan="4">
<asp:Label ID="Label5" runat="server" Text="Label"></asp:Label>
</td>
</tr>
</thead>
<tbody>
<asp:Repeater ID="outer" runat="server">
<ItemTemplate>
<tr>
<td>
<asp:Label Text='<%#Eval("[SpringFall]")%>' Style="text-align: center; display: none; font-size: 18px; font-family: 'Times New Roman'" ID="Label4" runat="server" Font-Bold="true"> </asp:Label>
</td>
<td>
<asp:Label ID="AllCcode" runat="server" Text='<%#Eval("[Course_Code]") %>'></asp:Label>
</td>
<td>
<asp:Label ID="AllSubject" runat="server" Text='<%#Eval("[Subject]") %>'></asp:Label>
</td>
<td>
<asp:Label ID="AllCrHr" runat="server" Text='<%#Eval("[Credit_Hours]") %>'></asp:Label>
</td>
<td>
<asp:Label ID="AllGrade" Font-Bold="true" runat="server" Text='<%#Eval("[Total_Marks]") %>'></asp:Label>
</td>
</tr>
</ItemTemplate>
</asp:Repeater>
</tbody>
</table>
Here Is my code..bind repeater on button click event using c# with simple sql SELECT query.Any query for bind repeater with more efficient working and show data in given form.
protected void AttendencBtn_Click(object sender, EventArgs e)
{
// MultiView1.ActiveViewIndex = 8;
conn.Open();
sda = new SqlDataAdapter("SELECT * FROM Attendence where Roll_Number='" + Session["RollNumb"] + "' ", conn);
dt = new DataTable();
sda.Fill(dt);
AttendencRpt.DataSource = dt;
AttendencRpt.DataBind();
conn.Close();
}
I think you are looking for something like this. First create a public variable in code behind.
public int currentYear = 2000;
protected void Page_Load(object sender, EventArgs e)
{
}
Then change the Repeater to something like this:
<table border="1">
<asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate>
<%# Convert.ToDateTime(Eval("myYear")).Year != currentYear && Container.ItemIndex > 0 ? "<tr><td colspan=\"5\">" + Eval("myYear") + "</td></tr>" : "" %>
<tr>
<td>
normal rows here
</td>
</tr>
<%# currentYear = Convert.ToDateTime(Eval("myYear")).Year %>
</ItemTemplate>
</asp:Repeater>
</table>
What will happen is that the value of currentYear is compared to the current row value. If it does not match a new row will be created. Then the value of currentYear is updated to be checked in the next row bound to the Repeater.

How to get the selected DDL value from a row in ListView

right now I am just trying to put together a simple role assigning pages in ASP.NET, I have created a listview, and in my item template I have a drop down list, with a button in the same template. The DDL is hooked up to an ODS which gets me the current roles, and I have a method which will take the role and the username, and assign that user to a specific role. My code is as follows:
<h2>Users</h2>
<asp:ListView ID="UserListView" runat="server"
ItemType="Synergy_System.Entities.Security.ApplicationUser"
OnItemCommand="UserListView_ItemCommand">
<EmptyDataTemplate>
<table runat="server">
<tr>
<td>
No users in this site.
<asp:LinkButton runat="server" CommandName="AddUsers" Text="Add users" ID="AddUsersButton" />
</td>
</tr>
</table>
</EmptyDataTemplate>
<ItemTemplate>
<tr>
<td>
<asp:Label Text='<%# Item.UserName %>' runat="server" ID="UserNameLabel" /></td>
<td>
<asp:Label Text='<%# Item.Email %>' runat="server" ID="EmailLabel" /></td>
<td><em>password is hashed</em></td>
<td>
<asp:DropDownList ID="RolesList" runat="server" DataSourceID="RoleListODS" DataTextField="Name" DataValueField="Name">
</asp:DropDownList>
<asp:Button ID="AddRoleToUser" runat="server" Text="Add Role" onclick="AddRoleToUser_Click" />
</td>
</tr>
</ItemTemplate>
<LayoutTemplate>
<table runat="server">
<tr runat="server">
<td runat="server">
<table runat="server" id="itemPlaceholderContainer"
class="table table-condensed table-hover table-striped">
<tr runat="server">
<th runat="server">User Name</th>
<th runat="server">Email</th>
<th runat="server">Password</th>
<th runat="server">Roles</th>
</tr>
<tr runat="server" id="itemPlaceholder"></tr>
</table>
</td>
</tr>
<tr runat="server">
<td runat="server">
<asp:DataPager runat="server" ID="DataPager1">
<Fields>
<asp:NextPreviousPagerField ButtonType="Button" ShowFirstPageButton="True" ShowLastPageButton="True"></asp:NextPreviousPagerField>
</Fields>
</asp:DataPager>
</td>
</tr>
</table>
</LayoutTemplate>
I have code behind pretty much empty, as it's just like this right now:
protected void AddRoleToUser_Click(object sender, EventArgs e)
{
string username;
string role;
//get username
//get value from DDL
UserManager.AddUserToRole(username,role);
}
I have used GridViews before but haven't dealt with ListViews much. I just want it so that when I click the button in that particular row, i retrieve this information, but there is no .Row in a listview.... any help would be greatly appreciated.
You can Use CommandName property of Button
<asp:Button ID="AddRoleToUser" CommandName="AddRole"
Code Behind
protected void UserListView_ItemCommand(object sender, ListViewCommandEventArgs e)
{
if (String.Equals(e.CommandName, "AddRole"))
{
ListViewDataItem dataItem = (ListViewDataItem)e.Item;
// this will gives you Row for your list items
Label UserNameLabel=(Label)dataItem.FindControl("UserNameLabel");
// you can find any control on row like this
string YourNameLabel=UserNameLabel.Text;
}
}

customise gridview display when bound to a datasource

I want to display the values in a gridview in this way(image),how can i perform it,i have no idea,if i have to edit the column or add template fields.,please help.
If we add,a footer,it can be displayed only on the last row,but,how to make it be displayed in the center.
You would be better off using something like a Repeater or DataList control, which give you more control over the output.
<asp:GridView runat="server" ID="gdv" AutoGenerateColumns="false" Width="100%">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<table width="100%">
<tr>
<td>
Exam Date
</td>
<td>
<%#Eval("Exam_Date") %>
</td>
<td>
Section
</td>
<td>
<%#Eval("Section") %>
</td>
</tr>
<tr>
<td>
Total Students
</td>
<td>
<%#Eval("Total_Students") %>
</td>
<td>
No. of students passed
</td>
<td>
<%#Eval("StudentPassed") %>
</td>
</tr>
<tr>
<td colspan="2">
over all pass percentange
</td>
<td colspan="2">
<%#Eval("Overall_Percent") %>
</td>
</tr>
</table>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
On cs page:
gdv.DataSource = YourDataSource;
gdv.DataBind();
I guess you need to do a column spanning to have this look. (I am assuming you have all the data needed to bind to the control.)
You can use Gridview Footer for this display .
<asp:TemplateField>
<FooterTemplate>
<table>
<tr>
<td>
<asp:Label ID="lblname" runat="server" Text="NAME"></asp:Label>
</td>
<td>
<asp:TextBox ID="txtbx" runat="server"></asp:TextBox>
</td>
</tr>
</table>
</FooterTemplate>
</asp:TemplateField>
On RowDataBound Event you can set your total to label .
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Footer)
{
Label lbl = (Label)e.Row.FindControl("lblTotal");
lbl.Text = "Total";
}
}

asp repeater download file c#

i am creating a website, in asp.net and using C# my problem is i created a asp repeater which is working fine so i added a imagebutton to download the file that the repeater contains, but i cant get it to download that file, hope someone can help me out i have this already, i have tried a couple of ways but could get it to do what i wanted. here is my code.
ASP
<asp:Repeater ID="RepDetailsPost" runat="server" OnItemCommand="Save" >
<HeaderTemplate>
<table style="width:500px" align ="center" cellpadding="0" class="rounded_corners" >
<tr style="background-color:Red; color:White">
<td valign="top" bgcolor="CC0000" >
<b>Post</b>
</td>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr style="background-color:#333">
<td>
<table style="background-color:#222; width:500px" class="rounded_corners" >
<table>
<tr>
<td style="font-size:large">
Utilizador:
<asp:Label ID="lblSubject0" runat="server" Text='<%#Eval("nome_utilizador") %>'
Font-Size="small" Font-Bold="true"/>
</td>
</tr>
</table>
</td>
</tr>
<tr style="background-color:#333;Color:White">
<td align="right" >
Criado em:<asp:Label ID="lblDate0" runat="server" Font-Bold="true"
Text='<%#Eval("data") %>'/>
</td>
</tr>
<tr>
<td>
<table style="background-color:#222; width:500px">
<tr>
<td>Anexo:
<asp:Image ImageUrl=<%# string.Format("~/uploads/{0}",Eval("Nome"))%> runat="server" width=500px/>
</td>
</tr>
</table>
<table style="background-color:#222; width:500px">
<tr>
<td>Guardar:
<asp:ImageButton ID="save" ImageUrl=<%# string.Format("~/imagens/icones/save.png")%> runat="server" width=30px CommandName="save"
CommandArgument='<%# Eval("Nome") %>' />
\\\\\\\\\\\\\\\ this is what I tried on top /////////////////
</tr>
</table>
<table style="background-color:#222; width:500px" align=center>
<tr>
<td>Comentário: <br /><asp:Label ID="lblComment0" Font-Size="small" runat="server" Text='<%#Eval("descricao") %>' /></td>
</tr>
</table>
</td>
</tr>
<tr>
<td colspan="2"valign="top" bgcolor="CC0000">
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
C#
protected void Save(Object Sender, RepeaterCommandEventArgs e)
{
if (e.CommandName == "Save")
{
int index = Convert.ToInt32(e.CommandArgument);
string fName = "teste" ;
Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Disposition", "attachment;filename=" + fName);
Response.TransmitFile(Server.MapPath("~/uploadsadmin/" + fName));
Response.End();
}
}
Your method name in OnItemCommand = "Dwn" must match the method name in your code-behind. Rename your Save method to Dwn, or change your markup to match the Save method name.
Refactor the code like this:
if(e.CommandName == "Save")
{
int index = Convert.ToInt32(e.CommandArgument);
string fName = row.Cells[2].Text;
Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Disposition", "attachment;filename=" + fName);
Response.TransmitFile(Server.MapPath("~/uploadsadmin/" + fName));
Response.End();
}
Put your breakpoint on the conditional (if...) and see if the breakpoint is hit.

How to find the data key on CheckedChanged event of checkbox in ListView in ASP.NET?

I am using a list view inside that in item template i am using a label and a checkbox.
I want that whenever user clicks on the check box the value should be updated in a table.i am using a datakeys in listview.on the basis of datakey value should be updated in the table. Query is:
string updateQuery = "UPDATE [TABLE] SET [COLUMN] = " + Convert.ToInt32(chk.Checked) + " WHERE PK_ID =" + dataKey + " ";`
also i want some help in displaying the result as it is inside the table.means if the value for column in table for a particular pkid is 1 then the checkbox shoul be checked.
Here is the code snippet:
<asp:ListView ID="lvFocusArea" runat="server" DataKeyNames="PK_ID" OnItemDataBound="lvFocusArea_ItemDataBound">
<LayoutTemplate>
<table border="0" cellpadding="1" width="400px">
<tr style="background-color: #E5E5FE">
<th align="left">
Focus Area
</th>
<th>
Is Current Focused
</th>
</tr>
<tr id="itemPlaceholder" runat="server">
</tr>
</table>
</LayoutTemplate>
<ItemTemplate>
<tr>
<td width="80%">
<asp:Label ID="lblFocusArea" runat="server" Text=""><%#Eval("FOCUS_AREA_NAME") %></asp:Label>
</td>
<td align="center" width="20%">
<asp:CheckBox ID="chkFocusArea" runat="server" OnCheckedChanged="chkFocusArea_CheckedChanged" AutoPostBack="true" />
</td>
</tr>
</ItemTemplate>
<AlternatingItemTemplate>
<tr style="background-color: #EFEFEF">
<td>
<asp:Label ID="lblFocusArea" runat="server" Text=""><%#Eval("FOCUS_AREA_NAME") %></asp:Label>
</td>
<td align="center">
<asp:CheckBox ID="chkFocusArea" runat="server" OnCheckedChanged="chkFocusArea_CheckedChanged" AutoPostBack="true" />
</td>
</tr>
</AlternatingItemTemplate>
<SelectedItemTemplate>
<td>
item selected
</td>
</SelectedItemTemplate>
</asp:ListView>
Help me.
Check this out : may help to resolve your issue of geting datakey
protected void chkFocusArea_CheckedChanged(object sender, EventArgs e)
{
CheckBox cb = (CheckBox)sender;
ListViewItem item = (ListViewItem)cb.NamingContainer;
ListViewDataItem dataItem = (ListViewDataItem)item ;
string code = ListView1.DataKeys[dataItem.DisplayIndex].Value.ToString();
}
Use Data Binding Expression
<asp:CheckBox ID="chkFocusArea" runat="server" Checked='<%# Eval("[COLUMN]") %>' oncheckedchanged="chkFocusArea_CheckedChanged" AutoPostBack="true" />
In your chkFocusArea_CheckedChanged event handler, perform your database insertion and rebind the data.

Categories