I've got a repeater with an itemlist, it get's data via C#/sql and a databind.
I then want to add onclick javascript to my 'deleteNewsButton' in the itemlist.
Im guessing that i have to use OnItemDataBound?
Heres my repeater:
<asp:Repeater ID="newsListRepeater" runat="server" OnItemDataBound="deleteConfirm_Databound">
<ItemTemplate>
<tr>
<td><%# Eval("id") %></td>
<td><%# Eval("title") %></td>
<td><%# Eval("tags") %></td>
<td><%# Eval("author") %></td>
<td style="width:100px;"><%# DataBinder.Eval(Container.DataItem, "time", "{0:dd/MM/yyyy}") %></td>
<td style="width:110px;">
<asp:Button ID="editNewsButton" runat="server" OnCommand="editNewsButton_Click" CommandArgument='<%# Eval("id") %>' Text="Rediger" />
<asp:Button ID="deleteNewsButton" runat="server" CommandArgument='<%# Eval("id") %>' Text="Slet" />
</td>
</tr>
</ItemTemplate>
</asp:Repeater>
EDIT:
How would i bind a paramter from behind code, to my 'deleteNewsButton' controllers.
It needs to be done from behind code...
I believe for a repeater item databound event you need to use RepeaterItemEventArgs instead of RepeaterItem
protected void deleteConfirm_Databound(object sender, RepeaterItem e)
becomes
protected void deleteConfirm_Databound(object sender, RepeaterItemEventArgs e)
You might also want a null check for that control:
LinkButton button = (LinkButton)e.FindControl("deleteNewsButton");
if(button != null) {
button.Attributes.Add("onclick", "javascript:return " +
"confirm('Er du sikker på du vil slette: " + DataBinder.Eval(e.DataItem, "id") + "')");
}
You may also consider making a common javascript function for the confirm box, and then just calling it from your button.
aspx
<script type="text/javascript">
function confirmFunction(id)
{
return confirm('Er du sikker på du vil slette: ' + id);
}
</script>
codebehind
LinkButton button = (LinkButton)e.FindControl("deleteNewsButton");
if(button != null) {
button.Attributes.Add("onclick", "javascript:return confirmFunction(" + DataBinder.Eval(e.DataItem, "id") + ");");
}
add an onclientclick event eg.
<asp:Button ID="deleteNewsButton"
OnClientClick="return confirm('Do you really want to Delete this record ?')"
runat="server" CommandArgument='<%# Eval("id") %>' Text="Slet" />
Related
How to automatic store value in database when checkbox is checked and refresh my To do list. Here is my code:
This is my HTML:
<asp:Repeater ID="rpTodos" runat="server">
<ItemTemplate>
<tr>
<td><%# Eval("TodosID", "{0:d}") %></td>
<td><%# Eval("TaskName", "{0:d}") %></td>
<td><asp:CheckBox ID="cbisdone" runat="server" AutoPostBack="true" checked='<%#Eval("IsDone")%>'></asp:CheckBox></td>
<td><%# Eval("TaskDate", "{0:d}") %></td>
</tr>
</ItemTemplate>
</asp:Repeater>
This is code behind:
var todos2 = ctx.Todos.ToList().Where(t=> t.IsDone == false).ToList();
rpTodos.DataSource = todos2;
rpTodos.DataBind();
Add a OnCheckedChanged event in your aspx page.
<asp:CheckBox ID="cbisdone" runat="server" AutoPostBack="true" checked='<%#Eval("IsDone")%>' OnCheckedChanged="cbisdone_changed"></asp:CheckBox>
Create a new event that would listen to raised event and cast the sender object back to Checkbox.
protected void cbisdone_changed(object sender, EventArgs e)
{
if (sender != null)
{
if (((CheckBox)sender).Checked)
{
// Update the status in DB.
}
}
}
I am having problem with the LinkButton onclick event not firing.
I have checked the following posts and taken the precaution of Postback but still joy
repeater linkbutton not firing
Repeater's Item command event is not firing on linkbutton click
Here is my Code so far
<asp:PlaceHolder runat="server" ID="phOrders">
<asp:Repeater ID="rprOrders" runat="server" OnItemCommand="rprOrders_ItemCommand">
<HeaderTemplate>
<table>
<tr>
<th>
<asp:LinkButton ID="lnkOrderByDate" runat="server" Text="Date" CommandName="OrderDate" OnClick="lnkOrderByDate_Click"></asp:LinkButton></th>
<th>
<asp:LinkButton ID="lnkOrderByOrderNumber" runat="server" Text="Order Number"></asp:LinkButton></th>
<th>
<asp:LinkButton ID="lnkOrderByProductNumber" runat="server" Text="Product Number"></asp:LinkButton></th>
<th>Product Description</th>
<th>Size</th>
<th>QTY</th>
<th>Status</th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td><strong><%# Eval("OrderDate") %></strong></td>
<td><%# Eval("OrderNumber") %></td>
<td><%# Eval("SKUNumber") %></td>
<td><%# Eval("OrderItemSKUName") %></td>
<td><%# Eval("mtrx_Code2") %></td>
<td><%# Eval("OrderItemUnitCount") %></td>
<td><strong><%# Eval("OrderItemStatus") %></strong></td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
<div class="track-footer"></div>
</asp:PlaceHolder>
Code Behind
protected void SetupControl()
{
if (this.StopProcessing)
{
// Do not process
}
else
{
if (CMSContext.ViewMode == ViewModeEnum.LiveSite)
{
if(!Page.IsPostBack)
{
PopulateProductClass();
PopulateProduct();
PopulateDefaultViewOrders();
}
}
}
}
protected void lnkOrderByDate_Click(object sender, EventArgs e)
{
//Do Something
}
Any suggestions? I can't seem to figure it out?
Even the OnItemCommand="rprOrders_ItemCommand" wont fire either?
The LinkButton within your DataControl triggers the Method rprOrders_ItemCommand
Set a breakpoint there. If you have multiple LinkButton then you can read CommandName="OrderDate" Codebehind: (e.CommandName)
For passing values CommandArgument should be used.
use some thing like this
<asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" OnCommand="LinkButton1_Command" CommandName="MyUpdate" CommandArgument='<%# Eval("erid") %>'>LinkButton</asp:LinkButton>
</ItemTemplate>
.cs
protected void Repeater1_OnItemCommand(object source, RepeaterCommandEventArgs e)
{
if (e.CommandName.Equals("MyUpdate"))
{
// some code
}
if (e.CommandName.Equals("EditCategory"))
{
// some code
}
}
For me the fix was to set EnableViewState="true" the the control tag of my ascx file.
E.G.
<%# Control Language="C#" AutoEventWireup="True" CodeBehind="Settings.ascx.cs" Inherits="Foo.Bar.Settings" EnableViewState="true" %>
And my LinkButton looks like this
<asp:LinkButton ID="btnRemoveMedia"
runat="server"
class="icon mdi-delete"
OnCommand="btnRemoveMedia_OnCommand"
CommandArgument="<%# ((Tuple<int, string>) Container.DataItem).Item1 %>"
CommandName="Delete"
UseSubmitBehavior="false" />
I have a repeater to show an order confirmation. It is bound to a table generated with Entity Framework code first. The table contains the right information and the repeater shows all the information right except for the part where I show the totals which I paste below.
I believe that this part also works okay but quantity does not. For example if I add 5 products it will only show one, but if I put a break point and I run the code in debug mode I see that the value inserted in the Quantity table is 5 not 1, so the Quantity value on the table is inserted correctly, but the repeater reports 1.
Here is the code:
<asp:Repeater ID="rptConfirmOrder" runat="server">
<ItemTemplate>
<fieldset class = "OrderConfirmationFieldset"><legend class ="OrderDataLegend">Order Summary</legend>
<td align="left" width="60%" runat="server" id="Td25">
<asp:Label ID="lblQuantity" runat="server" Text="Quantity: " CssClass = "lblOrderConfirmation">
</asp:Label> <%# Eval("Quantity") %>
<br />
</td>
<td align="left" width="60%" runat="server" id="Td26">
<asp:Label ID="lblProductName" runat="server" Text="Product Name: " CssClass = "lblOrderConfirmation">
</asp:Label><%# Eval("ProductName" ,"{0:c}" ) %>
<br />
</td>
<td align="left" width="60%" runat="server" id="Td27">
<asp:Label ID="lblProductPrice" runat="server" Text="Product price: " CssClass = "lblOrderConfirmation">
</asp:Label> <%# Eval("ProductPrice" ,"{0:c}" ) %>
<br />
</td>
<td align="left" width="60%" runat="server" id="Td28">
<asp:Label ID="lblSubtotal" runat="server" Text="Subtotal: " CssClass = "lblOrderConfirmation">
</asp:Label> <%# Eval("Subtotal" ,"{0:c}" ) %>
<br />
</td>
<td align="left" width="60%" runat="server" id="Td29">
<asp:Label ID="lblTotal" runat="server" Text="Total: " CssClass = "lblOrderConfirmation">
</asp:Label> <%# Eval("Total" ,"{0:c}" ) %>
<br />
</td>
</ItemTemplate>
</asp:Repeater>
Can Anyone help?
Thank you in advance!
Your repeater markup doesn't look right.
have it as follows:
<asp:Repeater ID="rptConfirmOrder" runat="server" OnItemDataBound="rptConfirmOrder_ItemDataBound">
<ItemTemplate>
// stuff
</ItemTemplate>
</asp:Repeater>
And if you have it that way, and this was a paste error, then forget the value in the database/entity etc.
You can verify accurately, what value is being bound by tapping into the row data bound event as follows..
protected void rptConfirmOrder_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
var dataItem = e.Item.DataItem as YOUR_ENTITY_TYPE;
Debug.Assert(5 == dataItem.Quantity);
}
}
I think you may need to add the < itemtemplate > tags in after the opening repeater and close it off before the closing repeater tag.
On my aspx page i have a repeater with 5 textboxes with 1 imagebutton to edit the row
these textboxes are readonly, to edit them I need them to not be readOnly..
In my behind code i am using :
protected void EditRecipeInfo(object sender, CommandEventArgs e)
{
ImageButton ib = sender as ImageButton;
TextBox titleTXT = (TextBox)ib.FindControl("titleRepeat");
TextBox qtyTXT = (TextBox)ib.FindControl("qtyRepeat");
TextBox uomTXT = (TextBox)ib.FindControl("uomRepeat");
TextBox prepTXT = (TextBox)ib.FindControl("prepRepeat");
TextBox orTXT = (TextBox)ib.FindControl("orRepeat");
titleTXT.ReadOnly = false;
qtyTXT.ReadOnly = false;
uomTXT.ReadOnly = false;
prepTXT.ReadOnly = false;
orTXT.ReadOnly = false;
////
}
But when I fire this event the break points show me that the property is being set to false, but when I click to delete any value in the textbox it still acts like a readonly
UPDATE:
<asp:Repeater ID="ingredRepeater" runat="server">
<HeaderTemplate>
<table style="width: 100%">
<tr>
<th></th>
<th></th>
<th><h2>Title</h2></th>
<th><h2>Qty.</h2></th>
<th><h2>UoM</h2></th>
<th><h2>Prep.</h2></th>
<th><h2>Alternate</h2></th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<asp:ImageButton Style="height: 25px; width: 25px;" ImageUrl="/img/edit.png" Visible="true"
ID="editRecipeInfo" AutoPostBack="true" runat="server" OnCommand="EditRecipeInfo" CommandName='<%# DataBinder.Eval(Container, "DataItem.DetailID") %>' />
</td>
<td>
<asp:ImageButton ImageUrl="/img/RedX.png" ID="button2" runat="server" Height="20"
Width="20" CommandName='<%# DataBinder.Eval(Container, "DataItem.DetailID") %>'
OnCommand="deleteRecipeView" />
</td>
<td>
<asp:TextBox AutoPostBack="true" ReadOnly="true" ID="titleRepeat" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.Name") %>'
size="45" />
</td>
<td>
<asp:TextBox AutoPostBack="true" ReadOnly='true' ID="qtyRepeat" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.Quantity") %>'
size="10" />
</td>
<td>
<asp:TextBox AutoPostBack="true" ReadOnly='true' ID="uomRepeat" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.UnitsOfMeasure") %>'
size="10" />
</td>
<td>
<asp:TextBox AutoPostBack="true" ReadOnly='true' ID="prepRepeat" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.Prep") %>'
size="10" />
</td>
<td>
<asp:TextBox AutoPostBack="true" ReadOnly='true' ID="orRepeat" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.AlternativeIngredients") %>'
size="20" />
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
Make sure you're not re-binding the repeater after you set the ReadOnly property to true.
I agree with #Tariqulazam , the markup would help.
Assuming your code comes from an ItemCommand event handler, I am quite surprised to see the FindControl applied to the ImageButton.
I guess your code should be something like this :
void rpAcces_ItemCommand(object source, RepeaterCommandEventArgs e)
{
//...
ImageButton ib = sender as ImageButton;
TextBox titleTXT = (TextBox)e.Item.FindControl("titleRepeat");
TextBox qtyTXT = (TextBox)e.Item.FindControl("qtyRepeat");
TextBox uomTXT = (TextBox)e.Item.FindControl("uomRepeat");
TextBox prepTXT = (TextBox)e.Item.FindControl("prepRepeat");
TextBox orTXT = (TextBox)e.Item.FindControl("orRepeat");
titleTXT.ReadOnly = false;
qtyTXT.ReadOnly = false;
uomTXT.ReadOnly = false;
prepTXT.ReadOnly = false;
orTXT.ReadOnly = false;
//...
}
Also, be aware that you can not rebind your repeater later in the page life-cycle without losing these changes.
And watch out for any Enabled attribute set on your TextBoxes
Once again, difficult to answer without the whole code.
I wrote a ASP.NET Application that get Data from the Active Directory. I use a ListView to display this data. The User input a String (Lastname or a part of this) in a TextBox. Than the ListView list all AD Users with the same string from the TextBox. Every Line get a Button "Anzeigen" to get more Informations about the User. This ListView has six columns and every line show a User. in column number six is the button "Anzeigen". If a User click on this button open a new WebForm "benutzer.aspx" with more Informations abaout this seleced User and get a Session Value "email" from the line.
My Problem:
I don't know how I get the Index of the Line of the ListView that I need for the Session Value.
My Code:
cs file:
protected void Button1_Command(object sender, CommandEventArgs e)
{
if (e.CommandName == "Anzeigen")
{
//This give me everyone the Value -1 back
int selectedLine = myListView.SelectedIndex;
//I need the Line Index for the right Value
Label lb = (Label)myListView.Items[selectedLine].FindControl("Label2");
string email = lb.Text;
Session["email"] = email;
Response.Redirect("Benutzer.aspx");
}
}
ASPX File:
...
<ItemTemplate>
<tr runat="server">
<td align="left" ><asp:Label ID="Label1" Text='<%# Eval("Benutzer") %>' runat="server" /></td>
<td align="left"><asp:Label ID="Label2" Text='<%# Eval("eMail") %>' runat="server" /></td>
<td align="left"><asp:Label ID="Label3" Text='<%# Eval("Vorname") %>' runat="server" /></td>
<td align="left"><asp:Label ID="Label4" Text='<%# Eval("Nachname") %>' runat="server" /></td>
<td align="left"><asp:Label ID="Label5" Text='<%# Eval("Telefon") %>' runat="server" /></td>
<td align="left"><asp:Button ID="Button1" Text="Anzeigen" OnCommand="Button1_Command" CommandName="Anzeigen" CommandArgument="myArgument" runat="server" /></td>
</tr>
</ItemTemplate>
...
I search and I found listview selectedindices but it don't work :( and I dont't can use it in my Application .
tarasov
Use ListView's ItemCommand rather than Button's on command
see http://www.codeproject.com/Articles/24570/Complete-ListView-in-ASP-NET-3-5
for more detail.
One more thing From example you can see that author has extracted the values from e.Item. You can pass the key(email,username or whatever) as CommandArgument and can access that value directly from command argument.
how to pass it
<asp:LinkButton ID="myLink" runat="server" CommandName="Anzeigen" CommandArgument='<%#Eval("KeyColumn")%>'>Anzeigen</asp:LinkButton>
Also use Linkbutton rather than Asp:Button
ASPX:
<td align="left"><asp:Button ID="Button1" Text="Anzeigen" OnCommand="Button1_Command" CommandName="Select" CommandArgument='<%# Container.DataItemIndex %>' runat="server" /></td>
CS:
protected void Button1_Command(object sender, CommandEventArgs e)
{
if (e.CommandName == "Select")
{
int index = Convert.ToInt32(e.CommandArgument);
Label lb = (Label)myListView.Items[index].FindControl("Label2");
string email = lb.Text;
Session["email"] = email;
Response.Redirect("Benutzer.aspx");
}
}