为什么直播器项目命令不会发射,即使我没有通过回复重新绑定?

时间:2015-08-20 04:43:59

标签: c# asp.net repeater itemcommand

作为你看到的标题,我遇到了触发itemcommand的问题(没有任何反应)。 在evry之前,让我告诉你我看看所有论坛和stackoverflow这样的问题:

Repeater ItemCommand Doesn't fire

ItemCommand of Repeater is not fire with LinkButton

但是我仍然有问题,所以请只是看看我的代码并指导我。

我的ASPX代码是:

<div class="span4">
                    <aside class="left-sidebar">
                        <div class="widget">
                            <div class="input-append">
                                <input class="span2" id="SearchPhrase" type="text" placeholder="Type a Phrase" maxlength="20" runat="server" />
                                <asp:Button ID="AppendedInputButton" runat="server" class="btn btn-color" OnClick="AppendedInputButton_Click" Text="Search" />
                            </div>
                        </div>
                        <div class="widget">
                            <h5 class="widgetheading">Categories</h5>
                            <ul class="cat">
                                <asp:Repeater ID="RPTCategories" runat="server" DataSourceID="SDSCategories">
                                    <ItemTemplate>
                                        <li><i class='icon-angle-right'></i>&nbsp
                                            <asp:HyperLink ID="HLCategories" runat="server" NavigateUrl='<%# string.Format("~/En/News/{0}/{1}",Eval("CategoryId"),Eval("CategoryName").ToString().Replace(" ","-")) %>'><%#Eval("CategoryName")%></asp:HyperLink>
                                            <sub>&nbsp(<%# Eval("PostsCount") %>)&nbsp</sub></li>
                                    </ItemTemplate>
                                </asp:Repeater>
                                <asp:SqlDataSource ID="SDSCategories" runat="server" SelectCommand="SELECT C.CategoryId,CategoryName,COUNT(P.CategoryId) as PostsCount FROM TblCategory C left join TblPost P ON C.CategoryId=P.CategoryId AND C.Deleted <> 'True' AND P.Deleted <> 'True' GROUP BY CategoryName,C.CategoryId ORDER BY CategoryName ASC" ConnectionString="<%$ ConnectionStrings:ConnectionCS %>"></asp:SqlDataSource>
                            </ul>
                        </div>
                        <div class="widget">
                            <h5 class="widgetheading">Recent posts</h5>
                            <ul class="cat">
                                <asp:Repeater ID="RPTRecentPosts" runat="server" DataSourceID="SDSRecentPosts">
                                    <ItemTemplate>
                                        <li><i class='icon-angle-right'></i>&nbsp
                                            <asp:HyperLink ID="HLRecentPosts" runat="server" NavigateUrl='<%# string.Format("~/En/Post/{0}/{1}",Eval("PostId"),Eval("Title").ToString().Replace(" ","-")) %>'><%#Eval("Title")%></asp:HyperLink>
                                            <sub>&nbsp(<%# Eval("SubmitDate") %>)&nbsp</sub></li>
                                    </ItemTemplate>
                                </asp:Repeater>
                                <asp:SqlDataSource ID="SDSRecentPosts" runat="server" SelectCommand="SELECT TOP 5 PostId,Title,SubmitDate FROM TblPost Order BY PostId DESC" ConnectionString="<%$ ConnectionStrings:ConnectionCS %>"></asp:SqlDataSource>
                            </ul>
                        </div>
                    </aside>
                </div>

<div class="span8">
      <asp:Repeater ID="RPTPosts" runat="server">
      <ItemTemplate>
       <br />
       <article>
       <div class="row">
       <div class="span8">
       <div class="post-heading">
       <h3>
       <asp:HyperLink ID="HLPostTitle" runat="server" NavigateUrl='<%# string.Format("~/En/Post/{0}/{1}",Eval("PostId"),Eval("Title").ToString().Replace(" ","-")) %>'><%#Eval("Title")%></asp:HyperLink>
       </h3>
       </div>

       <div class="post-image">
       <asp:Image ID="IMGPostImage" runat="server" AlternateText='<%#Eval("Title") %>' ImageAlign="AbsMiddle" ImageUrl='<%# "~/Image/Blogs/"+ Eval("ImageName")%>' />
       </div>
       <div class="meta-post">
       <a href="#" class="author">By<br />Admin</a>
       <asp:HyperLink ID="HLPostDate" CssClass="date" runat="server" NavigateUrl='<%# string.Format("~/En/News/{0}",Eval("SubmitDate").ToString().Replace("/","-"))%>'><%# Eval("SubmitDate", "{0:d/MMMM}") %><br />
       <%# Eval("SubmitTime") %></asp:HyperLink>
       <br />
       <i class="icon-tags"></i>
       <p class="keywords" runat="server" id="KeyWords"><%# Eval("Kewords").ToString().Replace(","," - ")%></p>
       </div>

       <div class="post-entry">
       <div class="subtext">
       <%# Eval("MainText").ToString() %>
       </div>
       <br />
       <asp:HyperLink ID="PostLink" runat="server" CssClass="read-more" NavigateUrl='<%# string.Format("~/En/Post/{0}/{1}",Eval("PostId"),Eval("Title").ToString().Replace(" ","-")) %>'>Read more about&nbsp<%#Eval("Title")%></asp:HyperLink>
        </div>
        </div>
        </div>
        </article>
        <br />
        </ItemTemplate>
        </asp:Repeater>

      <div id="pagination">
      <span class="all" runat="server" id="CurrentPage">Total Pages</span>
      <asp:Repeater ID="RPTPaging" runat="server" Visible="false" OnItemDataBound="RPTPaging_ItemDataBound" OnItemCommand="RPTPaging_ItemCommand" >
      <ItemTemplate>
      <asp:LinkButton ID="BtnPage" CssClass="inactive" CommandName="Page"  CommandArgument="<%# Container.DataItem %>" runat="server" Text="<%# Container.DataItem %>"></asp:LinkButton>
       </ItemTemplate>
       </asp:Repeater>
       </div>

这里我的C#代码IS:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Microsoft.AspNet.FriendlyUrls;

namespace KamSln.En
{
    public partial class News : System.Web.UI.Page
    {
        DBQuerys DBQ = new DBQuerys();
        Funcs Fn = new Funcs();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                NewsPagedData();
            }
        }
        protected void AppendedInputButton_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/En/Search/" + SearchPhrase.Value.ToString());
        }
        protected void RPTPaging_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            ViewState["PageNumber"] = Convert.ToInt32(e.CommandArgument);
            NewsPagedData();
        }
        public int CurrentPageNumber
        {
            get
            {
                if (ViewState["PageNumber"] != null)
                {
                    return Convert.ToInt32(ViewState["PageNumber"]);
                }
                else
                {
                    return 1;
                }
            }
            set
            {
                ViewState["PageNumber"] = value;
            }
        }
        private void NewsPagedData()
        {
            DataTable dt = GetNews();
            DataView dv = new DataView(dt);
            PagedDataSource pagedItems = new PagedDataSource();
            pagedItems.DataSource = dv;
            pagedItems.AllowPaging = true;
            pagedItems.PageSize = 1;
            pagedItems.CurrentPageIndex = CurrentPageNumber - 1;
            ViewState["PageNumber"] = CurrentPageNumber;

            int rowsCount = dv.Table.Rows.Count;
            int totalPages = rowsCount / 1;
            if (totalPages > 1)
            {
                RPTPaging.Visible = true;
                CurrentPage.Visible = true;
                System.Collections.ArrayList pages = new System.Collections.ArrayList();
                for (int i = 0; i < totalPages; i++)
                    pages.Add((i + 1).ToString());
                RPTPaging.DataSource = pages;
                RPTPaging.DataBind();

            }
            else
            {
                RPTPaging.Visible = false;
                CurrentPage.Visible = false;
            }
            CurrentPage.InnerText = "Page " + CurrentPageNumber + " of " + totalPages;
            RPTPosts.DataSource = pagedItems;
            RPTPosts.DataBind();
        }
        protected void RPTPaging_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            LinkButton lb = (LinkButton)e.Item.FindControl("BtnPage");
            if (!Fn.ValidityCheck(lb.Text, "MightNull") && ViewState["PageNumber"] != null)
                if (ViewState["PageNumber"].ToString() == lb.Text)
                    lb.CssClass = "current";

        }
        private DataTable GetNews()
        {
            DataTable NewsTable = new DataTable();
            NewsTable = DBQ.PostsList(null, 'A', null, null);
            if (Page.RouteData.Values.Count > 0)
            {
                string FRdatta = Page.RouteData.Values.Keys.First().ToString();
                if (Page.RouteData.Values[FRdatta].ToString() != "En")
                {
                    switch (Page.RouteData.Values.Keys.First())
                    {
                        case "ci": NewsTable = DBQ.PostsList(Page.RouteData.Values["ci"].ToString(), 'C', null, null); break;
                        case "pd": NewsTable = DBQ.PostsList(null, 'D', Page.RouteData.Values["pd"].ToString(), null); break;
                        case "sf": NewsTable = DBQ.PostsList(null, 'T', null, Page.RouteData.Values["sf"].ToString());
                            foreach (DataRow row in NewsTable.Rows)
                            {
                                row["Title"] = row["Title"].ToString().Replace(Page.RouteData.Values["sf"].ToString(), "<span style='color:blue;font-weight:bold;text-decoration:underline;'>" + Page.RouteData.Values["sf"].ToString() + "</span>");
                                row["MainText"] = row["MainText"].ToString().Replace(Page.RouteData.Values["sf"].ToString(), "<span style='color:blue;font-weight:bold;text-decoration:underline;'>" + Page.RouteData.Values["sf"].ToString() + "</span>");
                            }
                            break;
                        default: NewsTable = DBQ.PostsList(null, 'A', null, null); break;
                    }
                }
            }
            return NewsTable;
        }




    }
}

是 - 不火与 - LinkBut​​ton的

2 个答案:

答案 0 :(得分:1)

尝试使用LinkBut​​ton的点击事件,而不是OnItemCommand。您可以使用Click Event中的以下代码访问LinkBut​​ton。

LinkButton lb = (LinkButton)Sender;

OR

LinkButton lb = Sender as LinkButton;

答案 1 :(得分:1)

亲爱的专家2天后我终于找出了为什么我的页面没有回复。在我尝试用java脚本手动回发我的按钮后,我失败了。我试图跟踪脚本,我发现这个错误与firebug:

UIImage imageOn = UIImage.FromBundle(iconOn).ImageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal);

 button.SetImage(imageOn, UIControlState.Normal);

当我谷歌时,我发现我的主页中有一个按钮,ID =&#34;提交&#34;并且作为&#34;提交不是一个功能&#34;表示您将提交按钮或其他元素命名为提交。将按钮重命名为btnSubmit,您的通话将神奇地工作。

当您将按钮命名为submit时,您将覆盖表单上的submit()函数。

所以这个问题已经解决了。谢谢大家。