Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
i have a web application and i uaed a search option in it. my search input box is in the maser page and i take that value to the search page by using Request.QueryString["searchkey"]; after that i wanna click a button on the search page. but when i click that the result goes away. and "Please provide a keyword for searching!" is displayed. i tried Page.Ispostback == false and !ispostback. it didn't work. can somebody analyze my code and give me a solution?? here's my code,
i just wanna keep the search key in a place that whatever happens it doesnt change..

public partial class SearchMovie : System.Web.UI.Page
{
    String Searchkey = "acc";

    protected void Page_Load(object sender, EventArgs e)
    {
        setSearchKey();
      
        if (Searchkey != null)
        {
                CreateResultTable();
        }
        else 
        {
            notifylabel.Text = "Please provide a keyword for searching!";
        }
    }

    private void setSearchKey()
    {
        String Srchkey = Request.QueryString["searchkey"];

            if (Srchkey != null || Srchkey != "")
            {
                Searchkey = Srchkey;
                Session["search"] = Searchkey;
            }
        
    }

    public void CreateResultTable() 
    {
        MoviesGenDataContext search = new MoviesGenDataContext();

        var srchreslt = from srch in search.Item_Masters
                    where SqlMethods.Like(srch.Name, "%"+ Searchkey +"%")
                    select srch;

        foreach (var rslt in srchreslt) 
        {
                TableRow row = new TableRow();
                TableCell cell1 = new TableCell();
                TableCell cell2 = new TableCell();
                TableCell cell3 = new TableCell();


                Label desc = new Label();
                LiteralControl h1start = new LiteralControl("<h1>");
                LiteralControl h1end = new LiteralControl("</h1>");
                desc.Text = rslt.Item_Desc;

                HyperLink movlk = new HyperLink();
                HyperLink play = new HyperLink();
                HyperLink book = new HyperLink();

                if (Session["email"] == null)
                {
                    book.Enabled = false;
                    notifylabel.Text = "Please log in to book the DVD(s)!";
                }
                else
                {
                    book.Enabled = true;
                }

                LiteralControl nln = new LiteralControl("<br/>");
                LiteralControl nln2 = new LiteralControl("&nbsp;");

                play.ImageUrl = "Images/playtrailer.gif";
                play.NavigateUrl = "SearchMovie.aspx?Vn=Trailers/" + rslt.Name.Trim() + ".flv";


                book.ImageUrl = "Images/bookDVD.gif";
                book.NavigateUrl = "MyReservations.aspx?Vn=" + rslt.Name.Trim() + "&Vy=" + rslt.Year + "&Vs=" + rslt.Starring.Trim();

                movlk.ImageUrl = "Posters/" + rslt.Name.Trim() + ".jpg";
                movlk.NavigateUrl = "SearchMovie.aspx?Vn=Trailers/" + rslt.Name.Trim() + ".flv";

                cell1.Controls.Add(movlk);
                cell1.Width = Unit.Pixel(214);
                cell2.Width = Unit.Pixel(200);
                cell2.Controls.Add(h1start);
                cell2.Controls.Add(desc);
                cell2.Controls.Add(h1end);
                cell2.Controls.Add(nln);

                cell3.Controls.Add(play);
                cell3.Controls.Add(nln);
                cell3.Controls.Add(book);

                row.Cells.Add(cell1);
                row.Cells.Add(cell2);
                row.Cells.Add(cell3);
                Srchresult.Rows.Add(row);

            }
        //Session["resultable"] = Srchresult; 
        }

    }
Posted

Weird case.

C#
setSearchKey();
if (Searchkey != null)
{
        CreateResultTable();
}
else
{
    notifylabel.Text = "Please provide a keyword for searching!";
}


In Page_Load method, you are processing the search through CreateResultTable method on the basis of Searchkey object.

You initialized Searchkey in the beginning with acc. In case, setSearchKey fails, you will continue to have acc; otherwise non null values from QueryString.

Either case, you should have non null values in Searchkey. Unfortunately, program execution fails the below condition
if (Searchkey != null)


and moving to notifyLabel.

One solution would be moving Searchkey into public property with auto getter and setter as:
public String Searchkey {get;set;}
 
Share this answer
 
Searchkey will never be null, as its already initialized at the top. Pls check....
 
Share this answer
 
Anuja is correct Searchkey can neva b a null, I think you should more logic sir...Now correct me if am wrong. The textbox you using to search, you put it in a masterpage because you want to search what ever value in the page matches with the value in the textbox and the value from the textbox will be inside here "Request.QueryString["searchkey"];"

if what am saying is correct,why are you running this code on Page_Load?

if (Searchkey != null)
{
CreateResultTable();
}
else
{
notifylabel.Text = "Please provide a keyword for searching!";
}
 
Share this answer
 
Hi,
You are doing completely postback(),, I would suggest you should use callBack().
 
Share this answer
 
v2

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