Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP.NET
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:
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:
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 8-Feb-13 22:30pm
Comments
Tadit Dash at 9-Feb-13 8:42am
   
Just debug the code and find out what exactly happening after the button click.
Nitesh Luharuka at 9-Feb-13 12:11pm
   
Check if the event is actually being fired or not.
richcb at 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
good
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 http://www.4guysfromrolla.com/articles/092904-1.aspx[^] for some more information.
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 420
1 CHill60 410
2 Sergey Alexandrovich Kryukov 384
3 PIEBALDconsult 235
4 BillWoodruff 190


Advertise | Privacy | Mobile
Web01 | 2.8.141015.1 | Last Updated 11 Feb 2013
Copyright © CodeProject, 1999-2014
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