简体   繁体   中英

OnCheckedchange event not firing

I add check box in grid view code sample is here

<asp:TemplateField HeaderText="N/A" SortExpression="NotAvailableQuantity">
            <ItemTemplate>
             <asp:CheckBox ID="chk_NA_ItemTemp" Enabled="false" Checked='<%# Bind("StockAvailable") %>' runat="server" />
            </ItemTemplate>
               <EditItemTemplate>
                 <asp:CheckBox ID="chk_NA_EditTemp"  runat="server" />
     </EditItemTemplate>
           <FooterTemplate>
                <asp:CheckBox ID="chk_NA_FootTemp" ClientIDMode="Static" OnCheckedChanged="OnChkChangd_chk_NA_FootTemp" AutoPostBack="true"  runat="server" />
          </FooterTemplate>
            <HeaderStyle HorizontalAlign="right" />
             <ItemStyle HorizontalAlign="right" />
          </asp:TemplateField>

i add on checked change event in CS file as

protected void OnChkChangd_chk_NA_FootTemp(object sender, EventArgs e)
        {
            CheckBox chk = (CheckBox)sender;
            GridViewRow row = (GridViewRow)chk.Parent.Parent;
            if (chk.Checked == true)
            {
                ((Label)row.FindControl("Footer_txt_AvailableQuantity")).Text = DateTime.Now.ToString();
            }
            else 
            {
            }                
        }    

but I am getting this error.

does not contain a definition for 'OnChkChangd_chk_NA_FootTemp' and no extension method 'OnChkChangd_chk_NA_FootTemp' accepting a first argument of type 'ASP.setup_saleablestock_aspx' could be found (are you missing a using directive or an assembly reference?)

You have binded wrong event to checkbox.

Add OnCheckedChanged="OnChkChangd_chk_NA_FootTemp_CheckedChanged" to your checkbox

<asp:CheckBox ID="chk_NA_FootTemp" ClientIDMode="Static" OnCheckedChanged="OnChkChangd_chk_NA_FootTemp_CheckedChanged" AutoPostBack="true"  runat="server" />

The event name is different between your markup and the code-behind:

OnChkChangd_chk_NA_FootTemp_CheckedChanged vs OnChkChangd_chk_NA_FootTemp

Fix one or the other so the names match.

Your event name is

protected void OnChkChangd_chk_NA_FootTemp_CheckedChanged(object sender, EventArgs e)

So change your event name for checkbox OnCheckedChanged="OnChkChangd_chk_NA_FootTemp_CheckedChanged"


<asp:CheckBox ID="chk_NA_FootTemp" ClientIDMode="Static" OnCheckedChanged="OnChkChangd_chk_NA_FootTemp" AutoPostBack="true"  runat="server" />

It shoud be OnCheckedChanged="OnChkChangd_chk_NA_FootTemp_CheckedChanged"

<asp:CheckBox ID="chk_NA_FootTemp" ClientIDMode="Static" OnCheckedChanged="OnChkChangd_chk_NA_FootTemp_CheckedChanged" AutoPostBack="true"  runat="server" />

you have mentioned "AutoPotsBack=true" So call your event in !IsPostBack

if (!IsPostBack) 
{
\\Grid view event
}

I saw your code, Your code was looking good, I don't know why it's not worked.

But i guess, you have missed one thing.

Problem

The problem occurs when DataBind is called before the control event is firing

Solution

You need Add ! IspOstBack in inside of you page load event. And you must call the grid binding code inside of !IsPostBack . Because you have used AutoPotsBack=true in your checkbox, this helps to call(reload) the page load. So if you does not use the !IsPostBack, then your grid will be rebind, and your checkbox will re rendering. So on change event will not fire.

So please use this way

void page_load()    
{
   if(!IsPostBack)
  {
   // call Grid view Binding code here 
  }
}

If my solution is not working, then try these below way's

OnCheckedChanged event not firing in GridView at all

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM