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

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

Related

ASP.NET & C# : gridview select does no action

Why is GridView1_SelectedIndexChanged not working?
I'm learning ASP.NET on my own (which means I learn through youtube), using VS 2015.
I created a grid of items and want to show the selected item in a textbox, but code doesn't do any change.
I created an empty project, added a masterpage and default page, and just added a text and grid, scriptmanager updatepanel contenttemplate.
Whenever I click select on any row from the grid textbox is still empty.
MasterPage.master:
<%# Master Language="C#" AutoEventWireup="true" CodeFile="MasterPage.master.cs" Inherits="MasterPage" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<asp:ContentPlaceHolder id="head" runat="server">
</asp:ContentPlaceHolder>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ContentPlaceHolder id="ContentPlaceHolder1" runat="server">
</asp:ContentPlaceHolder>
</div>
</form>
</body>
</html>
Default.aspx:
<%# Page Title="" enableeventvalidation="true" Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
<style type="text/css">
.auto-style1 {
width: 100%;
}
</style>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<table class="auto-style1">
<tr>
<td>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
<tr>
<td>
<asp:GridView ID="GridView1" runat="server" OnSelectedIndexChanged="GridView1_SelectedIndexChanged">
<Columns>
<asp:CommandField ButtonType="Button" ShowSelectButton="True" />
</Columns>
</asp:GridView>
</td>
</tr>
</table>
</ContentTemplate>
</asp:UpdatePanel>
</asp:Content>
Default.aspx.cs:
using System;
using System.Data;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataTable tbl = new DataTable();
tbl.Columns.Add("serial");
for (int i = 0; i < 4; i++)
{
DataRow row = tbl.NewRow();
row[0] = i + 1;
tbl.Rows.Add(row);
}
GridView1.DataSource = tbl;
GridView1.DataBind();
}
}
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
TextBox1.Text = GridView1.SelectedRow.Cells[0].Text;
}
}
You are selecting the wrong cell on the SelectedIndexChanged method.
The first Cell (index 0) contains the button, so you want the second cell (index 1).
You should place a breakpoint on the method and see for yourself what other properties are filled in the GridView.

Only allow one checkbox to be selected in gridview within modalpopup

