Reach
Reach

Reputation: 1

Gridview RowDataBound dont work when I stay in the same page and click the search button

As title I have some code about add HyperLink and Highlight keywords in RowDataBound. It works when Page changes except stays in same page(click searchbtn). The condiction is HyperLink will disapper but highlight work normally. I try to write in Rowcreated(),if I do this the not only hyperlink but also content will disapper.How can I fix this problem? thanks.

Here's some code

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

        }
    }

Upvotes: 0

Views: 155

Answers (1)

Mike
Mike

Reputation: 721

Would you try by adding HyperLink inside Gridview Control first as below.

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

Upvotes: 0

Related Questions