How to/Help: Getting Values for Selected Rows in a Repeater

R

renil

I have a repeater control that displays info. from a datatable. Each row in
the repeater has a checkbox. Also, I have a delete linkbutton outside the
repeater control. What I'm trying to do when the delete linkbutton is
clicked is get the SubscriptionID from the repeater control for each row in
which the checkbox is checked, then call a delete function. (See the code
for the .aspx and .aspx.cs below.)

The problem I'm having is that I cannot get the SubscriptionID for each row
in the repeater in which the checkbox is checked.

How can I cycle through each row in the repeater, check whether the checkbox
is checked or not, and, if it's checked, get the SubscriptionID for that row
in the repeater and send it to a delete function?



..aspx
<asp:Image ID="ImageDelete" runat="server"
ImageUrl="~/Images/16del.gif" ToolTip="Delete" ImageAlign="Middle" />
<asp:LinkButton ID="LinkButtonDelete" runat="server"
OnClick="LinkButtonDelete_Click" ToolTip="Delete">Delete</asp:LinkButton>
<br />
<asp:Repeater ID="RepeaterSchedules" runat="server"
OnItemCommand="RepeaterSchedules_ItemCommand">
<HeaderTemplate>
<table>
<tr class="headerrow">
<td align="center" valign="middle">
<asp:CheckBox
ID="CheckBoxSelectedHeader" runat="server" AutoPostBack="True"
OnCheckedChanged="CheckBoxSelectedHeader_CheckedChanged" />
</td>
<td align="center" valign="middle"></td>
<td align="center"
valign="middle">Report</td>
<td align="center" valign="middle">Path</td>
<td align="center"
valign="middle">Description</td>
<td align="center" valign="middle">Last
Executed</td>
<td align="center"
valign="middle">Status</td>
<td align="center"
valign="middle">Owner</td>
<td align="center" valign="middle">Modified
By</td>
<td align="center" valign="middle">Modified
Date</td>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td align="center" valign="middle"
style="background-color: #EEEEEE">
<asp:CheckBox ID="CheckBoxSelected"
runat="server" />
</td>
<td align="center" valign="middle"
style="background-color: #EEEEEE">
<asp:LinkButton ID="LinkButtonEdit"
runat="server" CommandArgument='<%# DataBinder.Eval(Container.DataItem,
"SubscriptionID")%>'>Edit</asp:LinkButton>
</td>
<td align="center" valign="middle"
style="background-color: #EEEEEE">
<%# DataBinder.Eval(Container.DataItem,
"Report") %>
</td>
<td align="center" valign="middle"
style="background-color: #EEEEEE">
<%# DataBinder.Eval(Container.DataItem,
"Path") %>
</td>
<td align="center" valign="middle"
style="background-color: #EEEEEE">
<%# DataBinder.Eval(Container.DataItem,
"Description")%>
</td>
<td align="center" valign="middle"
style="background-color: #EEEEEE">
<%# DataBinder.Eval(Container.DataItem,
"LastExecuted") %>
</td>
<td align="center" valign="middle"
style="background-color: #EEEEEE">
<%# DataBinder.Eval(Container.DataItem,
"Status") %>
</td>
<td align="center" valign="middle"
style="background-color: #EEEEEE">
<%# DataBinder.Eval(Container.DataItem,
"Owner")%>
</td>
<td align="center" valign="middle"
style="background-color: #EEEEEE">
<%# DataBinder.Eval(Container.DataItem,
"ModifiedBy")%>
</td>
<td align="center" valign="middle"
style="background-color: #EEEEEE">
<%# DataBinder.Eval(Container.DataItem,
"ModifiedDate")%>
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>


..aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BuildSchedulesList();
}
}

private void BuildSchedulesList()
{
System.Data.DataTable tbl = new System.Data.DataTable("Schedules");
System.Data.DataColumn colSubscriptionID =
tbl.Columns.Add("SubscriptionID", typeof(string));
System.Data.DataColumn colReport = tbl.Columns.Add("Report",
typeof(string));
System.Data.DataColumn colPath = tbl.Columns.Add("Path",
typeof(string));
System.Data.DataColumn colDescription =
tbl.Columns.Add("Description", typeof(string));
System.Data.DataColumn colLastExecuted =
tbl.Columns.Add("LastExecuted", typeof(System.DateTime));
System.Data.DataColumn colStatus = tbl.Columns.Add("Status",
typeof(string));
System.Data.DataColumn colOwner = tbl.Columns.Add("Owner",
typeof(string));
System.Data.DataColumn colModifiedBy = tbl.Columns.Add("ModifiedBy",
typeof(string));
System.Data.DataColumn colModifiedDate =
tbl.Columns.Add("ModifiedDate", typeof(System.DateTime));

foreach (object in collection)
{
System.Data.DataRow row = tbl.NewRow();

row["SubscriptionID"] = subscription.SubscriptionID;
row["Report"] = subscription.Report;
row["Path"] = subscription.Path;
row["Description"] = subscription.Description;
row["LastExecuted"] = subscription.LastExecuted;
row["Status"] = subscription.Status;
row["Owner"] = subscription.Owner;
row["ModifiedBy"] = subscription.ModifiedBy;
row["ModifiedDate"] = subscription.ModifiedDate;

tbl.Rows.Add(row);
}
}

this.RepeaterSchedules.DataSource = tbl;
this.RepeaterSchedules.DataBind();
}

protected void RepeaterSchedules_ItemCommand(object source,
RepeaterCommandEventArgs e)
{
// Get the SubscriptionID
Response.Write(e.CommandArgument.ToString());
}

protected void LinkButtonDelete_Click(object sender, EventArgs e)
{
for (int i = 0; i < this.RepeaterSchedules.Items.Count; i++)
{
System.Web.UI.WebControls.CheckBox cb =
(System.Web.UI.WebControls.CheckBox)this.RepeaterSchedules.Items.FindCont
rol("CheckBoxSelected");

if (cb.Checked)
{
// How can I get the SubscriptionID for each row with the
checkbox checked?
// Pass the SubscriptionID to the delete method
}
}
}
 
C

chris

foreach (RepeaterItem dataItem in RepeaterSchedules.Items)
{
CheckBox ckDelete =
(CheckBox)dataItem.FindControl("CheckBoxSelected");
//txtSubscriptionID is a <asp:Textbox/> that has
visible=false.
TextBox txtSubscriptionID =
(TextBox)dataItem.FindControl("txtSubcriptionID");
if (ckDelete.Checked)
{
//delete the item
DoDelete(txtSubcriptionID.Text);
}
}

HTH,
Chris
 
A

Alan Silver

As it happens, I posted a question about this very issue earlier today.

I have a similar bit of code, but if you select more than one checkbox,
and then submit, the code only ever detects the first checkbox that has
been selected and thinks that the rest are unchecked.

Any ideas? If you want to see some code, look in the thread named "How
to get checkbox values form a repeater (looping only finds the first)"
although you'll find the code looks a lot like yours.

TIA
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,045
Latest member
DRCM

Latest Threads

Top