Click here to Skip to main content
15,914,163 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi,
I am using check box in ItemTemplate(gridview) in User Control. I wrote some code in CheckedChanged event. It is Running. after completing the event my page is loading with uncheck checkbox.

XML
<asp:TemplateField ItemStyle-Width="4%">
                                        <ItemTemplate>
                                            <asp:CheckBox Width="10%" ID="chkDocumentName" runat="server" OnCheckedChanged="chkDocumentName_CheckedChanged" AutoPostBack="true"/>
                                        </ItemTemplate>
                                        <ItemStyle Width="3%" />
                                    </asp:TemplateField>




C#
protected void chkDocumentName_CheckedChanged(object sender, EventArgs e)
    {
        for (int i = 0; i < gvDocumentTemplatestatus.Rows.Count; i++)
        {
            CheckBox chkbox = (CheckBox)gvDocumentTemplatestatus.Rows[i].FindControl("chkDocumentName");
            if (chkbox.Checked == true)
            {
                Label lblTemplateId = (Label)gvDocumentTemplatestatus.Rows[i].FindControl("lblTempID");
                Label lblDocumentNumberID = (Label)gvDocumentTemplatestatus.Rows[i].FindControl("lblDocumentNumberID");
                 Label lblTemplateName = (Label)gvDocumentTemplatestatus.Rows[i].FindControl("lblTemplateName");
                 Session["rownumber"] = i.ToString();
                Session["TempID"] = lblTemplateId.Text;
                
            }
            else
            {
                if (Session["rownumber"] != null)
                {
                    if (Convert.ToInt32(Session["rownumber"].ToString()) == i)
                    {
                        strcolumnnumber = "0";
                        Session["SubJobID"] = strcolumnnumber;
                    }
                }
            }
        }
    }


This is my code after postback my check box is unchecked.



if any one know about this tell me.

Regards
Nanda Kishore.CH
Posted
Updated 6-Aug-12 0:49am
v3
Comments
Sangramsingh Pawar 6-Aug-12 6:48am    
check that your gridview not binds to datasource property when your checkbox checked

use update panel inside item template

i.e.
ASP.NET
<updatepanel>
<contenttemplate>
<asp:checkbox width="10%" id="chkDocumentName" runat="server" oncheckedchanged="chkDocumentName_CheckedChanged" autopostback="true" xmlns:asp="#unknown" />
</contenttemplate>
</updatepanel>


if still not working
then use asynchronous postback trigger with this updatepanel
 
Share this answer
 
v2
Comments
nandkishorre 6-Aug-12 7:05am    
Still Not working
A solution is to add a hidden field to the page that represents the state of the checkbox, then update the field's value to "ON" or "OFF" for example, whenever the checkbox is clicked.

Then on the server you check the value of the hidden field, not the checkbox itself, as the hidden field is always posted.

Happy Coding!
:)
 
Share this answer
 
hi,

This is because when postback occurs gridview recreates so we loose the previous state so it will come always unchecked so put the gridview binded code under page.ispostback

check the code where gridview is binded in page load it should be under the page.ispostback

if (!Page.IsPostBack)
{
bind gridview here
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900