【问题标题】:Gridview RowDataBound dont work when I stay in the same page and click the search button当我留在同一页面并单击搜索按钮时,Gridview RowDataBound 不起作用
【发布时间】:2018-10-18 01:54:49
【问题描述】:

作为标题 我有一些关于在 RowDataBound 中添加 HyperLink 和 Highlight 关键字的代码。 它在页面更改时工作,除了停留在同一页面(单击 searchbtn)。 条件是 HyperLink 将消失,但突出显示正常工作。 我尝试在 Rowcreated() 中写入,如果这样做,不仅超链接而且内容都会消失。我该如何解决这个问题?谢谢。

这里有一些代码

 protected override void LoadViewState(object savedState)
    {
        if (savedState != null)
        {
            object[] myState = (object[])savedState;
            if (myState[0] != null)
            {
                base.LoadViewState(myState[0]);
            }
            if (myState[1] != null)
            {
                SqlDataSource1.SelectCommand = Convert.ToString(myState[1]);
            }
        }

    }

    protected override object SaveViewState()
    {
        object baseSate = base.SaveViewState();
        object[] myState = new object[2];
        myState[0] = baseSate;
        myState[1] = SqlDataSource1.SelectCommand;
        return myState;
    }

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)

        {

            string[] keywords = tbKeyWords.Text.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries);

            this.keywords = keywords.ToList();

            switch (DocRange.SelectedItem.Value)
            {
                case "all":
                    foreach (string item in this.keywords)
                    {
                        e.Row.Cells[1].Text = e.Row.Cells[1].Text.Replace(item, "<span style='background:#FF0;'>" + item + "</span>");

                        e.Row.Cells[2].Text = e.Row.Cells[2].Text.Replace(item, "<span style='background:#FF0;'>" + item + "</span>");

                    }
                    break;
                case "parsecontent":
                    foreach (string item in this.keywords)
                    {
                        e.Row.Cells[2].Text = e.Row.Cells[2].Text.Replace(item, "<span style='background:#FF0;'>" + item + "</span>");
                    }
                    break;
                case "filename":
                    foreach (string item in this.keywords)
                    {
                        e.Row.Cells[1].Text = e.Row.Cells[1].Text.Replace(item, "<span style='background:#FF0;'>" + item + "</span>");
                    }
                    break;
            }

            HyperLink Hyper = new HyperLink();

            Hyper.Text = e.Row.Cells[1].Text;

            Hyper.NavigateUrl = "" + e.Row.Cells[1].Text;

            e.Row.Cells[1].Controls.Add(Hyper);

        }
    }

【问题讨论】:

  • 欢迎来到stackoverflow。如果您发布一个可重现的最小示例,以便其他用户可以试验您的代码,您更有可能获得有用的答案。
  • 你会展示你的代码吗?所以,对其他人的建议会有所帮助。

标签: c# asp.net gridview rowdatabound


【解决方案1】:

您是否可以先在 Gridview 控件中添加 HyperLink,如下所示。

<asp:TemplateField>
  <ItemTemplate>
    <asp:HyperLink runat="server" ID="myHyperLink"></asp:HyperLink>
  </ItemTemplate>
</asp:TemplateField>

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if(e.Row.RowType == DataControlRowType.DataRow)
    {
        HyperLink myHyperLink = e.Row.FindControl("myHyperLink") as HyperLink;
        myHyperLink.Text = e.Row.Cells[1].Text;
        myHyperLink.NavigateURL = e.Row.Cells[1].Text;
    }
}

【讨论】:

  • 我使用断点调试,我发现点击btnsearch时它不会运行RowDataBound(在同一页面),但更改搜索限制(自动回发调用btnsearch)可以工作
猜你喜欢
  • 1970-01-01
  • 2018-06-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-08-21
  • 2017-04-27
相关资源
最近更新 更多