I have a modal popup that has a gridview, this gridview has a number of rows, I only want the user to be able to select one row. So if they select another it will deselect the previous one.
I have tried a number of methods but can't get the oncheckedchanged event to fire.
Please can someone assist
Cheers
<asp:button id="btnShowPopupOW" style="display: none" runat="server" />
<asp:modalpopupextender id="mpeOW" behaviorid="mpeOW" runat="server" targetcontrolid="btnShowPopupOW"
popupcontrolid="pnlpopupOW" cancelcontrolid="imgOWCancel" backgroundcssclass="modalBackground" />
<asp:panel id="pnlpopupOW" runat="server" width="600px" style="display: none;" class="ModalPanel">
<div style="position: relative; min-height: 490px;">
<asp:UpdatePanel ID="upExisting" runat="server" ChildrenAsTriggers="true">
<ContentTemplate>
<table style="width: 600px;">
<tr height="25px">
<td>
<asp:Panel ID="pnlPrev" runat="server" Height="200px" ScrollBars="Auto" HorizontalAlign="Center">
<asp:GridView ID="grdPrevious" runat="server" ClientIDMode="Static" AutoGenerateColumns="false" Width="100%"
ShowFooter="false" ShowHeaderWhenEmpty="false" DataKeyNames="ID" >
<Columns>
<asp:BoundField DataField="dates" HeaderText="Dates" />
<asp:BoundField DataField="Prev" HeaderText="Previous" />
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="ChkSelect" runat="server" OnCheckedChanged="ChkSelect_OnCheckedChanged" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</asp:Panel>
</td>
</tr>
</table>
</ContentTemplate>
<Triggers>
</Triggers>
</asp:UpdatePanel>
</div>
</asp:panel>
with the following in the codebehind
protected void ChkSelect_OnCheckedChanged(object sender, EventArgs e)
{
CheckBox activeCheckBox = sender as CheckBox;
foreach (GridViewRow rw in grdPrevious.Rows)
{
CheckBox chkBx = (CheckBox)rw.FindControl("ChkSelect");
if (chkBx != activeCheckBox)
{
chkBx.Checked = false;
}
else
{
chkBx.Checked = true;
}
}
}
You can do it like this. With single check box selection using jquery....
<ItemTemplate>
<asp:CheckBox ID="ChkSelect" runat="server" onclick="CheckOne(this)" />
</ItemTemplate>
function CheckOne(obj) {
var grid = obj.parentNode.parentNode.parentNode;
var inputs = grid.getElementsByTagName("input");
for (var i = 0; i < inputs.length; i++) {
if (inputs[i].type == "checkbox") {
if (obj.checked && inputs[i] != obj && inputs[i].checked) {
inputs[i].checked = false;
}
}
}
}
you want to a single checked in check box . i think Using this you will be able to select only one check box at a time. Put the following java script code in the head section of the web page
<script type="text/javascript">
function SingleCheckboxCheck(ob)
{
var gridvalue = ob.parentNode.parentNode.parentNode;
var inputs = gridvalue.getElementsByTagName("input");
for(var i=0;i<inputs.length;i++)
{
if (inputs[i].type =="checkbox")
{
if(ob.checked && inputs[i] != ob && inputs[i].checked)
{
inputs[i].checked = false;
}
}
}
}
</script>
Here checkbox as a TemplateField of the GridView inside just call the above javascript function to make it single checkable.
onclick ="SingleCheckboxCheck(this)"
What you need is probably a (RadioButton)[http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.radiobutton(v=vs.110).aspx] instead of a CheckBox.
You can group the RadioButton's together which then allows the user to only select one item and automatically deselects the previous selected - or: exactly the behavior you want for your application.
This example code is taken from the linked MSDN Documentation. Note the GroupName attribute
<%# Page Language="C#" AutoEventWireup="True" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>RadioButton Example</title>
<script language="C#" runat="server">
void SubmitBtn_Click(Object Sender, EventArgs e) {
if (Radio1.Checked) {
Label1.Text = "You selected " + Radio1.Text;
}
else if (Radio2.Checked) {
Label1.Text = "You selected " + Radio2.Text;
}
else if (Radio3.Checked) {
Label1.Text = "You selected " + Radio3.Text;
}
}
</script>
</head>
<body>
<h3>RadioButton Example</h3>
<form id="form1" runat="server">
<h4>Select the type of installation you want to perform:</h4>
<asp:RadioButton id="Radio1" Text="Typical" Checked="True" GroupName="RadioGroup1" runat="server" />
<br />
This option installs the features most typically used. <i>Requires 1.2 MB disk space.</i><br />
<asp:RadioButton id="Radio2" Text="Compact" GroupName="RadioGroup1" runat="server"/>
<br />
This option installs the minimum files required to run the product. <i>Requires 350 KB disk space.</i>
<br />
<asp:RadioButton id="Radio3" runat="server" Text="Full" GroupName="RadioGroup1" />
<br />
This option installs all features for the product. <i>Requires 4.3 MB disk space.</i>
<br />
<asp:button text="Submit" OnClick="SubmitBtn_Click" runat="server"/>
<asp:Label id="Label1" font-bold="true" runat="server" />
</form>

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>

ItemCommand of Repeater is not fire with LinkButton

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

Gridview not visible even after binding

