做了个留言搜索,搜索和结果页关键代码如下<table align="center">
<tr align="center"><td>关键词</td><td>搜索条件</td><td>&nbsp;</td></tr>
<tr align="center">
    <td><asp:TextBox ID="TextBox1" runat="server" /></td>
        <td><asp:DropDownList ID="DropDownList1" runat="server" >
            <asp:ListItem Selected="True" Value="Title">标题</asp:ListItem>
            <asp:ListItem Value="Content">内容</asp:ListItem>
            <asp:ListItem Value="UserName">作者</asp:ListItem>
            <asp:ListItem Value="GuestDate">时间</asp:ListItem>
        </asp:DropDownList></td>
        <td><asp:Button ID="Button1" runat="server" Text="搜索留言" OnClick="Button1_Click" /></td>
    </tr>
</table>protected void Button1_Click(object sender, EventArgs e)
    {
        if (DropDownList1.SelectedValue == "Title")
        {
            Response.Redirect("GuestResult.aspx?Search=GuestBook&drop=Title&words=" + TextBox1.Text );
        }
        else if (DropDownList1.SelectedValue == "Content")
        {
            Response.Redirect("GuestResult.aspx?Search=GuestBook&drop=Content&words=" + TextBox1.Text);
        }
        else if (DropDownList1.SelectedValue == "UserName")
        {
            Response.Redirect("GuestResult.aspx?Search=GuestBook&drop=UserName&words=" + TextBox1.Text);
        }
        else
        {
            Response.Redirect("GuestResult.aspx?Search=GuestBook&drop=GuestDate&words=" + TextBox1.Text);
        }
    }
结果页protected void Page_Load(object sender, EventArgs e)
    {     
        string drop = Request.QueryString["drop"].ToString();
        string words = Request.QueryString["words"].ToString();
          …………………………
        string strsql = "select * from GuestBook where IsChecked=1 and " + drop + " like '%" + words + "%' and IsNotify=0 order by IsTop desc,GuestDate desc";
……………………
做了分页,里面有可以到自定义页面去的(TextBox)protected void Button1_Click(object sender, EventArgs e)
    {
        string drop = Request.QueryString["drop"].ToString();
        string words = Request.QueryString["words"].ToString();
        Response.Redirect("GuestBook.aspx?drop=" + drop + "&words=" + words + "&Page=" + TextBox1.Text);
    }
string drop = Request.QueryString["drop"].ToString();
        string words = Request.QueryString["words"].ToString();
这两个变量本来没有放在Button1_Click方法下,我觉得应该不用
但是不放的话页面就在这里报错说drop没有被实例化
于是才从 Page_Load里粘了过来,这样页面才算过去了
页面调用,不是首先经过 Page_Load方法吗?
这样想,两个变量均被赋值(实例化)了
为何在这里却直接报Button1_Click方法下Response.Redirect("GuestBook.aspx?drop=" + drop + "&words=" + words + "&Page=" + TextBox1.Text);
里的变量没有被实例化?

解决方案 »

  1.   

    string drop;string words;
    放在Page_load下只能在Page_load事件中使用,
    应该放到Page_load的外面.如:string drop;
    string words;
    protected void Page_Load(object sender, EventArgs e)
    {  
            drop = Request.QueryString["drop"].ToString();
            words = Request.QueryString["words"].ToString();
            ..............
    }
      

  2.   


    string drop="";
    string words="";