使用ASP.NET中的ListView显示数据

时间:2013-10-29 09:55:06

标签: c# asp.net listview

我想在用户注册的同一网页中显示注册数据库。我可以通过使用listview配置数据库来显示它。

  

但我想通过在c#中使用add item命令来做到这一点。

另一个问题是,即使在我添加了listview (system.web.ui.webcontrols)的引用之后,当我输入listview时,Visualstudio说它的含糊不清。

我的代码在

之下
        public void Insertfunc()//inserting into database 
        {
            string database = @"Data Source=.;Initial Catalog=NewDB;Integrated Security=True";
            SqlConnection myConn = new SqlConnection(database);
            string queryStr = @"insert into Registration values (@fname,@lname,@dob,@emailid,@uname)";
            SqlCommand myCommand = new SqlCommand(queryStr, myConn);
            myCommand.Parameters.AddWithValue("@fname", FirstName);
            myCommand.Parameters.AddWithValue("@lname", LastName);
            myCommand.Parameters.AddWithValue("@dob", DateofBirth);
            myCommand.Parameters.AddWithValue("@emailid", Mailid);
            myCommand.Parameters.AddWithValue("@uname", UserName);
            myConn.Open();
            myCommand.ExecuteNonQuery();
            myConn.Close();

            string query = @"select * from Registration";
            SqlCommand mycommand1 = new SqlCommand(query,myConn);
            SqlDataReader tbl = mycommand1.ExecuteReader();
// i just want to add the listview coding here 

        }

    }


    protected void Page_Load(object sender, EventArgs e)
    {

    }

    public void Submit_Click(object sender, EventArgs e)
    {
        UserRegistration User1 = new UserRegistration();
        int flag = 0;

       User1.Mailid = mailid.Text;
       User1.UserName = UN.Text;
       User1.FirstName = FN.Text;
       User1.LastName = LN.Text;
       User1.DateofBirth = DOB.Text;
     if (User1.Validatefunc(User1.Mailid, "[Email]") == true)
        {
            IDlbl.Text = "Provide Someother mailid";
        }
        else
        {
            IDlbl.Text = "Okay";
            flag = 1;

        }

        if (User1.Validatefunc(User1.UserName, "[Username]") == true)
        {
            UNlbl.Text = "Username Not Available";

        }
        else
        {
            flag++;
            UNlbl.Text = "Username Available";
        }
        if (flag == 2)
        {
            User1.Insertfunc();
        }


    }


}

2 个答案:

答案 0 :(得分:1)

使用代码

使用listview显示数据
protected void Page_Load(object sender, EventArgs e)
        {
            ListView1.DataSource = this.GetData();
            ListView1.DataBind();
        }

        private DataSet GetData()
        {
            string conString = ConfigurationManager.ConnectionStrings["Connectionstr"].ConnectionString;
            string query = "SELECT * FROM Registration";
            SqlCommand cmd = new SqlCommand(query);
            using (SqlConnection con = new SqlConnection(conString))
            {
                using (SqlDataAdapter sda = new SqlDataAdapter())
                {
                    cmd.Connection = con;
                    sda.SelectCommand = cmd;
                    using (DataSet ds = new DataSet())
                    {
                        sda.Fill(ds);
                        return ds;
                    }
                }
            }
        }

答案 1 :(得分:0)

ASPX代码

<asp:ListView ID="ListView1" runat="server" ItemPlaceholderID="PlaceHolder1">
    <ItemTemplate>

        <strong>Country Id : </strong>
        <asp:Label runat="server" ID="lblId" Text='<%# Eval("Id") %>'></asp:Label>
        <br />
        <strong>Country Name :</strong>
        <asp:Label runat="server" ID="lblName" Text='<%# Eval("CountryName") %>'></asp:Label>

        </div>
    </ItemTemplate>
    <LayoutTemplate>
        <asp:PlaceHolder runat="server" ID="PlaceHolder1"></asp:PlaceHolder>
    </LayoutTemplate>
</asp:ListView>

Aspx.cs代码

SqlConnection con = new SqlConnection(@"Data Source=.\SqlExpress;Initial Catalog=dbTest2;Integrated Security=True");
DataSet ds = new DataSet();
SqlDataAdapter sda = new SqlDataAdapter("select * from tblCountry", con);
sda.Fill(ds);
ListView1.DataSource = ds;
ListView1.DataBind(); 

希望它有助于你的欢呼