如何使用asp.net中的dropdownlist,gridview控件检索多个表信息 [英] How to retrieve multiple table information using dropdownlist,gridview controls in asp.net

查看:49
本文介绍了如何使用asp.net中的dropdownlist,gridview控件检索多个表信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<asp:DropDownList ID="DropDownList1" runat="server" 

            onselectedindexchanged="DropDownList1_SelectedIndexChanged">
            <asp:ListItem>--select table--</asp:ListItem>
            <asp:ListItem>Student</asp:ListItem>
            <asp:ListItem>category</asp:ListItem>
            <asp:ListItem>logindetails</asp:ListItem>
        </asp:DropDownList>
        <asp:GridView ID="GridView1" runat="server">
        </asp:GridView>
<pre lang="c#">

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;

public partial class Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
       
        SqlConnection con = new SqlConnection("Data Source=VICTORIN11-PC\\SQLEXPRESS;Initial Catalog=db;Integrated Security=True");
        SqlDataAdapter da;
        DataSet ds = new DataSet();
        con.Open();
        for (int i = 0; i < DropDownList1.Items.Count; i++)

        {



            string s = "select * from " + DropDownList1.Items[1].ToString();

            da = new SqlDataAdapter(s, con);

            da.Fill(ds, DropDownList1.Items[i].ToString());



        }

    }

    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)

    {

        DataSet ds = new DataSet ();

        DropDownList1.DataSource = ds.Tables[DropDownList1.SelectedIndex];

    }

}





你好朋友帮帮我



在上面的.cs代码无法绑定表格信息。



我正在开发像dropdownlist这样的web应用程序,ddl项目中的gridview控件,比如表名,当我点击第一项并加载时第一个表格信息到gridview控件和第二个项目点击表格信息绑定到gridview控件。给我建议如何在c#.net中编写业务逻辑代码。



b / b








先谢谢。



Hi Friends help me

In the above .cs code cannot bind the table information.

I''m developing web application like dropdownlist,gridview controls in ddl items like tables names and when i''m clicking the first item and it loads the first table information to the gridview control and second item clicking that table information binding to gridview control.Give me suggestions how to write the business logic code in c#.net.






Thanks in Advance.

推荐答案

尝试 CodeProject常见问题系列1:ASP.NET GridView [ ^ ]。


试试这个

Try this
<asp:dropdownlist id="DropDownList1" runat="server" autopostback="true" onselectedindexchanged="DropDownList1_SelectedIndexChanged" >
            <asp:listitem>--select table--</asp:listitem>
            <asp:listitem>Student</asp:listitem>
            <asp:listitem>category</asp:listitem>
            <asp:listitem>logindetails</asp:listitem>
        </asp:dropdownlist>
        <asp:gridview id="GridView1" runat="server" >
        </asp:gridview>




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

public partial class Default2 : System.Web.UI.Page
{
    int iSIndex;
    DataSet ds = new DataSet();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            SqlConnection con = new SqlConnection(@"Data Source=VICTORIN11-PC\\SQLEXPRESS;Initial Catalog=db;Integrated Security=True");
            SqlDataAdapter da;
            con.Open();
            for (int i = 1; i < DropDownList1.Items.Count; i++)
            {
                string s = "select * from " + DropDownList1.Items[i].ToString();
                da = new SqlDataAdapter(s, con);
                da.Fill(ds, DropDownList1.Items[i].ToString());
            }
            ViewState["data"] = ds;
        }
    }
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        iSIndex = DropDownList1.SelectedIndex;
        if (iSIndex > 0)
        {
            ds = (DataSet)ViewState["data"];
            GridView1.DataSource = ds.Tables[iSIndex - 1];
            GridView1.DataBind();
        }
        else
        {
            GridView1.DataSource = null;
            GridView1.DataBind();
        }
    }
}


这篇关于如何使用asp.net中的dropdownlist,gridview控件检索多个表信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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