如何为dropdownlist编写事件以填充asp.net中的网格视图 [英] How to write event for dropdownlist to fill grid view in asp.net
本文介绍了如何为dropdownlist编写事件以填充asp.net中的网格视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
任何人都可以帮我解决如何根据下拉列表值填充网格视图。我刚接触dotnet
我的节目:
设计页面:
< asp:内容 ID = BodyContent runat = 服务器 ContentPlaceHolderID = 主要内容 >
< 表格 >
< tr >
< td > Empno:< / td > < td > < / td >
< ; td >
< asp:DropDownList ID = emplist runat = server DataSourceID = emplist1
DataTextField = empCode DataValueField < span class =code-keyword> = empCode >
< / asp:Dro pDownList >
< asp:SqlDataSource ID = emplist1 runat = server
ConnectionString = <% $ ConnectionStrings:euroTransConnectionString3%>
< span class =code-attribute> SelectCommand = SELECT DISTINCT [empCode] FROM [eurEvents_activity] >
< / asp:SqlDataSource >
< / td >
< td > < / td >
< td >
< asp:DropDownList ID = atddroplist runat = < span class =code-keyword> server AutoPostBack = true >
< asp:ListItem 已选择 = True 值 = 1 > 当前周< / asp:ListItem >
< asp:ListItem 值 = 2 > 上周< / asp:ListItem >
< asp:ListItem 值 = 3 > 当前月份< / asp:ListItem >
< asp:ListItem 值 = 4 > 上个月< / asp:ListItem >
< asp:ListItem 值 = 5 > 自定义< / asp:ListItem >
< / asp:DropDownList >
< / td >
< / tr >
< / table >
< br / >
< asp:GridView ID = GridView1 AutoGenerateColumns = False runat = server >
< 列 >
< asp:BoundField DataField = EmpCode HeaderText = EmpCode / >
< asp:BoundField DataField = EventDate HeaderText = < span class =code-keyword> EventDate / >
< asp:BoundField DataField = InTime HeaderText = InTime / >
< asp:BoundField DataField = OutTime < span class =code-attribute> HeaderText = OutTime / >
< /列 >
< / asp: GridView >
< < span class =code-leadattribute> / asp:Content >
我的业务逻辑是:
名称空间empatdList
{
public class empatdListBI
{
#region Local Variables
DbConnection connection = new DbConnection();
DataTable tempData;
// DataSet tempData1;
SqlCommand命令;
SqlDataAdapter适配器;
#endregion
public DataTable LoadRecords(string empcode)
{
//此函数用于为各个字段加载QueryReport
尝试
{
command = new SqlCommand(sp_InOutDetails_LastMonth);
command.CommandType = CommandType.StoredProcedure;
command.Parameters.Add(@ EmpCode,SqlDbType.VarChar).Value = empcode;
adapter = new SqlDataAdapter(command);
tempData = new DataTable();
// tempData1 = new DataSet();
command.Connection = connection.OpenConnection();
adapter.Fill(tempData);
command.ExecuteNonQuery();
}
catch(System.Data.SqlClient.SqlException ex)
{
HttpContext.Current.Session [Error] = ex;
}
最后
{
connection.CloseConnection();
}
return tempData;
}
}
}
我的源代码是:
使用System;
使用System.Collections.Generic;
使用System.Linq;
使用System.Web;
使用System.Web.UI;
使用System.Web.UI.WebControls;
使用System.Data;
使用System.Data.SqlClient;
namespace empatdList
{
public partial class _Default:System.Web.UI.Page
{
protected void Page_Load(object sender,EventArgs e)
{
if(!IsPostBack)
{
binddropdown();
}
}
protected void binddropdown()
{
empatdListBI c = new empatdListBI();
DbConnection b = new DbConnection();
SqlDataAdapter da = new SqlDataAdapter();
DataTable dt = new DataTable();
DataSet ds = new DataSet();
b.OpenConnection();
dt = c.LoadRecords(emplist.Text);
GridView1.DataSource = dt;
GridView1.DataBind();
b.CloseConnection();
}
}
}
我把数据库连接保存在单独的类中。请帮我解决我犯错误的地方
解决方案
ConnectionStrings:euroTransConnectionString3%>
< span class =code-attribute> SelectCommand = SELECT DISTINCT [empCode] FROM [eurEvents_activity] >
< / asp:SqlDataSource >
< / td >
< td > < / td >
< td >
< < span class =code-leadattribute> asp:DropDownList ID = atddroplist runat = server AutoPostBack = tru e >
< asp:ListItem 已选择 = True Value = 1 > 当前周< / asp:ListItem >
< asp:ListItem 值 = 2 > 上周< / asp:ListItem >
< asp:ListItem 值 = 3 > 当前月份< / asp:ListItem >
< asp:ListItem 值 = 4 > 上个月< / asp:ListItem >
<asp:ListItem 值 = 5 > 自定义< / asp:ListItem >
< / asp:DropDownList >
< / td >
< / tr >
< / table >
< br / >
< asp:GridView ID = GridView1 AutoGenerateColumns = False runat = 服务器 >
< 列 >
< asp:BoundFie ld DataField = EmpCode HeaderText = EmpCode / >
< asp:BoundField DataField = EventDate HeaderText = EventDate < span class =code-keyword> / >
< asp:BoundField DataField = InTime HeaderText = InTime / >
< asp:BoundField DataField = OutTime HeaderText = OutTime/ >
< /列 >
< / asp:GridView >
< / asp:内容 >
我的业务逻辑是:
namespace empatdList
{
public class empatdListBI
{
#region Local Variables
DbConnection connection = new DbConnection();
DataTable tempData;
// DataSet tempData1;
SqlCommand命令;
SqlDataAdapter适配器;
#endregion
public DataTable LoadRecords(string empcode)
{
//此函数用于为各个字段加载QueryReport
尝试
{
command = new SqlCommand(sp_InOutDetails_LastMonth);
command.CommandType = CommandType.StoredProcedure;
command.Parameters.Add(@ EmpCode,SqlDbType.VarChar).Value = empcode;
adapter = new SqlDataAdapter(command);
tempData = new DataTable();
// tempData1 = new DataSet();
command.Connection = connection.OpenConnection();
adapter.Fill(tempData);
command.ExecuteNonQuery();
}
catch(System.Data.SqlClient.SqlException ex)
{
HttpContext.Current.Session [Error] = ex;
}
最后
{
connection.CloseConnection();
}
return tempData;
}
}
}
我的源代码是:
使用System;
使用System.Collections.Generic;
使用System.Linq;
使用System.Web;
使用System.Web.UI;
使用System.Web.UI.WebControls;
使用System.Data;
使用System.Data.SqlClient;
namespace empatdList
{
public partial class _Default:System.Web.UI.Page
{
protected void Page_Load(object sender,EventArgs e)
{
if(!IsPostBack)
{
binddropdown();
}
}
protected void binddropdown()
{
empatdListBI c = new empatdListBI();
DbConnection b = new DbConnection();
SqlDataAdapter da = new SqlDataAdapter();
DataTable dt = new DataTable();
DataSet ds = new DataSet();
b.OpenConnection();
dt = c.LoadRecords(emplist.Text);
GridView1.DataSource = dt;
GridView1.DataBind();
b.CloseConnection();
}
}
}
我把数据库连接保存在单独的类中。请帮我解决我犯错误的地方
使用DropDownList对象的OnSelectedIndexChanged事件。
参见:在其中 [ ^ ]
< html >
< body >
< 表格 id = form1 runat = server >
< div >
< asp:DropDownList ID = ddlCountry runat < span class =code-keyword> = server DataSourceID = sds_Countries
DataTextField = 国家/地区 DataValueField = 国家/地区 AutoPostBack = True >
< / asp:DropDo wnList >
< asp:SqlDataSource ID = sds_Countries runat = server
ConnectionString = <%
Can anyone please help me out how to fill grid view based on dropdownlist value. Iam new to dotnet
my program:
Design Page:
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<table>
<tr>
<td>Empno: </td> <td></td>
<td>
<asp:DropDownList ID="emplist" runat="server" DataSourceID="emplist1"
DataTextField="empCode" DataValueField="empCode">
</asp:DropDownList>
<asp:SqlDataSource ID="emplist1" runat="server"
ConnectionString="<%$ ConnectionStrings:euroTransConnectionString3 %>"
SelectCommand="SELECT DISTINCT [empCode] FROM [eurEvents_activity]">
</asp:SqlDataSource>
</td>
<td></td>
<td>
<asp:DropDownList ID="atddroplist" runat="server" AutoPostBack="true">
<asp:ListItem Selected="True" Value="1">Current Week</asp:ListItem>
<asp:ListItem Value="2">Last Week</asp:ListItem>
<asp:ListItem Value="3">Current Month</asp:ListItem>
<asp:ListItem Value="4">Last Month</asp:ListItem>
<asp:ListItem Value="5">Custom</asp:ListItem>
</asp:DropDownList>
</td>
</tr>
</table>
<br />
<asp:GridView ID="GridView1" AutoGenerateColumns="False" runat="server">
<Columns>
<asp:BoundField DataField="EmpCode" HeaderText="EmpCode" />
<asp:BoundField DataField="EventDate" HeaderText="EventDate" />
<asp:BoundField DataField="InTime" HeaderText="InTime" />
<asp:BoundField DataField="OutTime" HeaderText="OutTime" />
</Columns>
</asp:GridView>
</asp:Content>
My Business logic is:
namespace empatdList { public class empatdListBI { # region Local Variables DbConnection connection = new DbConnection(); DataTable tempData; // DataSet tempData1; SqlCommand command; SqlDataAdapter adapter; # endregion public DataTable LoadRecords(string empcode) { //This function used to Load QueryReport for the respective Fields try { command = new SqlCommand("sp_InOutDetails_LastMonth"); command.CommandType = CommandType.StoredProcedure; command.Parameters.Add("@EmpCode", SqlDbType.VarChar).Value = empcode; adapter = new SqlDataAdapter(command); tempData = new DataTable(); //tempData1 = new DataSet(); command.Connection = connection.OpenConnection(); adapter.Fill(tempData); command.ExecuteNonQuery(); } catch (System.Data.SqlClient.SqlException ex) { HttpContext.Current.Session["Error"] = ex; } finally { connection.CloseConnection(); } return tempData; } } }
My source code is:
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 empatdList { public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { binddropdown(); } } protected void binddropdown() { empatdListBI c = new empatdListBI(); DbConnection b = new DbConnection(); SqlDataAdapter da = new SqlDataAdapter(); DataTable dt = new DataTable(); DataSet ds = new DataSet(); b.OpenConnection(); dt = c.LoadRecords(emplist.Text); GridView1.DataSource = dt; GridView1.DataBind(); b.CloseConnection(); } } }
I had kept database connection in separate class. Please help me out where i am making mistake
解决方案
ConnectionStrings:euroTransConnectionString3 %>" SelectCommand="SELECT DISTINCT [empCode] FROM [eurEvents_activity]"> </asp:SqlDataSource> </td> <td></td> <td> <asp:DropDownList ID="atddroplist" runat="server" AutoPostBack="true"> <asp:ListItem Selected="True" Value="1">Current Week</asp:ListItem> <asp:ListItem Value="2">Last Week</asp:ListItem> <asp:ListItem Value="3">Current Month</asp:ListItem> <asp:ListItem Value="4">Last Month</asp:ListItem> <asp:ListItem Value="5">Custom</asp:ListItem> </asp:DropDownList> </td> </tr> </table> <br /> <asp:GridView ID="GridView1" AutoGenerateColumns="False" runat="server"> <Columns> <asp:BoundField DataField="EmpCode" HeaderText="EmpCode" /> <asp:BoundField DataField="EventDate" HeaderText="EventDate" /> <asp:BoundField DataField="InTime" HeaderText="InTime" /> <asp:BoundField DataField="OutTime" HeaderText="OutTime" /> </Columns> </asp:GridView> </asp:Content>
My Business logic is:
namespace empatdList { public class empatdListBI { # region Local Variables DbConnection connection = new DbConnection(); DataTable tempData; // DataSet tempData1; SqlCommand command; SqlDataAdapter adapter; # endregion public DataTable LoadRecords(string empcode) { //This function used to Load QueryReport for the respective Fields try { command = new SqlCommand("sp_InOutDetails_LastMonth"); command.CommandType = CommandType.StoredProcedure; command.Parameters.Add("@EmpCode", SqlDbType.VarChar).Value = empcode; adapter = new SqlDataAdapter(command); tempData = new DataTable(); //tempData1 = new DataSet(); command.Connection = connection.OpenConnection(); adapter.Fill(tempData); command.ExecuteNonQuery(); } catch (System.Data.SqlClient.SqlException ex) { HttpContext.Current.Session["Error"] = ex; } finally { connection.CloseConnection(); } return tempData; } } }
My source code is:
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 empatdList { public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { binddropdown(); } } protected void binddropdown() { empatdListBI c = new empatdListBI(); DbConnection b = new DbConnection(); SqlDataAdapter da = new SqlDataAdapter(); DataTable dt = new DataTable(); DataSet ds = new DataSet(); b.OpenConnection(); dt = c.LoadRecords(emplist.Text); GridView1.DataSource = dt; GridView1.DataBind(); b.CloseConnection(); } } }
I had kept database connection in separate class. Please help me out where i am making mistake
Use the OnSelectedIndexChanged event from the DropDownList object.
See: here[^]
<html> <body> <form id="form1" runat="server"> <div> <asp:DropDownList ID="ddlCountry" runat="server" DataSourceID="sds_Countries" DataTextField="Country" DataValueField="Country" AutoPostBack="True"> </asp:DropDownList> <asp:SqlDataSource ID="sds_Countries" runat="server" ConnectionString="<%
这篇关于如何为dropdownlist编写事件以填充asp.net中的网格视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文