Click here to Skip to main content
15,887,175 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi,
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:
C#
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());
            }
            else
                Response.Redirect("SearchResult.aspx?SearchKey=Error for loading page...");
        }

When I am loading Search.aspx page:
C#
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(lnkSearch);
                    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
Comments
Just debug the code and find out what exactly happening after the button click.
Nitesh Kejriwal 9-Feb-13 12:11pm    
Check if the event is actually being fired or not.
Richard C Bishop 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

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 http://www.4guysfromrolla.com/articles/092904-1.aspx[^] for some more information.
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900