本人是初学者,现在编写一个数据库查询的页面。先说明如下:
1、有两个页面a.aspx和b.aspx;
2、a.aspx和b.aspx中各放有一个GridView控件;
3、a.aspx页面通过GridView控件中的HyperLink提交按钮进行传值给b.aspx页面;语句如下:
……
<asp:TemplateField HeaderText="浏览">
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" OnClientClick="<%# &quot;window.open('Default2.aspx?id_1=&quot; + Eval(&quot;DP_001&quot;) + &quot;&id_2=&quot; + Eval(&quot;DP_002&quot;) + &quot;')&quot; %>" runat="server">浏览</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
……
4、b.aspx页面中的GridView控件将根据传值参数id_2进行数据库条件查询。语句如下:
public partial class Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string text_1 = Request.QueryString["id_1"];
        string text_2 = Request.QueryString["id_2"];
        TextBox1.Text=text_1;
        TextBox2.Text=text_2;
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        //实例化SqlConnection对象
        SqlConnection sqlCon = new SqlConnection();
        //实例化SqlConnection对象连接数据库的字符串
        sqlCon.ConnectionString = "server=HEWUHEN;uid=sa;pwd=;database=db_Student";        //定义SQL语句
        string SqlStr_1 = "select * from tb_Drawing_ML where DML_001='TextBox2.Text'";
        //实例化SqlDataAdapter对象
        SqlDataAdapter da_1 = new SqlDataAdapter(SqlStr_1, sqlCon);
        //实例化数据集DataSet
        DataSet ds_1 = new DataSet();
        da_1.Fill(ds_1, "tb_Drawing_ML");
        //绑定DataList控件
        GridView1.DataSource = ds_1;//设置数据源,用于填充控件中的项的值列表
        GridView1.DataBind();//将控件及其所有子控件绑定到指定的数据源
        da_1.Dispose();    }
}请问各位大侠,以上程序中什么地方错了。我调试了几次都过不去。谢谢!
另外,有什么好的方法,望各位指教!

解决方案 »

  1.   

    你在a.aspx里给id_1和id_2赋值了吗?
      

  2.   

    用CommandName
    <asp:TemplateField HeaderText="浏览"> 
    <ItemTemplate> 
    <asp:LinkButton ID="LinkButton1" CommandName="open" runat="server">浏览 </asp:LinkButton> 
    </ItemTemplate> 
    </asp:TemplateField>后台gridview rowcommand 
    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e) 
        { 
    if(e.CommandName == "open") 

    //find id1 id2 here
    string id_1 = "";
    string id_2 = "";
    string script = string.Format("window.open('Default2.aspx?id_1={0}&id_2={1}');",id_1, id_2 );
    ClientScript.RegisterStartupScript(GetType(), "open", script, true);


      

  3.   


     <ItemTemplate>
         <a href='<%# "javascript:window.open(\"a.aspx?ID=" + Eval("ID")+ "\",null,\"width=300,height=200\");void 0;" %>'><%# Eval("Name")%></a>
     </ItemTemplate>
      

  4.   

    在B页面中Page_Load加载中使用 ispostback 判断下
      

  5.   

    http://blog.csdn.net/xianfajushi/archive/2009/11/02/4581138.aspx
      

  6.   

    谢谢各位的建议,我已经调试成功了。
    我在其中增加了一个传值数据的判断if (TextBox2.Text != null){},代码如下:
            if (!IsPostBack)
            {
                //实例化SqlConnection对象
                SqlConnection sqlCon = new SqlConnection();
                //实例化SqlConnection对象连接数据库的字符串
                sqlCon.ConnectionString = "server=HEWUHEN;uid=sa;pwd=;database=db_Student";            if (TextBox2.Text != null)
                {
                    string sqlStr = "Server=HEWUHEN;DataBase=db_Student;User id=sa;Pwd=";
                    SqlConnection con = new SqlConnection(sqlStr);
                    con.Open();
                    string cmdStr = "select * from tb_Drawing_P where DP_002='" + TextBox2.Text + "'";
                    SqlCommand cmd = new SqlCommand(cmdStr, con);
                    SqlDataReader dr = cmd.ExecuteReader();
                    //循环将数据添加到列表控件中
                    if (dr.Read())
                    {
                        TextBox3.Text=dr.GetString(dr.GetOrdinal("DP_003"));
                        TextBox4.Text = dr.GetString(dr.GetOrdinal("DP_004"));
                        TextBox5.Text = dr.GetString(dr.GetOrdinal("DP_005"));
                        TextBox6.Text = dr.GetString(dr.GetOrdinal("DP_006"));
                        TextBox7.Text = dr.GetString(dr.GetOrdinal("DP_007"));
                        TextBox8.Text = dr.GetString(dr.GetOrdinal("DP_008"));
                        TextBox9.Text = dr.GetString(dr.GetOrdinal("DP_009"));
                        TextBox10.Text = dr.GetString(dr.GetOrdinal("DP_010"));
                        TextBox11.Text = dr.GetString(dr.GetOrdinal("DP_011"));
                        TextBox12.Text = dr.GetString(dr.GetOrdinal("DP_012"));
                        TextBox13.Text = dr.GetString(dr.GetOrdinal("DP_013"));
                    }
                    //关闭数据库
                    con.Close(); 
                    //定义SQL语句
                    string SqlStr_1 = "select * from tb_Drawing_ML where DML_001='" + TextBox2.Text + "' ORDER BY DML_002 ASC";
                    //实例化SqlDataAdapter对象
                    SqlDataAdapter da_1 = new SqlDataAdapter(SqlStr_1, sqlCon);
                    //实例化数据集DataSet
                  DataSet ds_1 = new DataSet();
                  da_1.Fill(ds_1, "tb_Drawing_ML");
                    //绑定DataList控件
                    GridView1.DataSource = ds_1;//设置数据源,用于填充控件中的项的值列表
                    GridView1.DataBind();//将控件及其所有子控件绑定到指定的数据源
                    da_1.Dispose();
                  sqlCon.Close();
                }
            }