0

I have a querystring.aspx file having a gridview inside in it.

In querystring.cs file's default class that is

public partial class querystring : System.Web.UI.Page,

i can access the gridview control id.I have made a another class below this class ,but it is not accessing the gridview control id.

This is user defined class code in querystring.cs file:-

public partial class bindingmethod : System.Web.UI.Page
{

    public void show()
    {
        SqlConnection con = new SqlConnection(getconnectionstring());
        SqlCommand cmd = new SqlCommand();
        DataTable dt = new DataTable();
        cmd.Connection = con;
        con.Open();
        SqlDataAdapter adb = new SqlDataAdapter("show_answers", con);
        adb.Fill(dt);

       GridView1.DataSource = dt;

        GridView1.DataBind();
    }
    //Get a connection string to make a db connection
    public static string getconnectionstring()
    {
        return System.Configuration.ConfigurationManager.ConnectionStrings["crudconnection1"].ConnectionString;
    }
}

For GridView1 it says:

The name GridView1 does not exist in the current context.    

Please help me. Thank you.

4

3 回答 3

0

您可以简单地将 GridView 的引用传递给您的新类/方法。

从您的 querystring.cs 类:

bindingmethod b = new bindingmethod();
b.show(this.GridView1);

绑定方法.cs

public class bindingmethod 
{ 
    public void show(GridView gridView) 
    { 
        using(SqlConnection con = new SqlConnection(getconnectionstring()))
        using(SqlCommand cmd = new SqlCommand())
        {
            using(DataTable dt = new DataTable())
            {
              cmd.Connection = con; 
              con.Open(); 

              using(SqlDataAdapter adb = new SqlDataAdapter("show_answers", con))
              {
                  adb.Fill(dt); 

                  gridView.DataSource = dt; 

                  gridView.DataBind(); 
              }
           }
        }
    } 

    //Get a connection string to make a db connection 
    public static string getconnectionstring() 
    { 
        return System.Configuration.ConfigurationManager.ConnectionStrings["crudconnection1"].ConnectionString; 
    } 
} 
于 2012-09-11T11:00:05.997 回答
0

这是没有意义的,bindingmethod不是,System.Web.UI.Page因此我不知道你为什么要继承它。此外,Grid/GridViewquerystring页面的一部分,因此您只能从该页面访问它们。

我认为您想要做的是将您的show/getconnectionstring方法移到您的querystring课程中并完全摆脱bindingmethod

于 2012-09-11T10:43:04.083 回答
0

您顶部的 aspx 页面在Inherits="querystring"页面属性中有一个属性。那就是将您的 aspx 页面链接到您的代码后面的 class querystring。由于您创建了一个新类bindingmethod,因此它与aspx页面没有任何关系。您不能从多个类继承 aspx 页面,并且aspx页面中的对象GridView1以任何方式对您的新类都不可见。

我不确定你为什么要创建一个新课程。看来您只需要querystring类中的另一个方法,并且可能需要调用该方法Page_Load

于 2012-09-11T10:48:35.007 回答