Reputation: 11
When I click the button I can't enter in the IF condition because I get false value of a checkbox even the checkbox is checked. So please help me out how can I get the true value below is my code?
I request all to help me in this bug.
Here is my aspx file code Default2.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>
<!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 runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="grd" runat="server" AutoGenerateColumns="false" DataKeyNames="USER_CODE">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="chk_row" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="USER_FULL_NAME" HeaderText ="User Full Name" />
<asp:BoundField DataField="USER_DEPT_NAME" HeaderText ="Department" />
</Columns>
</asp:GridView>
<br />
<br />
<asp:Button ID="btn_select" runat="server" Text="Selected DemandNote"
onclick="btn_select_Click" />
<br />
<br />
<asp:Label ID="lbl_msg" runat="server"></asp:Label>
</div>
</form>
</body>
</html>
C# code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
DBConnect db = new DBConnect();
SqlDataAdapter da = new SqlDataAdapter("SELECT USER_CODE,USER_FULL_NAME,USER_DEPT_NAME FROM USER_MASTER",db.connect());
DataSet ds = new DataSet();
da.Fill(ds);
grd.DataSource = ds;
grd.DataBind();
}
protected void btn_select_Click(object sender, EventArgs e)
{
foreach (GridViewRow gvrw in grd.Rows)
{
if (gvrw.RowType == DataControlRowType.DataRow)
{
CheckBox chk = (CheckBox)gvrw.FindControl("chk_row");
if (chk.Checked)
{
lbl_msg.Text = grd.DataKeys[gvrw.RowIndex].Value.ToString();
}
}
}
}
}
Upvotes: 1
Views: 1719
Reputation: 98
A TemplateField works differently than a normal CheckBoxField because you need to look in the cell where the control is to find the CheckBox. So instead of declaring your checkbox like:
CheckBox chk = (CheckBox)gvrw.FindControl("chk_row");
You need to find the CheckBox by:
CheckBox chk = (CheckBox)gvrw.Cells[0].FindControl("chk_row");
Upvotes: 1
Reputation: 448
here's a grid view with Check box in it,
<asp:GridView runat="server" ID="AgencyGrid" OnRowCommand="AgencyGrid_RowCommand" AutoGenerateColumns="False" BackColor="White"
BorderColor="#333534" BorderStyle="None" BorderWidth="1px" CellPadding="4" PageSize="30"
ForeColor="#333534" GridLines="Horizontal" HorizontalAlign="Center" Width="100%" DataSourceID="AgenciesList">
<Columns>
<asp:BoundField DataField="ID" HeaderText="ID" Visible="false" SortExpression="ID" />
<asp:CheckBox runat="server" AutoPostBack="true" ID="ShowDetailsForThisAgency" OnCheckedChanged="ShowDetailsForThisAgency_CheckedChanged" Onclick="RadioCheck(this);" value='<%# Eval("ID") %>' />
</ItemTemplate>
</asp:GridView>
now in order to Get the select check box you will need JS for that :
<script type="text/javascript">
function RadioCheck(rb) {
var gv = document.getElementById("<%=AgencyGrid.ClientID%>");
var rbs = gv.getElementsByTagName("input");
var row = rb.parentNode.parentNode;
for (var i = 0; i < rbs.length; i++) {
if (rbs[i].type == "checkbox") {
if (rbs[i].checked && rbs[i] != rb) {
rbs[i].checked = false;
break;
}
}
}
}
</script>
in this code I'm using the check box check event but you can use it in the button click event this last code will get you the checked box :
foreach (GridViewRow row in this.AgencyGrid.Rows)
{
CheckBox checkedAgency = (CheckBox)row.FindControl("ShowDetailsForThisAgency");
if (checkedAgency.Checked)
{
do your logic
}
}
note : I'm using the JS to allow the user only select on check box if don't need it you can remove the Onclick="RadioCheck(this); and it will work
Upvotes: 0
Reputation: 4753
Do your data bind only for the first time i.e check for !IsPostBack
condidition
protected void Page_Load(object sender, EventArgs e)
{
DBConnect db = new DBConnect();
SqlDataAdapter da = new SqlDataAdapter("SELECT USER_CODE,USER_FULL_NAME,USER_DEPT_NAME FROM USER_MASTER",db.connect());
DataSet ds = new DataSet();
da.Fill(ds);
if(!isPostBack)
{
grd.DataSource = ds;
grd.DataBind();
}
}
Upvotes: 1