 |
|
 |
Hi,
I am inserting data into database by selecting checkbox in datagrid.
But when same page is open,which checkboxes have seen seleced those should appear as checked according to specific condition.
Help me.
Thank you.
|
|
|
|
 |
|
 |
hi there, i did it but using code not javascripts
/*************************************************/
#region TODO: check/uncheck all CheckBox in GridView
protected void Create_Status_CheckBox_All_CheckedChanged(object sender, EventArgs e)
{
bool checkState=false;
if (((CheckBox)(sender)).Checked)
checkState = true;
ToggleCheckState("Create_Status_CheckBox", checkState);
}
protected void Update_Status_CheckBox_All_CheckedChanged(object sender, EventArgs e)
{
bool checkState = false;
if (((CheckBox)(sender)).Checked)
checkState = true;
ToggleCheckState("Update_Status_CheckBox", checkState);
}
protected void Delete_Status_CheckBox_All_CheckedChanged(object sender, EventArgs e)
{
bool checkState = false;
if (((CheckBox)(sender)).Checked)
checkState = true;
ToggleCheckState("Delete_Status_CheckBox", checkState);
}
private void ToggleCheckState(string CheckBoxID,bool checkState)//, GridView grid)
{
if (CheckBoxID == "")
return;
// Iterate through the Products.Rows property
try
{
foreach (GridViewRow row in FuncsGridView.Rows)
{
// Access the CheckBox
CheckBox cb = (CheckBox)row.FindControl(CheckBoxID);
if (cb != null)
cb.Checked = checkState;
}
}
catch (Exception ex)
{
return;
}
}
#endregion
/*************************************************/
and in the aspx (in gridview columns
/*****************************************************/
<asp:TemplateField HeaderText="Create" SortExpression="cb1data">
<asp:CheckBox ID="Create_Status_CheckBox" runat="server" Checked='<%# IsActive((string)(Eval("Create_Status"))) %>' />
<HeaderTemplate >
<asp:CheckBox ID="Create_Status_CheckBox_All" AutoPostBack="true" Text="|Create|" runat="server" OnCheckedChanged="Create_Status_CheckBox_All_CheckedChanged" />
</HeaderTemplate>
<HeaderStyle CssClass="from" HorizontalAlign="Left" />
<asp:TemplateField HeaderText="Update" SortExpression="cb1data">
<asp:CheckBox ID="Update_Status_CheckBox" runat="server" Checked='<%# IsActive((string)(Eval("Update_Status"))) %>' />
<HeaderTemplate >
<asp:CheckBox ID="Update_Status_CheckBox_All" AutoPostBack="true" Text="|Update|" runat="server" OnCheckedChanged="Update_Status_CheckBox_All_CheckedChanged" />
</HeaderTemplate>
<HeaderStyle CssClass="from" HorizontalAlign="Left" />
<asp:TemplateField HeaderText="Delete" SortExpression="cb1data">
<asp:CheckBox ID="Delete_Status_CheckBox" runat="server" Checked='<%# IsActive((string)(Eval("Delete_Status"))) %>' />
<HeaderTemplate >
<asp:CheckBox ID="Delete_Status_CheckBox_All" AutoPostBack="true" Text="|Delete|" runat="server" OnCheckedChanged="Delete_Status_CheckBox_All_CheckedChanged" />
</HeaderTemplate>
<HeaderStyle CssClass="from" HorizontalAlign="Left" />
/*****************************************************/
where the function IsActive is my owen function to return true or false
/****************/
public bool IsActive(string asd)
{
if (asd == "Yes")
return true;
else if (asd == null)
return false;
else
return false;
}
/****************/
and that is it, so anyone can provide javascript sol to enhance posting back ??
|
|
|
|
 |
|
 |
Hi there, thanks a lot for this great topic.
I've Q,
if I've griview like this
<img src="http://aycu22.webshots.com/image/35901/2003242605890244495_rs.jpg" />
and I want to check all in these 3 columns , so can you provide any help !! thanks in advance
|
|
|
|
 |
|
 |
Even wen I hav checked the check box it returns false.. I am pasting the code here....
Any help would be strongly appreciated.
I need it urgently guys..!!!
for (int i = 0; i < GridView1.Rows.Count; i++)
{
GridViewRow row = GridView1.Rows[i];
bool check= ((CheckBox)row.FindControl("chkselect")).Checked;
if (check)
{
string str=GridView1.Rows[i].Cells[1].Text;
Response.Write(str);
}
}
Also the DataItemIndex is zero..
I dont know how to resolve the problem since i am a beginner in asp.net please help me out
Regards,
Namrata.
-- modified at 5:43 Thursday 11th October, 2007
|
|
|
|
 |
|
 |
Hello , this problem faced me too , if you fiend solution plz write it here.
thanks a lot.
|
|
|
|
 |
|
 |
<script language="javascript" type="text/javascript">
function SelectAllCheckboxes(spanChk)
{
var oItem = spanChk.children;
var theBox=(spanChk.type=="checkbox")?spanChk:spanChk.children.item[0];
xState=theBox.checked;
elm=theBox.form.elements;
for(i=0;i
<HeaderTemplate>
<input id="chkAll" önclick="javascript:SelectAllCheckboxes(this);" runat="server" type="checkbox" />
</HeaderTemplate>
This Code Works Fine... But i got the Error in the line below...
row.Cells[2].Text...
But i bugged all the errors and it works fine...
Thank You For this Article...!
Regards,
UmayalDevi.R
-- modified at 1:46 Saturday 27th October, 2007
|
|
|
|
 |
|
 |
Great , well done
It makes problems with me as it return false even selected.
the error was that I was bindding the gridview in each load
i put this bindding in the event if (!Page.IsPostBack){GridView1.DataBind();}
and its great.
thanks a lot.
|
|
|
|
 |
|
 |
Thank you. It really worked.
|
|
|
|
 |
|
 |
If i have two cloumn each CheckBox , how can i your javascript apply to that, and also if one check box is checked other cloumn check box have to unchanged using javascript.
Tanveer
|
|
|
|
 |
|
 |
Using your example I have been trying without success to update a particular Gridview field/cell with a value, using the button click, for all rows with checkbox selected. Is this possible?
Ken Laing
|
|
|
|
 |
|
 |
You have no idea how long I have searched for a way to do this... And as it turns out, look how simple it is... Absolutely brilliant. Thanks so much for publishing this article. You get my '5' vote for this one!
|
|
|
|
 |
|
 |
Thanks a lot azamsharp, this article is really helpful
|
|
|
|
 |
|
 |
Here is my HTML code plz check it i still got checkbox value false though i select checkbox and do debug
Thanks in advance.
<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="address.aspx.vb" Inherits="WebApplication1.address" %>
<!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>Contact Address</title>
<%-- following script for close window--%>
<script lang ="ja" type ="text/javascript" >
function callclose()
{
var newwindow;
newwindow=window.close()
return true
}
</script>
<%-- following java scrpt is for check all checkboxes--%>
<script language=javascript>
function SelectAllCheckboxes(spanChk){
// Added as ASPX uses SPAN for checkbox
var oItem = spanChk.children;
var theBox= (spanChk.type=="checkbox") ?
spanChk : spanChk.children.item[0];
xState=theBox.checked;
elm=theBox.form.elements;
for(i=0;i
</head>
<body>
<form id="form1" runat="server">
<asp:LinkButton ID="lnkclose" runat="server" Style="left: 104px;
position: relative; top: 160px" OnClick ="lnkclose_click" >Close
<asp:GridView
ID="GridView1" runat="server" AutoGenerateColumns="False" Style="position: relative; top: 0px;">
<asp:TemplateField HeaderText ="Select">
<HeaderTemplate >
<input id="chkAll" önclick="javascript:SelectAllCheckboxes(this);" runat="server" type="checkbox" />
</HeaderTemplate >
<asp:CheckBox ID="chk1" Enabled =true EnableTheming =true Visible =true EnableViewState =true runat="server" />
<asp:BoundField DataField ="name" HeaderText ="Name" />
<asp:BoundField DataField ="email" HeaderText ="Email-ID" />
<asp:Button ID="btnsubmmit" runat ="server" Text ="Submmit" />
<asp:CheckBox ID="chk1" runat="server" Style="left: -62px; position: relative;
top: 96px" />
</form>
</body>
</html>
============================================
and follow is my .aspx.vb code
Imports System.Data
Imports System.Data.SqlClient
Imports System.Text
Partial Public Class address
Inherits System.Web.UI.Page
Dim con As SqlConnection
Dim cmd As SqlCommand
Dim sread As SqlDataReader
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
con = New SqlConnection("server=spick;database=address;uid=sa;pwd=sa")
con.Open()
Dim s As String
s = "select name,email from address"
cmd = New SqlCommand(s, con)
sread = cmd.ExecuteReader()
GridView1.DataSource = sread
GridView1.DataBind()
con.Close()
End Sub
Protected Sub lnkclose_click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lnkclose.Click
lnkclose.Attributes.Add("onclick", "return callclose()")
End Sub
Protected Sub btnsubmmit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnsubmmit.Click
If IsPostBack = True Then
Dim str As StringBuilder = New StringBuilder()
' Select the checkboxes from the GridView control
Dim i As Integer
For i = 0 To GridView1.Rows.Count - 1 Step i + 1
Dim row As GridViewRow = GridView1.Rows(i)
Dim isChecked As Boolean = (CType(row.FindControl("chk1"), CheckBox)).Checked
If (isChecked) Then
str.Append(GridView1.Rows(i).Cells(2).Text)
End If
Next
' prints out the result
Response.Write(str.ToString())
End If
End Sub
End Class
Thnks,
Jigar Chaudhari,
Software Developer
|
|
|
|
 |
|
|
 |
|
 |
Check out the following article:
http://gridviewguy.com/ArticleDetails.aspx?articleID=228
Mohammad Azam
azamsharp@gmail.com
www.gridviewguy.com
videos.gridviewguy.com
Houston, TEXAS
|
|
|
|
 |
|
 |
Thank you for your Response.
I alrady solve my problem with one of other blogger help and your article use.
thanks a lots .
Jigar Chaudhari,
Software Developer.
hi
|
|
|
|
 |
|
 |
Hey jigar let me know how did you solve your problem..
Even I am facing the same hurdle..
Regards,
Namrata
|
|
|
|
 |
|
 |
Thank you vary much.
I alrady solve problem with one of another blogger vijay modi and your article helps.
I also visited your site ,really such a helpful idea given by you.
Thanks a lots,
hi
Jigar Chaudhari
Software Devloper,
Ahmedabad-India
|
|
|
|
 |
|
 |
hello sir,
I tried to use above article which was posted by mohmad but i have problem in
checkbox selection.
when i select checkbox then after when i debug this code in vb.net they give me false value rathere than true.
here is my code.
so plz give me a solution its a agrent.
Thanks to all u in advance.
Dim str As StringBuilder = New StringBuilder()
' Select the checkboxes from the GridView control
Dim i As Integer
For i = 0 To GridView1.Rows.Count - 1 Step i + 1
If CType(GridView1.Rows(i).FindControl("chk1"), CheckBox).Checked = True Then
str.Append(GridView1.Rows(i).Cells(2).Text)
End If
Next
' prints out the result
Response.Write(str.ToString())
hi
Worm Regards,
Jigar chaudhari
|
|
|
|
 |
|
 |
Please update your code with the following one:
===================================================
Dim str As StringBuilder = New StringBuilder()
' Select the checkboxes from the GridView control
Dim i As Integer
For i= 0 To GridView1.Rows.Count - 1
Dim gvRow As GridViewRow = GridView1.Rows(i)
If CType(gvRow.FindControl("chk1"), CheckBox).Checked Then
str.Append("testing")
End If
Next
===================================================
I think it will help you.
Regards...
VBM
Vijay Modi
Software Engineer.
|
|
|
|
 |
|
 |
the samething happen to my website appliaction, when i checked the check box,the value were still false. it seems that when i cheked the value in the gridview on client-side. it did not work. appreciate for considering it.
i am a boy from china. my engish is not pity good . so ask a excuse from everyone... thank you.
|
|
|
|
 |
|
 |
The ((CheckBox)row.FindControl("chkSelect")).Checked
always return false. Even i check all in the CheckBox Control. I can not get the result from it. Here is my code. Please help. I have try this a days. Thank you too much.
In default.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!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>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True"
AutoGenerateColumns="False" DataKeyNames="USER_ID" Width="366px" CellPadding="4" ForeColor="#333333" GridLines="None">
<asp:TemplateField HeaderText="Select">
<asp:CheckBox ID="chkSelect" runat="server" />
<HeaderTemplate>
</HeaderTemplate>
<SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
<HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
<asp:TextBox ID="TextBox1" runat="server">
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
</form>
</body>
</html>
In default.aspx.cs
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.OracleClient;
using System.Net;
public partial class _Default : System.Web.UI.Page
{
DataSet ds;
protected void Page_Load(object sender, EventArgs e)
{
OracleConnection oracleConnection = new OracleConnection();
oracleConnection.ConnectionString = "user id = scott; data source=mysource; password=password";
oracleConnection.Open();
OracleCommand oraclecomm = new OracleCommand("select * from USER_INFO", oracleConnection);
OracleDataAdapter adapter = new OracleDataAdapter(oraclecomm);
ds = new DataSet();
adapter.Fill(ds, "table1");
GridView1.DataSource = ds;
GridView1.DataBind();
}
protected void Button1_Click(object sender, EventArgs e)
{
for (int i = 0; i < GridView1.Rows.Count; i++)
{
GridViewRow row = GridView1.Rows[i];
bool isChecked = ((CheckBox)row.FindControl("chkSelect")).Checked;
if (isChecked)
{
TextBox1.Text += "1";
TextBox1.Text +=(GridView1.Rows[i].Cells[0].Text);
}
else
{
TextBox1.Text += "2";
TextBox1.Text += (GridView1.Rows[i].Cells[0].Text);
}
}
}
}
|
|
|
|
 |
|
 |
Hi,
thank you for the wonderful example. I tried this example on a simple test page and it works. But if I want to use this code in my page, which is using a MasterPage, it doesn't work anymore. It says "Object expected".
The GridView is placed in a ContentPlaceHolder. I tried to put the javascript code in the MasterPage and in the page which is using the MasterPage too, but both won't work. I tried to put a simple alert in the function and the alert isn't run at all... It seems asp.net can not find the function at all...
How should I do this right?
Thank you in advance.
|
|
|
|
 |
|
 |
Hi,
Can you paste some code so that I can look into it?
Thanks,
Azam
Mohammad Azam
.net Web Developer
Undergraduate Student ( Computer Science )
University of Houston Main Campus
email: azamsharp@gmail.com
www.azamsharp.net
www.gridviewguy.com
|
|
|
|
 |
|
 |
Hi Azam,
thank you for your fast response, but I found the solution.
I have put the javascript function in a .js file, and in the Page_Load() method from the page that is using the MasterPage, I register the script with this method:
ClientScript.RegisterClientScriptInclude("key", "./js/javascript.js");
Thank you.
|
|
|
|
 |