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.
Related
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.
There is a ListView which shows the data that were retrieved from the database.
I want to edit a single record on a ListView by clicking the edit button beside it but if I press the edit button, I get the error:
The ListView 'lvItemView' raised event ItemEditing which wasn't handled.
Here is the ListView:
<asp:ListView ID="lvItemView" runat="server" ItemPlaceholderID="itemHolder">
<LayoutTemplate>
<table style="color: Black;" width="100%" border="0" cellpadding="5">
<tr>
<th style="text-align: center;">Customer ID</th>
<th style="text-align: center;">Contact Name</th>
<th style="text-align: center;">Company</th>
<th style="text-align: center;">Created By</th>
<th style="text-align: center;">Created Date</th>
<th style="text-align: center;">Modified By</th>
<th style="text-align: center;">Modified Date</th>
</tr>
<asp:PlaceHolder ID="itemHolder" runat="server"></asp:PlaceHolder>
</table>
</LayoutTemplate>
<ItemTemplate>
<tr>
<td align="left">
<asp:Label ID="lblCustomerID" runat="Server" Text='<%#Eval("_CustomerID") %>' />
</td>
<td align="left">
<asp:Label ID="lblContactName" runat="Server" Text='<%#Eval("_ContactName") %>' />
</td>
<td align="left">
<asp:Label ID="lblCompany" runat="Server" Text='<%#Eval("_CompanyID") %>' />
</td>
<td align="left">
<asp:Label ID="lblCreatedBy" runat="Server" Text='<%#Eval("_CreatedBy") %>' />
</td>
<td align="left">
<asp:Label ID="lblCreatedDate" runat="Server" Text='<%#Eval("_CreatedDate") %>' />
</td>
<td align="left">
<asp:Label ID="lblModifiedBy" runat="Server" Text='<%#Eval("_ModifiedBy") %>' />
</td>
<td align="left">
<asp:Label ID="lblModifiedDate" runat="Server" Text='<%#Eval("_ModifiedDate") %>' />
</td>
<td align="left">
<asp:LinkButton ID="EditButton" runat="Server" Text="Edit" CommandName="Edit" CommandArgument='<%#DataBinder.Eval(Container, "DataItemIndex")%>' />
</td>
<td align="left">
<asp:LinkButton ID="Delete" runat="Server" Text="Delete" CommandName="Delete" CommandArgument='<%#DataBinder.Eval(Container, "DataItemIndex")%>' />
</td>
</tr>
</ItemTemplate>
</asp:ListView>
And the code behind
protected void lvItemView_ItemEditing(object sender, ListViewEditEventArgs e)
{
System.Web.UI.WebControls.Label index_id = (System.Web.UI.WebControls.Label)lvItemView.Items[e.NewEditIndex].FindControl("lblCustomerID");
int customerID = int.Parse(index_id.Text);
Item item = new Item();
item._CustomerID = customerID;
System.Web.UI.WebControls.Label cmp_id = (System.Web.UI.WebControls.Label)lvItemView.Items[e.NewEditIndex].FindControl("lblCompany");
int companyID = int.Parse(cmp_id.Text);
item._CompanyID = companyID;
System.Web.UI.WebControls.Label samp = (System.Web.UI.WebControls.Label)lvItemView.Items[e.NewEditIndex].FindControl("lblContactName");
item._ContactName = samp.Text;
samp = (System.Web.UI.WebControls.Label)lvItemView.Items[e.NewEditIndex].FindControl("lblCreatedBy");
item._CreatedBy = samp.Text;
samp = (System.Web.UI.WebControls.Label)lvItemView.Items[e.NewEditIndex].FindControl("lblCreatedDate");
item._CreatedDate = samp.Text;
samp = (System.Web.UI.WebControls.Label)lvItemView.Items[e.NewEditIndex].FindControl("lblModifiedBy");
item._ModifiedBy = samp.Text;
samp = (System.Web.UI.WebControls.Label)lvItemView.Items[e.NewEditIndex].FindControl("lblModifiedDate");
item._ModifiedDate = samp.Text;
CustomerID = item._CustomerID.ToString();
ContactName = item._ContactName.ToString();
CompanyID = item._CompanyID.ToString();
CreatedBy = item._CreatedBy.ToString();
CreatedDate = item._CreatedDate.ToString();
ModifiedBy = item._ModifiedBy.ToString();
ModifiedDate = item._ModifiedDate.ToString();
modAdd.Show();
}
I am new to asp.net and c# so I have no idea what to do with this kind of error.
set the event lvItemView_ItemEditing in OnItemEditing which will be triggered when click on edit button
<asp:ListView ID="lvItemView" runat="server" ItemPlaceholderID="itemHolder" OnItemEditing="lvItemView_ItemEditing">
and also set data source if you are binding on pageload
lvItemView.DataSource = SomeData;
lvItemView.DataBind();
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;
}
}
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" />
I’m currently working on an asp.net page using C#. This page contains a button and once you click you will get a small HTML table with a name of person, cell phone number and email address. What I want to do is in code behind capture this HTML table along with its data in memory stream or other type of streams in order to do some operations. Here's my code
<table id="tb" runat="server">
<tr>
<td> Name </td>
<td> <asp:Label ID="lblName" runat="server" ></asp:Label> </td>
</tr>
<tr>
<td> Phone </td>
<td> <asp:Label ID="lblPhone" runat="server" ></asp:Label> </td>
</tr>
<tr>
<td> Email </td>
<td> <asp:Label ID="lblEmail" runat="server" ></asp:Label> </td>
</tr>
</table>
So please, if anyone could help of how to accomplish this process and I will be so thankful
Well although I don't really understand why do you want to do that, you can do this pretty simply, you have to put id and runat server tag at your table, and you already have that,
and then render this control to string :
markup:
<form id="form1" runat="server">
<table id="tb" runat="server">
<tr>
<td> Name </td>
<td> <asp:Label ID="lblName" runat="server" ></asp:Label> </td>
</tr>
<tr>
<td> Phone </td>
<td> <asp:Label ID="lblPhone" runat="server" ></asp:Label> </td>
</tr>
<tr>
<td> Email </td>
<td> <asp:Label ID="lblEmail" runat="server" ></asp:Label> </td>
</tr>
</table>
<hr />
Rendered table :
<hr />
<asp:Label ID="lblRenderedTable" runat="server"></asp:Label>
<hr />
</form>
code behind:
protected void Page_Load(object sender, EventArgs e)
{
lblName.Text = "User Name";
lblEmail.Text = "user#domain.com";
lblPhone.Text = "555-4214";
StringBuilder sb = new StringBuilder();
StringWriter tw = new StringWriter(sb);
HtmlTextWriter hw = new HtmlTextWriter(tw);
tb.RenderControl(hw);
string tableContents = sb.ToString();
lblRenderedTable.Text = tableContents;
}