简体   繁体   English

OnCheckedchange事件未触发

[英]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 我在CS文件中添加了选中的更改事件

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?) 不包含'OnChkChangd_chk_NA_FootTemp'的定义,找不到扩展方法'OnChkChangd_chk_NA_FootTemp'接受类型为'ASP.setup_saleablestock_aspx'的第一个参数的扩展方法(您是否缺少使用指令或程序集引用?)

You have binded wrong event to checkbox. 您已将错误事件绑定到复选框。

Add OnCheckedChanged="OnChkChangd_chk_NA_FootTemp_CheckedChanged" to your checkbox OnCheckedChanged="OnChkChangd_chk_NA_FootTemp_CheckedChanged"添加到您的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 OnChkChangd_chk_NA_FootTemp_CheckedChangedOnChkChangd_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" 因此,请更改复选框的事件名称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" 应该是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 您已经提到“ AutoPotsBack = true”,因此请在!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 在触发控制事件之前调用DataBind时会发生问题

Solution

You need Add ! 您需要添加 IspOstBack in inside of you page load event. 页面加载事件中的IspOstBack And you must call the grid binding code inside of !IsPostBack . 而且,您必须在!IsPostBack内部调用网格绑定代码。 Because you have used AutoPotsBack=true in your checkbox, this helps to call(reload) the page load. 因为您在复选框中使用了AutoPotsBack = true,所以这有助于调用(重新加载)页面加载。 So if you does not use the !IsPostBack, then your grid will be rebind, and your checkbox will re rendering. 因此,如果您不使用!IsPostBack,则将重新绑定网格,并且复选框将重新呈现。 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 完全不在GridView中触发OnCheckedChanged事件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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