ItemCommand of Repeater is not fire with LinkButton - c#

I have a Linkbutton inside a repeater and I want to delete the Item when the user clicks on the Linkbutton; in this case the LinkButton's ItemCommand event is not fired, my code is below:
<asp:Repeater ID="rptSubject" runat="server" OnItemCommand="rptSubject_OnItemCommand">
<ItemTemplate>
<tr>
<td><asp:CheckBox id="chkAll" runat="server"/></td>
<td><%#Eval("SubjectName") %></td>
<td>
<asp:ImageButton ID="imgbtnDelete" ImageUrl="~/assets/images/icons/delete.png" runat="server" CommandName="Delete" CommandArgument='<%#Eval("SubjectID") %>'/>
<asp:LinkButton ID="lnkEditCategory" runat="server" CommandName="EditCategory" CommandArgument='<%#Eval("SubjectID") %>' Text="Edit Category"></asp:LinkButton>
</td>
</tr>
</ItemTemplate>
my repeater's itemcommand event handler is:
protected void rptSubject_OnItemCommand(object source, RepeaterCommandEventArgs e)
{
if (e.CommandName.Equals("Delete"))
{
// some code
}
if (e.CommandName.Equals("EditCategory"))
{
// some code
}
}
when I click on the image button my item command event fires but when I click on the link button it doesn't.

The following code works for me:
<%# Page Language="C#" %>
<script type="text/c#" runat="server">
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
var data = new[]
{
new
{
SubjectID = "1",
SubjectName = "subject name 1"
},
new
{
SubjectID = "2",
SubjectName = "subject name 2"
},
};
rptSubject.DataSource = data;
rptSubject.DataBind();
}
}
protected void RptSubject_OnItemCommand(object source, RepeaterCommandEventArgs e)
{
if (e.CommandName.Equals("Delete"))
{
// some code
}
if (e.CommandName.Equals("EditCategory"))
{
// some code
}
}
</script>
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<form id="Form1" runat="server">
<asp:Repeater ID="rptSubject" runat="server" OnItemCommand="RptSubject_OnItemCommand">
<ItemTemplate>
<div>
<asp:CheckBox id="chkAll" runat="server"/>
<%#Eval("SubjectName") %>
<asp:LinkButton ID="imgbtnDelete" runat="server" CommandName="Delete" CommandArgument='<%#Eval("SubjectID") %>' Text="Delete" />
<asp:LinkButton ID="lnkEditCategory" runat="server" CommandName="EditCategory" CommandArgument='<%#Eval("SubjectID") %>' Text="Edit" />
</div>
</ItemTemplate>
</asp:Repeater>
</form>
</body>
</html>

You also need to make sure you bind to the repeater explicitly
Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
AddHandler rptPages.ItemCommand, AddressOf rptPages_ItemCommand
End Sub

Related

Get data from from repeater via Request.Form to another page

I have a simple repeater to list textbox.
try.aspx
<form id="form1" runat="server" action="try_send.aspx" method="get">
<div>
<asp:Repeater ID="ClRpt" runat="server">
<HeaderTemplate>
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="Kod" runat="server" Text='<%#Eval("KOD") %>'></asp:Label>
<asp:TextBox ID="Amount" runat="server"></asp:TextBox>
<asp:Button ID="ButtonClick" CssClass="bt" runat="server" Text="SEPETE EKLE" Width="100%" UseSubmitBehavior="false" />
</ItemTemplate>
</asp:Repeater>
</div>
</form>
try.aspx.cs:
protected void Page_Load(object sender, EventArgs e)
{
string Firma = Session["FirmaID"].ToString();
string Pryt = "01";
string GRID = "KATALOG";
Page.Title = GRID + " ÜRÜN LİSTESİ";
string SQLGrup = "SQL Here";
SqlCommand rsCl = new SqlCommand(SQLGrup, bag.Bagla());
SqlDataReader ClOku = rsCl.ExecuteReader();
ClRpt.DataSource = ClOku;
ClRpt.DataBind();
ClOku.Close();
}
I want to send textbox value named Amount to try_send.aspx file
try_send.aspx:
<body>
<asp:Label ID="Amount" runat="server" Text="Label"></asp:Label>
</body>
try_send.aspx.cs:
protected void Page_Load(object sender, EventArgs e)
{
decimal Amnt = Convert.ToDecimal(Request.Form["Amount"]);
Amount.Text = Amnt.ToString();
}
Amount is null when I send the form.
Thanks...
In try.aspx, you just show a bunch of information in your Repeater.
You don't have a button to submit the action.
<form id="form1" runat="server">
<asp:Repeater ID="ClRpt" runat="server">
//content
</asp:Repeater>
<asp:Button Id="xx" runat="server" OnClick="xx_OnClick"/>
</form>
Then, you redirect and pass the amount you want in the event click
public void xx_Onclick(object sender, EventArgs e)
{
//redirect to try_send with the value amount
}

