使gridview响应两个下拉列表 [英] Make gridview respond to two dropdown lists
问题描述
我的页面中有两个下拉列表和一个gridview,我从数据库绑定了一个下拉列表,另一个我直接在页面中列出了列表项,因为在另一个下拉列表中只有5个项目。
现在的问题是我应该能够让gridview响应两个下拉菜单
selectindex更改事件并在gridview中填充它。 ..我没有得到怎么做..
onselectindex事件未触发且数据未进入gridview。
我尝试过:
I have Two dropdowns and one gridview in my page,, i am binding one dropdownlist from database and the other i directly took list items in the page as there are only 5 items in another dropdown.
now the problem is i should be able to make the gridview respond to two dropdowns
in selectindex changed event and populate that in gridview... i am not getting how to do that..
onselectindex event not firing and data is not coming in gridview.
What I have tried:
<asp:DropDownList ID="ddlGroupName" runat="server" CssClass="form-control input-sm" AutoPostBack="true" AppendDataBoundItems="true" OnSelectedIndexChanged="ddlGroup_SelectedIndexChanged">
<asp:ListItem Text="All" Value="0"></asp:ListItem>
</asp:DropDownList>
<div class="row">
<div class="col-lg-12">
<asp:GridView ID="grdDeviceDetails" runat="server" AutoGenerateColumns="false" CssClass="table table-hover table-bordered"
AllowPaging="true" PagerSettings-PageButtonCount="3" GridLines="None" PageSize="10" OnPageIndexChanging = "OnPaging">
<Columns>
<asp:BoundField DataField="GroupName" HeaderText="GroupName" SortExpression="GroupName" />
<asp:BoundField DataField="Brand" HeaderText="Brand" SortExpression="Brand" />
<asp:BoundField DataField="Model" HeaderText="Model" SortExpression="Model" />
<asp:BoundField DataField="SerialNo" HeaderText="SerialNo" SortExpression="SerialNo" />
<asp:BoundField DataField="Status" HeaderText="Status" SortExpression="Status" />
<asp:BoundField DataField="AddedOn" HeaderText="Date" SortExpression="AddedOn" />
</Columns>
<PagerStyle CssClass="pagination-ys" />
</asp:GridView>
</div>
</div>
<asp:DropDownList ID="ddlState" runat="server" CssClass="form-control input-sm" AutoPostBack="true" AppendDataBoundItems="true">
<asp:ListItem Text="All" Value="All"></asp:ListItem>
<%-- <asp:ListItem Text="Active" Value="Active"></asp:ListItem>
<asp:ListItem Text="Assigned" Value="Assigned"></asp:ListItem>
<asp:ListItem Text="Returned" Value="Returned"></asp:ListItem>
<asp:ListItem Text="De-activated" Value="De-activated"></asp:ListItem>
<asp:ListItem Text="Sent for repair" Value="Sent for repair"></asp:ListItem>
<asp:ListItem Text="End of life-retired" Value="End of life-retired"></asp:ListItem>--%>
</asp:DropDownList>
public DataSet GetItems()
{
SqlConnection con = new SqlConnection(cs);
SqlCommand cmd = new SqlCommand("sp_DeviceDetails", con);
cmd.CommandType = CommandType.StoredProcedure;
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
try
{
con.Open();
da.Fill(ds);
con.Close();
}
catch (Exception ex)
{
//write error message
}
return ds;
}
protected void Page_Load(object sender, EventArgs e)
{
ddlGroupName.DataSource = GetItems();
ddlGroupName.DataTextField = "GroupName";
ddlGroupName.DataBind();
grdDeviceDetails.DataSource = GetItems();
grdDeviceDetails.DataBind();
}
protected void ddlGroup_SelectedIndexChanged(object sender, EventArgs e)
{
GetItems();
}
推荐答案
添加IsPostBack
条件PageLoad
事件
AddIsPostBack
Condtion inPageLoad
event
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
ddlGroupName.DataSource = GetItems();
ddlGroupName.DataTextField = "GroupName";
ddlGroupName.DataBind();
grdDeviceDetails.DataSource = GetItems();
grdDeviceDetails.DataBind();
}
}
您需要从 DropDownList传递所选项目如果要基于 DropDownList
选择过滤 GridView
,请在SQL查询或SP中使用code>。这是一个快速举例供您参考:
You need to pass the selected item from theDropDownList
in your sql query or SP if you want to filterGridView
based on theDropDownList
selection. Here's a quick example for your reference:
<br />
protected void BindGrid(string selectedItem){<br />
using(SqlConnection connection = new SqlConnection("YOUR CONNECTION STRING HERE")){<br />
string sql = "SELECT Field1, Field2, Field3 FROM YourTableName WHERE YourFieldName = @Param1";<br />
using(SqlCommand cmd = new SqlCommand(sql,connection)){<br />
cmd.Parameters.AddWithValue("@Param1", selectedItem);<br />
<br />
DataTable dt = new DataTable();<br />
SqlDataAdapter ad = new SqlDataAdapter(cmd);<br />
ad.Fill(dt);<br />
<br />
if (dt.Rows.Count > 0) { //check if the query returns any data<br />
GridView1.DataSource = dt;<br />
GridView1.DataBind();<br />
}<br />
else<br />
{<br />
//No records found<br />
}<br />
}<br />
<br />
}<br />
}<br />
然后在你的下拉菜单的 SelectedIndexChanged
事件中,你可以像这样调用上面的方法:
Then at SelectedIndexChanged
event of your dropdown, you can call the method above like this:
<br />
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e){<br />
BindGrid(DropDownList1.SelectedItem.Text);<br />
}<br />
只需确保将 AutoPostback
设置为true即可你的下拉列表触发 selectedindexchanged
事件。
Just make sure to set AutoPostback
to true for your dropdown to fire the selectedindexchanged
event.
我不是ASP.NET程序员(更喜欢MVC),但看起来< asp:DropDownList ID =ddlStaterunat =serverCssClass =form-control input-smAutoPostBack =trueAppendDataBoundItems =true>
control没有接通OnSelectedIndexChanged
事件。
I am not an ASP.NET programmer (prefer MVC), but it appears that<asp:DropDownList ID="ddlState" runat="server" CssClass="form-control input-sm" AutoPostBack="true" AppendDataBoundItems="true">
control is not wired up for theOnSelectedIndexChanged
event.
这篇关于使gridview响应两个下拉列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!