Click here to Skip to main content
15,897,704 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I have two check box inside gridview.While clicking on first check box Autopostback is not happening.How to do.Please see my below code--

ASPX Page--
XML
<asp:TemplateColumn HeaderStyle-Width="60px" HeaderStyle-CssClass="gridheader">
                                <ItemStyle HorizontalAlign="Center" />
                                <HeaderTemplate>
                                    Is Omani
                                </HeaderTemplate>

                                <ItemTemplate>

                                    <asp:CheckBox ID="chkIsOmani" CssClass="CheckBox" runat="server" Checked='<%#Bind("IsOmani") %>'
                                        OnCheckedChanged="chkIsOmani_CheckedChanged" AutoPostBack="true"   />

                                </ItemTemplate>
                            </asp:TemplateColumn>
                            <asp:TemplateColumn HeaderStyle-Width="60px" HeaderStyle-CssClass="gridheader">
                                <ItemStyle HorizontalAlign="Center" />
                                <HeaderTemplate>
                                    Is SME
                                </HeaderTemplate>
                                <ItemTemplate>
                                    <asp:CheckBox ID="chkIsSME" CssClass="CheckBox" runat="server" Checked='<%#Bind("IsSME") %>' ToolTip="IS SME can be selected only if IS Omani is selected"  />
                                </ItemTemplate>
                            </asp:TemplateColumn>



.CS Page---

C#
public string CheckOmaniData()
    {
        string _Items = "";
        foreach (DataGridItem _item in dgServices.Items)
        {
            CheckBox chkIsOmani = (CheckBox)_item.FindControl("chkIsOmani");
            CheckBox chkIsSME = (CheckBox)_item.FindControl("chkIsSME");



            if (chkIsOmani.Checked)
            {

                _Items = _Items + _item.Cells[0].Text + ",";


                chkIsSME.Enabled = true;
                chkIsSME.Attributes.Add("onclick", "return true;"); // To retain the value like enabling
            }
            else
            {
                _Items = _Items + _item.Cells[0].Text + ",";

                //chkIsSME.Enabled = false;
                chkIsSME.Attributes.Add("onclick", "return false;"); //To retain original value like disabling
                chkIsSME.Checked = false;

            }


        }
        return _Items.Substring(0, _Items.Length - 1);
    }

    protected void chkIsOmani_CheckedChanged(object sender, EventArgs e)
    {

        CheckOmaniData();
    }
Posted
Updated 25-May-14 6:28am
v2
Comments
DamithSL 25-May-14 0:05am    
have you debug and check whether it hit the event chkIsOmani_CheckedChanged?
Kornfeld Eliyahu Peter 25-May-14 13:27pm    
Probably what happening is that you hit server side but because of re-initialization of the data you lost your binding so no CheckedChanged event...

1 solution

Hi,

You can use rowcommand instead of current coding.

Give CommandName to Chechbox in gridview. say "Check1"

You have to do the code on RowCommand event of gridview. check following example

C#
void ProductsGridView_RowCommand(Object sender, GridViewCommandEventArgs e)
 {
   // CommandName property to determine which button was clicked.
   if(e.CommandName=="check1")
   {
     // code on checkbox select
   }
}



Thanks,
Bh@gyesh
 
Share this answer
 
Comments
kamalsekhar 4-Jun-14 6:04am    
Hi All,
I have fixed this issue using java script.

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