State of CheckBox in ItemTemplate of DataGrid
Hi,
I have two seperate datagrids on the same page with checkbox as an Itemtemplate Column in each one. Both database get filled up with data from database. CheckBoxes are checked(marked)by user after rows are displayed.
After selecting rows, when user clicks a button for processing these rows a button event is raised. In this event CheckBox state is checked to find which rows are selected. Here CheckBox state remains always unchecked, eventhough some of the checkboxes are checked by user. I am using javscript to put checkmark in checkboxes. And I have marked Autopostback=false for checkbox;
Here is the code snippet:
1)javascript:
=============
<script>
<script>function check(chkB)
{
var xState=chkB.checked;
if(xState)
{
chkB.Checked = true;
}
else
{
chkB.Checked = false;
}
}
</script>
2)Html of ItemTemplate
====================
<asp:TemplateColumn>
<HeaderTemplate>
<asp:Label id="lblchkAll" runat="server" ToolTip="Select each row">SelectTo</asp:Label>
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox id="chkSelect" onclick="javascript:check(this);" runat="server" Text="Remove" ToolTip="Select/Deselect each row" AutoPostBack="False"></asp:CheckBox>
</ItemTemplate>
3)CodeBehind of Button Click Event
=================================
private void btnTrimPrv_Click(object sender, System.EventArgs e)
{
CheckBox chkBox = new CheckBox();
DataSet ds = (DataSet)Cache.Get("dsPrv");
foreach(DataGridItem dgi in this.QueryGridPrv.Items)
{
chkBox = (CheckBox)dgi.Cells[0].Controls[1];
if(chkBox != null && chkBox.Checked)
{
string strItemIndex = this.QueryGridPrv.DataKeys[dgi.ItemIndex].ToString();
ListItem li = new ListItem("ID=" + strItemIndex, strItemIndex);
if(ds.Tables.Contains(li.Value))
{
ds.Tables.Remove(li.Value);
}
}
}
ds.AcceptChanges();
this.QueryGridPrv.DataSource = ds;
this.QueryGridPrv.DataBind();
}
In above code
if(chkBox != null && chkBox.Checked)
always remains "false". Why true state of checkbox is not refleted in post back? Your suggestions will be truely appriciated.
Thank you.
Bharat Gadhia
Bharat Gadhia
__________________
Bharat Gadhia
|