无法访问DataList中的checkbox checkedchanged事件

时间:2013-04-09 17:37:50

标签: asp.net checkbox datalist

我在datalist itemtemplate中有复选框..但是我无法访问checkbox chechedchange事件。我将AutoPostBack设置为true。但仍然无法开火。 这是我的代码。

<ul class="commentlist" >
                    <asp:DataList ID="datalistYorum" runat="server" DataSourceID="ods_yorumlar"  
                            RepeatLayout="Flow" ItemStyle-Wrap="True" RepeatDirection="Horizontal" 
                            onitemcreated="datalistYorum_ItemCreated" 
                            onitemdatabound="datalistYorum_ItemDataBound" onload="datalistYorum_Load" 
                            ondatabinding="datalistYorum_DataBinding">
                      <ItemTemplate>
                        <li class="comment">
                         <div class="comment-body">
                            <div class="comment-author vcard">
                                <div class="lightbox-photo">
                                    <a class="image-overlay" href='<%# "Foto/profil/foto_buyuk/" + Eval("Yorum_Profil_Foto_Buyuk") %>' data-rel="prettyPhoto" title='<%# Eval("Yorum_UserName")%>'>
                                    <img src='<%# "Foto/profil/foto_kucuk/" + Eval("Yorum_Profil_Foto_Kucuk") %>' alt='<%# Eval("Yorum_UserName")%>' class="avatar" />
                                    </a>
                                 </div>
                                 <cite class="fn"><asp:HyperLink ID="linkProfil" runat="server" Text='<%# Eval("Yorum_UserName")%>' NavigateUrl='<%# "~/profil.aspx?user_id="+ Eval("User_ID") %>'></asp:HyperLink></cite>
                                 <cite class="fn-time"><%# Eval("Yorum_Gecen_Zaman")%></cite> 
                            </div>
                            <p><%# Eval("Yorum_Text")%></p>
                             <div class="reply"><asp:CheckBox ID="checkLike" runat="server" CssClass="comment-reply-link"   AutoPostBack="True" />
                                 <asp:ToggleButtonExtender ID="ToggleButtonLike" runat="server" TargetControlID ="checkLike" ImageHeight="32" ImageWidth="52" CheckedImageUrl="~/images/liked.png" UncheckedImageUrl="~/images/like.png" CheckedImageAlternateText="Like">
                                 </asp:ToggleButtonExtender>
                             </div>
                    <div class="reply"><asp:CheckBox ID="checkDislike" runat="server" CssClass="comment-reply-link"  AutoPostBack="True" />
                     <asp:ToggleButtonExtender ID="ToggleButtonDislike" runat="server" TargetControlID="checkDislike" ImageHeight="32" ImageWidth="62" UncheckedImageUrl="~/images/dislike.png" CheckedImageUrl="~/images/disliked.png">
                     </asp:ToggleButtonExtender>
                     </div>
                      </div>
                       </li>
                        </ItemTemplate>

                        </asp:DataList>
                       <asp:ObjectDataSource ID="ods_yorumlar" runat="server" 
                            DataObjectTypeName="Yorum" TypeName="yonet" SelectMethod="PostYorumlariGetir" 
                            ondatabinding="ods_yorumlar_DataBinding" 
                            onselecting="ods_yorumlar_Selecting" onselected="ods_yorumlar_Selected">
                       <SelectParameters>

和代码背后:

   protected void datalistYorum_ItemDataBound(object sender, DataListItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType==ListItemType.AlternatingItem)
        {
            CheckBox checkLike = (CheckBox)e.Item.FindControl("checkLike");
            CheckBox checkDislike = (CheckBox)e.Item.FindControl("checkDislike");

            if (!Page.IsPostBack)
            {
                checkLike.CheckedChanged += new EventHandler(checkLike_CheckedChanged);
                checkDislike.CheckedChanged += new EventHandler(checkDislike_CheckedChanged);
            }
        }
    }

 public void checkLike_CheckedChanged(object sender, EventArgs e)
    { 
        object user_id =  Membership.GetUser().ProviderUserKey;
        DateTime event_date = DateTime.Now;
        CheckBox checkLike = (CheckBox)datalistYorum.FindControl("checkLike");
         if (checkLike.Checked == true)
        {
            try
            {
                using (SqlConnection baglanti = new SqlConnection(ConfigurationManager.ConnectionStrings["xxx"].ConnectionString.ToString()))
                {

                    SqlCommand komut = new SqlCommand("sp_likeordislike", baglanti);
                    komut.CommandType = CommandType.StoredProcedure;
                    komut.Parameters.AddWithValue("@comment",1);
                    komut.Parameters.AddWithValue("@user_id", user_id);
                    komut.Parameters.AddWithValue("@likeordislike", 1);
                    komut.Parameters.AddWithValue("@event_date", event_date);
                    baglanti.Open();
                    komut.ExecuteNonQuery();
                    baglanti.Close();
                }
            }
            catch (Exception)
            {
                throw;
            }
        }

    }

但没有任何事情发生。谢谢!

2 个答案:

答案 0 :(得分:0)

试试这个:

protected void checkDislike_CheckedChanged(Object sender, EventArgs e)
{
    CheckBox cb = (CheckBox) sender;
    DataListItem item = (DataListItem) cb.NamingContainer;
    //
    //
}

您的标记将如下所示:

<asp:CheckBox ID="checkDislike" runat="server" CssClass="comment-reply-link"  AutoPostBack="True" OnCheckedChanged="checkDislike_CheckedChanged" />
  

在尝试此评论之前,请注释所有其他checkbox events

检查link

答案 1 :(得分:0)

除非你在Page_Load中为'checklike'复选框添加事件处理程序,否则看起来好像没有分配。

更改'checklike'复选框声明,如下所示:

<asp:CheckBox ID="checkLike" runat="server" CssClass="comment-reply-link" AutoPostBack="True" OnCheckedChanged="checkLike_CheckedChanged" />