I got stucked to some weird condition where I have a gridview inside a ajax toolkit tabcontainer. On tab index change i am binding grid. But nothing happend. Grid is not appearing. I have check the following
Viewstate
Visibility of grid
Visibility of the parent table.
Data is coming from the method
visibility of the tab panel
Even i have debugged and added watch to check if its getting null before loading the page.
Please help me out
** BELOW IS THE UPDATED CODE**
<HTMLCode>
<Toolkit:TabPanel HeaderText="Pending Challans" ID="tpPendingChallan" runat="server" Height="200px" >
<ContentTemplate>
<table style="width: 100%">
<tr>
<td>
<asp:GridView ID="gvPendingChallans" runat="server" AutoGenerateColumns="True" CellPadding="4" Width="100%" OnPageIndexChanging="gvPendingChallans_PageIndexChanging"
OnRowCommand="gvPendingChallans_RowCommand" AllowPaging="True" GridLines="None">
</asp:GridView>
</td>
</tr>
</table>
</ContentTemplate>
</Toolkit:TabPanel>
</HTMLCode>
========================================================================
<C#>
private void BindPendingChallans()
{
var dat = JobCardManager.DisplayChallan();
gvPendingChallans.DataSource = dat;
gvPendingChallans.DataBind();
}
protected void tcMembers_ActiveTabChanged(object sender, EventArgs e)
{
if(tcMembers.ActiveTabIndex == 6)
{
BindPendingChallans();
}
}
</C#>
Sorry for miss interpretation of your code with my first answer. I thought that it just a simple population of grid view, but as review I found that you are using the Ajax Toolkit library and your grid is inside the tab selection. You can try this:
<%# Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%# Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="Toolkit" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head2" runat="server">
<title>Untitled Page</title>
<style type="text/css">
.style1
{
font-family: Arial;
color: #3399FF;
}
</style>
</head>
<body class="style1">
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager2" runat="server" EnablePageMethods="true" />
<div>
<asp:UpdatePanel ID="upMember" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<table cellpadding="1" cellspacing="4" border="0" width="100%">
<tr>
<td>
<Toolkit:TabContainer ID="tcMembers" runat="server" AutoPostBack="true"
ActiveTabIndex="0" onactivetabchanged="tcMembers_ActiveTabChanged">
<Toolkit:TabPanel HeaderText="Pending Challans" ID="tpPendingChallan" runat="server"
Height="200px">
<ContentTemplate>
<asp:GridView ID="gvPendingChallans" runat="server" AutoGenerateColumns="True" CellPadding="4"
Width="100%" OnPageIndexChanging="gvPendingChallans_PageIndexChanging" OnRowCommand="gvPendingChallans_RowCommand"
AllowPaging="True" GridLines="None">
</asp:GridView>
</ContentTemplate>
</Toolkit:TabPanel>
<Toolkit:TabPanel HeaderText="Pending 2" ID="tpPending2" runat="server"
Height="200px">
<ContentTemplate>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="True" CellPadding="4"
Width="100%" OnPageIndexChanging="gvPendingChallans_PageIndexChanging" OnRowCommand="gvPendingChallans_RowCommand"
AllowPaging="True" GridLines="None">
</asp:GridView>
</ContentTemplate>
</Toolkit:TabPanel>
</Toolkit:TabContainer>
</td>
<td width="2%">
</td>
</tr>
</table>
</ContentTemplate>
</asp:UpdatePanel>
</div>
</form>
</body>
</html>
Then in your code behind:
protected void Page_Load(object sender, EventArgs e)
{
upMember.Update();
}
private void BindPendingChallans()
{
//var dat = JobCardManager.DisplayChallan();
//gvPendingChallans.DataSource = dat; gvPendingChallans.DataBind();
}
protected void tcMembers_ActiveTabChanged(object sender, EventArgs e)
{
if (tcMembers.ActiveTabIndex == 6)
{
BindPendingChallans();
}
}
protected void gvPendingChallans_PageIndexChanging(object sender, GridViewPageEventArgs e){
}
protected void gvPendingChallans_RowCommand(object sender, GridViewCommandEventArgs e){
}
Note: That in you 'tcMembers_ActiveTabChanged' you had specify tab index 6
The Tab index begins with 0. Maybe you can adjust it depending the number of
you Intended tab....
Regards,

Categories

Resources