Asp.net web form passing argument on button click generated inside loop

I am generating buttons inside foreach loop
<% foreach (var myObject in myObjectList)
{
%>
<b>Text field</b>: <%= myObject.Text%><br>
<asp:Button ID="" runat="server" OnClick="WaitQueueDeleteBtn_Click" CommandArgument="pass myObjectText" Text="Delete"/>
<% } %>
It seems that I cannot use myObject's field inside CommandArgument however myObjectList is accessible
i.e
<asp:Button ID="" runat="server" OnClick="WaitQueueDeleteBtn_Click" CommandArgument="<%#myObjectList.Count%>" Text="Delete"/>
The above statement would work but the one below would not
<asp:Button ID="" runat="server" OnClick="WaitQueueDeleteBtn_Click" CommandArgument="<%#myObject.Text %>" Text="Delete"/>
Any idea why? And how can I pass myObject's field values as CommandArgument?
Update:
Button method in class
protected void WaitQueueDeleteBtn_Click(object sender, EventArgs e)
{
}
It's so easy with Repeater
Markup
<asp:Repeater ID="Repeater1" runat="server" OnItemCommand="Repeater1_ItemCommand">
<ItemTemplate>
<div><b>Text field</b>: <%# Eval("Text") %></div>
<asp:Button runat="server" CommandArgument='<%# Eval("ID") %>' Text="Delete" />
</ItemTemplate>
</asp:Repeater>
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
Repeater1.DataSource = YOUR_DATA_SOURCE; // myObjectList
Repeater1.DataBind();
// ...
}
}
protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
{
if (e.CommandArgument == null) return;
var id = int.Parse(e.CommandArgument.ToString());
// your logic here ...
}
You can add multiple buttons and using CommandName can figure out which one clicked.
Hope this helps.

how to find checkbox where checked in repeater in asp.net?

