我是asp.net.i的新用户,我有一个按钮,下拉列表和gridview. [英] i m new in asp.net.i hv a button,dropdownlist,and gridview.

查看:96
本文介绍了我是asp.net.i的新用户,我有一个按钮,下拉列表和gridview.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个按钮,下拉列表和gridview.当我选择任何选项dropdownlist时,我都想要.然后将重新存储的数据显示到gridview中.这是一个问题,即数据显示在gridview中,但是gridview不会影响数据列表..
我的代码是

i hv a button,dropdownlist,and gridview. and i want that when i select any option dropdownlist . then the releted data is show into gridview .i hv a problem that tha data show in gridview but gridview d''nt affect with the data list..
my code is

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Net.Mail;
using System.IO;
using System.ComponentModel;
using System.Drawing;
using System.Web.SessionState;

namespace AccountManagement
{
    public partial class Account : System.Web.UI.Page
    {
        SqlConnection con;
       SqlDataAdapter da;
        SqlDataReader dr;
        //DataSet ds1;
       // String Sql;
        //int Count;
        DataTable dt;
        SqlCommand cmd;
        protected void Page_Load(object sender, EventArgs e)
        {
            string s = ConfigurationManager.ConnectionStrings["conn"].ConnectionString;
            con = new SqlConnection(s);
            if (!IsPostBack)
            {
                bindgrid();
                bindlst();
            }
            con.Open();
        }

        //protected void Button1_Click(object sender, EventArgs e)
        //{
        //    bindgrid();
        //    GridView1.Visible = true;
            
        //}
        public void bindgrid()
        {
            cmd=new SqlCommand ("select distinct Fname,Lname from Client_registration_tbl",con);
            //ds1 = new DataSet();
            con.Open();
            dr = cmd.ExecuteReader();
            //da.Fill(ds1);
            GridView1.DataSource = dr;
            GridView1.DataBind();
            GridView1.Visible = true;
            con.Close();

        }

        public void bindlst()
        {
            da = new SqlDataAdapter("select distinct Fname,Lname from Client_registration_tbl", con);
            dt = new DataTable();
            da.Fill(dt);
            DropDownList1.DataSource = dt;
            DropDownList1.DataBind();
        }
        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            da = new SqlDataAdapter("select distinct Fname,Lname from Client_registration_tbl" + DropDownList1.SelectedItem.Text + "", con);
            dt = new DataTable();
            da.Fill(dt);
            GridView1.DataSource = dt;
            GridView1.DataBind();
           // bindgrid();
            //GridView1.Visible = true;
        }

      


    }
}

推荐答案

2个选项:
1.在服务器端使用Dropdown selectedIndexchange事件(将AutoPostback设置为true)在下拉列表中查找所选的值.根据所选的值,从数据库中获取数据并显示在网格中.
2.使用AJAX在更改更改时从客户端向服务器触发XMLHttpRequest/Callback/PageMethod,并返回数据作为响应,可用于与XML绑定.

两者都尝试并继续学习.

选项1很简单.选项2需要有关与服务器窗体客户端进行交互的方式的知识,请阅读此处:
1. XMLHttpRequest [回调 [ Web服务调用 [ PageMethod [
2 Options:
1. Use Dropdown selectedIndexchange event (with AutoPostback set to true) on server side to find the value selected in the dropdownlist. based on the value selected, get the data from database and display in a grid.
2. Use AJAX to fire a XMLHttpRequest/Callback/PageMethod from client side to server on drodown change and get the data back as a response which can be used to bind with the XML.

Try both and learn on.

Option 1 is straightforward. Option 2 needs knowledge on ways to interact with server form client, read here:
1. XMLHttpRequest[^]
2. Callback[^]
3. WebService call[^]
4. PageMethod[^]


这篇关于我是asp.net.i的新用户,我有一个按钮,下拉列表和gridview.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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