从母版页访问方法按钮,并在子级上使用onclick事件



问题1

我正在尝试使用主页面子页中的搜索按钮中的Onclick事件。我在这个网站上找到了一个很好的解决方案,但它不起作用。

链接到它:如何处理内容页中的母版页按钮事件?

与该解决方案的不同之处在于,我的IpageInterface位于App_Code文件夹中,并且位于命名空间Business中。

除此之外,情况基本相同。

所以我有IpageInterface类:

namespace Business {
    public interface IpageInterface {
        void DoSomeAction();
    }
}

然后在我的主页上我有:

using Business;
public partial class site : System.Web.UI.MasterPage {
    protected void SearchQuery_Click(object sender, EventArgs e) {
        IpageInterface pageInterface = Page as IpageInterface;
        if (pageInterface != null) {
            pageInterface.DoSomeAction();
        }
    }            
}

最后,在我想使用的页面上(结果页面,因为按钮是搜索按钮(:

using Business;
public partial class results : System.Web.UI.Page, IpageInterface {
public void DoSomeAction() {
    ContentPlaceHolder mainContent = (ContentPlaceHolder)Master.FindControl("mainContent");
    if (mainContent != null) {
        DropDownList ddlSearchOn = (DropDownList)mainContent.FindControl("ddlSearchOn");
        TextBox TxtSearch = (TextBox)mainContent.FindControl("TxtSearch");
        if (ddlSearchOn.Text == "Everything" || ddlSearchOn.Text == "Title" || ddlSearchOn.Text == "Text") {
            List<News> newsSearchResults = News.SearchNews(ddlSearchOn.Text, TxtSearch.Text);
            foreach(News newsArticle in newsSearchResults) {
                newsArticle.Text = LimitCharacters(newsArticle.Text, 350);
            }
            repeaterSearchResults.DataSource = newsSearchResults;
            repeaterSearchResults.DataBind();
        }
    }
}

但当我按下按钮,看到pageInterface的值时,它说它为null。

问题2

编辑:问题2已解决。我在"外键关系"窗口中选择了删除规则旁边的"级联"。

除了这个问题之外,我还有MS SQL的第二个问题,如下所示:我有带评论的新闻文章,当我想删除文章时,我也必须删除评论,所以我使用以下查询:

SqlCommand cmd = new SqlCommand(@"DELETE FROM News_comments WHERE News_comments.news_Id = @news_Id AND News_comments.account_Id = @account_Id;
                                              DELETE FROM News_news WHERE News_news.news_Id = @news_Id"
                                              , conn);

然而,问题是,这篇文章似乎仍然有评论,即使评论被删除了。所以我不能删除这篇文章。例外情况是:

"The DELETE statement conflicted with the REFERENCE constraint" exception.

非常感谢您的帮助!

这并不是我在上面代码中遇到的问题的真正解决方案,但它是我的情况的解决方案。

我只是通过url将需要的信息提供给子页面。这不是个人或受限数据,所以这不是问题。

最新更新