qwerks
asked on
Accessing a checkbox inside a DataGrid
Hello. I am new to ASP.NET and need help on how to access checkbox inside a datagrid that is populated by a SQL database. I have the pseudocode written in the void Click_Cancel (Object s, EventArgs E) function.
<%@ Page Language="C#" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.Sql Client" %>
<script runat = "server">
void Page_Load(Object Source, EventArgs E)
{
//populate from account
if (!IsPostBack){
SqlConnection conPubs;
SqlCommand cmdSelect;
SqlDataReader dtrResults;
conPubs = new SqlConnection("connect line" );
cmdSelect = new SqlCommand( "select statement", conPubs);
try {
conPubs.Open();
IDataReader reader = cmdSelect.ExecuteReader ();
dgHistory.DataSource = reader;
dgHistory.DataBind();
}
finally { conPubs.Dispose(); }
}
}
void Click_Cancel (Object s, EventArgs E) {
/*
Code here to access to checked the column cancel and see if it is checked, if it is checked then display the amount
for (i = 0; i < dgTransfer.Column.Count(), ++i) {
if (dgTransfer.Column [i].Cancel.isChecked == true)
Response.Write (dgTransfer.Column [i].Amount.ToString());
}
*/
}
</script>
<html><body>
<form runat = "server">
<asp:datagrid id = dgHistory runat = "server" AutoGenerateColumns = "false">
<Columns>
<asp:EditCommandColumn UpdateText="Update"/>
<asp:BoundColumn HeaderText = "Transaction Number" DataField = "transaction_number" ReadOnly= "true"/>
<asp:BoundColumn HeaderText = "Date Transfer" DataField = "date_transfer" ReadOnly= "true"/>
<asp:TemplateColumn HeaderText="Amount" ItemStyle-HorizontalAlign= "Right">
<ItemTemplate>
<%#DataBinder.Eval(Contain er.DataIte m,"Amount" ,"{0:C}")% >
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn Headertext = "Cancel">
<ItemTemplate>
<asp:CheckBox id = "cancel" runat="server"/>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:datagrid>
<asp:Button id = "btnCancel" OnClick = "Click_Cancel" text = "Cancel " runat = "server" />
</form>
</body>
</html>
<%@ Page Language="C#" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.Sql
<script runat = "server">
void Page_Load(Object Source, EventArgs E)
{
//populate from account
if (!IsPostBack){
SqlConnection conPubs;
SqlCommand cmdSelect;
SqlDataReader dtrResults;
conPubs = new SqlConnection("connect line" );
cmdSelect = new SqlCommand( "select statement", conPubs);
try {
conPubs.Open();
IDataReader reader = cmdSelect.ExecuteReader ();
dgHistory.DataSource = reader;
dgHistory.DataBind();
}
finally { conPubs.Dispose(); }
}
}
void Click_Cancel (Object s, EventArgs E) {
/*
Code here to access to checked the column cancel and see if it is checked, if it is checked then display the amount
for (i = 0; i < dgTransfer.Column.Count(),
if (dgTransfer.Column [i].Cancel.isChecked == true)
Response.Write (dgTransfer.Column [i].Amount.ToString());
}
*/
}
</script>
<html><body>
<form runat = "server">
<asp:datagrid id = dgHistory runat = "server" AutoGenerateColumns = "false">
<Columns>
<asp:EditCommandColumn UpdateText="Update"/>
<asp:BoundColumn HeaderText = "Transaction Number" DataField = "transaction_number" ReadOnly= "true"/>
<asp:BoundColumn HeaderText = "Date Transfer" DataField = "date_transfer" ReadOnly= "true"/>
<asp:TemplateColumn HeaderText="Amount" ItemStyle-HorizontalAlign=
<ItemTemplate>
<%#DataBinder.Eval(Contain
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn Headertext = "Cancel">
<ItemTemplate>
<asp:CheckBox id = "cancel" runat="server"/>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:datagrid>
<asp:Button id = "btnCancel" OnClick = "Click_Cancel" text = "Cancel " runat = "server" />
</form>
</body>
</html>
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Hi qweks,
//This is your code:
for (i = 0; i < dgTransfer.Column.Count(), ++i) {
if (dgTransfer.Column [i].Cancel.isChecked == true)
Response.Write (dgTransfer.Column [i].Amount.ToString());
}
1- Column is to move vertical, so there is no column which contains checkbox and "Amount" data. OK, but that's row. And in DataGrid, we has item which is same as row
So,...
//You should use to this instead
Before this code, you should check to see if there is any data in Grid or it may throw Exception.
for(int i=0;i<dgTransfer.Items.Cou nt;i++)
{
if(((CheckBox)dgTransfer.I tems[i].Fi ndControl( "cancel")) .Checked== true)
{
//Do something here }
}
//This is your code:
for (i = 0; i < dgTransfer.Column.Count(),
if (dgTransfer.Column [i].Cancel.isChecked == true)
Response.Write (dgTransfer.Column [i].Amount.ToString());
}
1- Column is to move vertical, so there is no column which contains checkbox and "Amount" data. OK, but that's row. And in DataGrid, we has item which is same as row
So,...
//You should use to this instead
Before this code, you should check to see if there is any data in Grid or it may throw Exception.
for(int i=0;i<dgTransfer.Items.Cou
{
if(((CheckBox)dgTransfer.I
{
//Do something here }
}
CheckBox myCheckBox = (CheckBox) (dgTransfer.FindControl("c
Response.Write(myCheckBox.
HTH