简体   繁体   English

为什么即使我不通过回发重新绑定,Repeater ItemCommand也不会触发?

[英]Why Repeater ItemCommand Doesn't fire, even if i don't rebind by post back?

As title you see, I Have problem with firing itemcommand(nothing happened). 如标题所示,我无法启动itemcommand(什么都没发生)。 before evry thing, let me tell you i take a look to all forums and stackoverflow questions like these : 在大惊小怪之前,让我告诉你我看一下所有这样的论坛和stackoverflow问题:

Repeater ItemCommand Doesn't fire 中继器ItemCommand不会触发

ItemCommand of Repeater is not fire with LinkButton 中继器的ItemCommand不能与LinkBut​​ton一起触发

But I Still Have Problem With this, so please just take a look to my code and guid me. 但是我仍然对此有疑问,所以请看一下我的代码并引导我。

My ASPX Code is: 我的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>

And Here My C# Code IS : 在这里,我的C#代码是:

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




    }
}

is-not-fire-with-linkbutton 不使用链接按钮触发

Try using click event of a LinkButton, instead of OnItemCommand . 尝试使用LinkBut​​ton的click事件而不是OnItemCommand You can access LinkButton using below code in Click Event. 您可以在Click Event中使用以下代码访问LinkBut​​ton。

LinkButton lb = (LinkButton)Sender;

OR 要么

LinkButton lb = Sender as LinkButton;

Dear Expert After 2 day finally i figure out why my page doesn't post back. 亲爱的专家2天后,我终于弄清了为什么我的页面不回发。 after i tried to post back my button manually with java script and i failed. 在我尝试用Java脚本手动发回按钮后,我失败了。 i tried to trace the script and i found this Error with firebug: 我试图跟踪脚本,但发现了萤火虫错误:

 TypeError: theForm.submit is not a function
     theForm.submit();

when i google it i found i have a button in my MasterPage with id="Submit" and as "submit is not a function" means that you named your submit button or some other element submit. 当我在google上搜索时,我发现我的MasterPage中有一个带有id =“ Submit”的按钮,并且因为“ submit is a function”意味着您命名了Submit按钮或其他提交元素。 Rename the button to btnSubmit and your call will magically work. 将按钮重命名为btnSubmit,您的呼叫将神奇地工作。

When you name the button submit, you override the submit() function on the form. 当您将按钮命名为Submit时,您将覆盖表单上的commit()函数。

So This Problem Resolved. 所以这个问题解决了。 Thanks to all. 谢谢大家。

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

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