Click here to Skip to main content
12,820,546 members (29,418 online)
Rate this:
Please Sign up or sign in to vote.
See more: C# ASP.NET
I have a Search.aspx and SearchResult.aspx files. When Iam searching any thing in a TextBox which is exist in Search.aspx fle it return in Search.aspx file with result. In my result I get some LinkButton where I want to click to go in SearchResult.aspx file with elaborate of search result. But, I think in this case my code is not working.
My Code:
private void lnkSearch_Command(object sender, CommandEventArgs e)
            string commandName = e.CommandName;
            if (commandName.Length > 0)
                Response.Redirect("SearchResult.aspx?SearchResult=" + commandName.ToString() + "&SearchKey=" + txtSearch.Text.ToString());
                Response.Redirect("SearchResult.aspx?SearchKey=Error for loading page...");

When I am loading Search.aspx page:
if (!IsPostBack == true)
for (int i = 0; i < db.dataSet.Tables["searchResult"].Rows.Count; i++)
                    LinkButton lnkSearch = new LinkButton();
                    lnkSearch.Text = txtSearch.Text.ToString();
                    lnkSearch.ID = "lnkBtn" + i.ToString();
                    lnkSearch.EnableViewState = true;
                    //lnkSearch.AccessKey = db.dataSet.Tables["searchResult"].Rows[i][0].ToString();
                    lnkSearch.Command += new CommandEventHandler(lnkSearch_Command);
                    lnkSearch.CommandName = db.dataSet.Tables["searchResult"].Rows[i][0].ToString();
                    this.UpdatePanel1.ContentTemplateContainer.Controls.Add(new LiteralControl("<br/>"));

This working good. But, in the event of LinkButton_Command, this is not working. Cause, when I am clinking in the LinkButton it just refreshing this page with previous Query String url but not going on SearchResult.aspx file.

So, please help me. How can I solve it.
Posted 8-Feb-13 23:30pm
Tadit Dash 9-Feb-13 8:42am
Just debug the code and find out what exactly happening after the button click.
Nitesh Luharuka 9-Feb-13 12:11pm
Check if the event is actually being fired or not.
richcb 11-Feb-13 17:28pm
I would try removing the if statement in your lnksearch_command event. If you are getting the same result still, you will know that the command name is blank.

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Dynamic controls need to be created in the Init event of the Page, not the Load event.

Do not check for IsPostback as the control needs to be recreated each time the page is created so it exists when ViewState and Event information is loaded AFTER the Init event, but BEFORE the Load event.

see[^] for some more information.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

    Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170308.1 | Last Updated 11 Feb 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100