asp.net从数据库中列出我的数据 [英] asp.net listing my data from data base

查看:111
本文介绍了asp.net从数据库中列出我的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在页面加载事件中有代码,通过它我将数据从数据库显示到表单。

之后在按钮点击事件中使用复选框选中复选框对应的项目应显示在标签中但我收到错误



I have code in page load event through which i am displaying data from database to form.
after that in button click event am using check box on selected the check box corresponding item should display in label but am getting error

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
        <Columns>
        <asp:TemplateField><ItemTemplate>
        <asp:CheckBox ID="chk" runat="server" />
        </ItemTemplate></asp:TemplateField>
        <asp:BoundField HeaderText="id" DataField="id" />
        <asp:BoundField HeaderText="name" DataField="name" />
        <asp:BoundField HeaderText="quali" DataField="quali" />
        </Columns>


        </asp:GridView>
    <asp:Button ID="btnProcess" Text="Get Selected Records" runat="server"

            onclick="btnProcess_Click" /><br />
         <asp:Label ID="lblmsg" runat="server" />
    </div>
    </form>
</body>
</html>













using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

namespace WebApplication1
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            
            SqlConnection n = new SqlConnection("Data Source=AIMLAB-PC\\SQLEXPRESS;Initial Catalog=legend;Integrated Security=True;Pooling=False");
            n.Open();
            SqlDataAdapter k = new SqlDataAdapter("select * from kk", n);
            DataSet ds = new DataSet();
            k.Fill(ds);
            GridView1.DataSource = ds;
            if (!Page.IsPostBack)
            {
                GridView1.DataBind();
            }
            //GridView1.DataBind();

        }

        protected void btnProcess_Click(object sender, EventArgs e)
        {
            string id = string.Empty;
            string name = string.Empty;
            string quali = string.Empty;
            foreach (GridViewRow gvrow in GridView1.Rows)
            {
                CheckBox ck = (CheckBox)gvrow.FindControl("chk");
                {
                    if (ck!=null&ck.Checked)
                    {
                        id = GridView1.DataKeys[gvrow.RowIndex].Value.ToString();
                        name += gvrow.Cells[2];
                        quali += gvrow.Cells[3];
                    }
                }
                lblmsg.Text = "Selected UserIds: " + id + "<br />" + "Selected UserNames: " + name + "";
            }
        }
    }
}







请告诉我做了什么错误




pls tell what wrong i have done

推荐答案

首先你需要把你的整个代码放在Page.IsPostBack的旁边,因为我看到你只需添加Gridview.DataBind ()仅在您的Page.IsPostBack中,因此每次调用页面加载事件时它都会命中数据库,而不是编写代码的正确方法。所以请把它做成这样......





First of all you need to put your entire code in side Page.IsPostBack, as i see you just add Gridview.DataBind() only in your Page.IsPostBack, so it will hit your database every time your page load event is call, and its not a proper way to write code. so please make it something like this....


protected void Page_Load(object sender, EventArgs e)
{          
   if (!Page.IsPostBack)
   {
     BindData();
   }
}
protected void BindData()
{

    SqlConnection n = new SqlConnection("Data Source=AIMLAB-PC\\SQLEXPRESS;Initial Catalog=legend;Integrated Security=True;Pooling=False");
    n.Open();
    SqlDataAdapter k = new SqlDataAdapter("select * from kk", n);
    DataSet ds = new DataSet();
    k.Fill(ds);
    GridView1.DataSource = ds;
    GridView1.DataBind();

}





还有一件事,你说你有错误,所以在哪一行你得到了这个错误,错误信息是什么?请更具体地说明您的问题及其描述。



and also one more thing, you said that you got error, so at which line you got that error, and what is the error message? please be more specific with your question and its description.


这篇关于asp.net从数据库中列出我的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