网格视图不显示结果

发布于 2024-10-31 15:44:03 字数 1184 浏览 1 评论 0原文

当选择 DataSource 以及用户从 DropDownList 中选择选项时,我想在运行时绑定 GridView。但所选的表或连接未正确建立。 请检查以下代码并给我适当的解决方案。

    public partial class index : System.Web.UI.Page
    {
        SqlConnection conn = new SqlConnection();
        string option = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            option = selectProductdropdown.SelectedValue;
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            Label2.Text = option;
            if (option == "Books")
            {

                Label3.Text = option;
                conn.ConnectionString = ConfigurationManager.ConnectionStrings["booksconnectionstring"].ConnectionString;
                conn.Open();
                SqlCommand cmd = new SqlCommand("SELECT * from books", conn);
                cmd.CommandType = CommandType.Text;

                SqlDataAdapter reader = new SqlDataAdapter(cmd);

                DataSet s = new DataSet();
                reader.Fill(s);
                GridView1.DataSource = s;
                GridView1.DataBind();
                conn.Close();
            }

I want to bind GridView at run time when the DataSource is selected and when the user select a option from a DropDownList. But the selected table or connection is not made properly.
Please check the following code and give me the appropriate solution.

    public partial class index : System.Web.UI.Page
    {
        SqlConnection conn = new SqlConnection();
        string option = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            option = selectProductdropdown.SelectedValue;
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            Label2.Text = option;
            if (option == "Books")
            {

                Label3.Text = option;
                conn.ConnectionString = ConfigurationManager.ConnectionStrings["booksconnectionstring"].ConnectionString;
                conn.Open();
                SqlCommand cmd = new SqlCommand("SELECT * from books", conn);
                cmd.CommandType = CommandType.Text;

                SqlDataAdapter reader = new SqlDataAdapter(cmd);

                DataSet s = new DataSet();
                reader.Fill(s);
                GridView1.DataSource = s;
                GridView1.DataBind();
                conn.Close();
            }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

痞味浪人 2024-11-07 15:44:03

问题出在您的 page_load 事件中,您在其中为选项分配了一个值。当您单击该按钮时, page_load 将再次调用并且您的值将重置。

它应该是......

 protected void Page_Load(object sender, EventArgs e)
    {
       if(!Page.IsPostBack)
        option = selectProductdropdown.SelectedValue;
    }

或者如果你喜欢的话会更好......

if (selectProductdropdown.SelectedValue == "Books")

The problem is in your page_load event, where you are assigning a value to option. When you click the button, the page_load will call again and your value will reset.

it should be...

 protected void Page_Load(object sender, EventArgs e)
    {
       if(!Page.IsPostBack)
        option = selectProductdropdown.SelectedValue;
    }

OR it would be better if you do like..

if (selectProductdropdown.SelectedValue == "Books")
眼泪淡了忧伤 2024-11-07 15:44:03

因为您可能在每次页面加载时清空选项。

because you are probably emptying option at each page load.

離殇 2024-11-07 15:44:03

避免使用公共变量string option = "";

而是在点击事件中定义相同的变量并在那里获取选定的值

option = selectProductdropdown.SelectedValue;// move to click event

因为单击按钮时下拉列表将被重置(假设您没有在此处显示下拉绑定代码)

Avoid the public variable string option = "";

Instead define the same in the Click Event and get the selected value there

option = selectProductdropdown.SelectedValue;// move to click event

Because when the button is clicked the dropdown would be reset (assuming you have not shown the dropdown bind code here)

撑一把青伞 2024-11-07 15:44:03
protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
        option = selectProductdropdown.SelectedValue;
}
protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
        option = selectProductdropdown.SelectedValue;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文