0

我在数据列表项模板中有复选框..但我无法访问复选框 chechedchange 事件。我设置AutoPostBack为真。但仍然不能触发事件。这是我的代码。

<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;
            }
        }

    }

但什么也没发生。谢谢!

4

2 回答 2

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

检查此链接

于 2013-04-09T18:41:29.750 回答
0

除非您在 Page_Load 中为“checklike”复选框添加事件处理程序,否则它看起来好像缺少分配。

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

<asp:CheckBox ID="checkLike" runat="server" CssClass="comment-reply-link" AutoPostBack="True" OnCheckedChanged="checkLike_CheckedChanged" />
于 2013-04-09T21:51:42.160 回答