i want when click btndelete , every checkbox where chechek in repeater1 to get value of column(titr) in same row, but i dont find which checkbox checked and dont access to value of column(titr)
<asp:Repeater ID="Repeater1" runat="server">
<HeaderTemplate>
<table>
<tr>
<td> </td>
<td>subject</td>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<asp:CheckBox ID="CheckBox1" runat="server" />
</td>
<td id="checkvalue" >
<%# Eval("titr") %>
</td>
</tr>
</ItemTemplate>
<FooterTemplate></table></FooterTemplate>
</asp:Repeater>
<asp:Button ID="btndelete" runat="server" Text="delete" OnClick="btndelete_Click" />
.cs code is :
protected void btndelete_Click(object sender, EventArgs e)
{
}
You could get a list of repeaterItems for each row in which the checkbox is checked using linq such that:
List<RepeaterItem> selectedItems = Repeater1.Items.Cast<RepeaterItem>().Where(x => ((CheckBox)x.FindControl("CheckBox1"))
.Checked).ToList();
You could then iterate through the list and get the value of titr for each of the selected rows, but you would have to set some server control equal to "titr" when you bind the control, like:
<asp:literal id="literal1" runat="server"><%# Eval("titr") %></asp:literal>
That way you could go find the value later when you iterate through the list, like this:
List<string> titrValues = selectedItems.Select(t => ((Literal)t.FindControl("literal1).Text));
You may have to do some tweaking, but that should get you the values of titr for each row with a selected value.
Aspx Code:
<%# Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<asp:Repeater ID="Repeater1" runat="server" >
<ItemTemplate>
<div>
<asp:CheckBox ID="CategoryID" runat="server" Text='<%# Eval("val") %>' />
</div>
</ItemTemplate>
</asp:Repeater>
<asp:Button Text="Click" OnClick="Button2_Click" runat="server" />
</form>
</body>
</html>
CS Code:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataTable dt = new DataTable();
dt.Columns.Add("val", typeof(string));
for (int i = 0; i < 10; i++)
dt.Rows.Add("testing" + i.ToString());
Repeater1.DataSource = dt;
Repeater1.DataBind();
}
}
protected void Button2_Click(object sender, EventArgs e)
{
string Rpt = "Repeater Items Checked:<br />";
for (int i = 0; i < Repeater1.Items.Count; i++)
{
CheckBox chk = (CheckBox)Repeater1.Items[i].FindControl("CategoryID");
if (chk.Checked)
{
Rpt += (chk.Text + "<br />");
}
}
Response.Write(Rpt);
}
You can also use break point to check the get values....
Refrenced By: http://www.codeproject.com/Questions/534719/GetplusSelectedplusCheckboxesplusinplusASPplusRepe

How to get LinkButton text in nested Repeaters

I have problem getting the LinkButton text in nested Repeaters
<div>
<asp:Repeater ID="rp_resList" runat="server" OnItemDataBound="rp_resList_ItemDataBound">
<ItemTemplate>
<div class="resourcesResult">
<asp:HiddenField ID="hf_resID" runat="server" Value='<%# Eval("Id") %>' />
<a href='<%# Eval("pageID") %>'><%# Eval("name") %></a>
<br />
<asp:Literal ID="litSummary" runat="server" Text='<%# Eval("summary") %>'></asp:Literal>
<br />
<asp:Repeater ID="rp_tagsTopics" runat="server">
<ItemTemplate>
<h6>
<asp:LinkButton ID="lnkBtnTags" runat="server" Text=' <%# Container.DataItem %>' OnClick="LinkButton1_Click" > <%# Container.DataItem %></asp:LinkButton>
</h6>
</ItemTemplate>
</asp:Repeater>
</div>
</ItemTemplate>
</asp:Repeater>
</div>
protected void LinkButton1_Click(object sender, EventArgs e)
{
LinkButton lnkBtnTags = (LinkButton)rp_tagsTopics.FindControl("lnkBtnTags");
Response.Redirect("~/WebsofWonder.aspx?tag=" + lnkBtnTags.Text);
}
Or you can make use of the ItemCommand event by specifying the CommandName and CommandArgument parameters of the LinkButton
<asp:LinkButton ID="lnkBtnTags" runat="server" Text=' <%# Container.DataItem %>' OnClick="LinkButton1_Click" CommandName="Redirect" CommandArgument='<%# Container.DataItem %>' > <%# Container.DataItem %></asp:LinkButton>
And in the handler use the parameters:
protected void rp_tagsTopics_ItemCommand(object sender, RepeaterCommandEventArgs e)
{
if( e.CommandName == "Redirect" )
{
Response.Redirect("~/WebsofWonder.aspx?tag=" + e.CommandArgument);
}
}
What you should do is use the sender argument in the LinkButton_Click handler to get access to the instance of LinkButton that was actually clicked, and has raised the Click event:
protected void LinkButton1_Click(object sender, EventArgs e)
{
// Use sender instead of trying to find the control within the Repeater
LinkButton lnkBtnTags = (LinkButton) sender;
Response.Redirect("~/WebsofWonder.aspx?tag=" + lnkBtnTags.Text);
}

ClientSideEvents on ASP:Net repeater control

I'm creating a textbox control in a repeater. Here is .cs code:
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
Est estimationItem = (Est)e.Item.DataItem;
TextBox txtWeekly = (TextBox)e.Item.FindControl("txtWeekly");
txtWeekly.Text = estimationItem.SMEst.ToString();
}
And here is .aspx code:
<asp:Repeater ID="WeeklyEst" OnItemDataBound="WeeklyEst_ItemDataBound" runat="server">
<HeaderTemplate>
<table>
<tr>
</HeaderTemplate>
<ItemTemplate>
<td>
<asp:TextBox ID="txtWeekly" runat="server">
<ClientSideEvents OnTextChanged="function(s, e) { alert('AlertIsHere!');}" />
</asp:TextBox>
</td>
</ItemTemplate>
<FooterTemplate>
<td>
</td>
</tr>
</table>
</FooterTemplate>
</asp:Repeater>
I want to do is firing an alert when text changed on text box. How can I do this? CliendSideEvent fire on another textbox but in repeater control, it doesn't work.
Use the ItemDataBound event of Repeater control, you can do this like..., if i understood you correctly
protected void myRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if(e.Item.ItemType == ListItemType.Item)
{
TextBox mytxt = e.Item.FindControl("txtWeekly") as TextBox;
// you can just pass "this" instead of "mytxt.ClientID" and get the ID from the DOM element
mytxt.Attributes.Add("onchange", "doStuff('" + mytxt.ClientID + "');");
}
}
if you donot want to do this in codebehind then you can use jquery ,it would be lot easier
$('.txtbox').change(function() {
alert($(this).val());
});
just give cssclass 'txtbox' to your textbox.
What about this unobtrusive approach
<head runat="server">
<title></title>
<script type="text/javascript" src="js/jquery-1.7.1.js" ></script>
<script type="text/javascript">
$(document).ready(function () {
// select all input text ending with txtWeekly as repeater control
// will create something like rptWeeklyEst$ctl00$txtWeekly
$('input[name$="txtWeekly"][type=text]').change(function () {
alert('textbox changed to ' + $(this).val());
});
});
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Repeater ID="rptWeeklyEst" runat="server">
<ItemTemplate>
<asp:TextBox ID="txtWeekly" runat="server"></asp:TextBox>
</ItemTemplate>
</asp:Repeater>
<br />
Nope
<asp:TextBox ID="txtNope" runat="server"></asp:TextBox>
</div>
</form>
</body>

Categories

Resources