问题描述
我的问题是,我有两个存储的proc,一个正在调用数据填充下拉列表,另一个正在填充我的gridview.我需要的是当我从下拉列表中选择一个项目时,页面应刷新并使用新数据更新gridview.但是我遇到的问题是,每次回发后都会出现相同的数据和页面,因此它不会提取新数据...对于我选择的值,我有一个硬编码的值,但是我还需要将其取出来,以便当用户选择时一个项目,然后它会自动更新...
My issue, I have two stored procs, one is calling data to fill the dropdown list, and another that fills my gridview. What I need is when I select an item from the dropdown list, the page should refresh and update the gridview with the new data. But the issue I am having is, after each postback the same data and page appears, so its not pulling new data... I have a hardcoded value for my select value but I also need to take this out so that when the users pick an item then it automatically updates...
namespace WorkStationUITarget
{
public partial class Default : System.Web.UI.Page
{
//public static int CountLabel = 0;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Refreshdata(225);
BindDropDownList();
}
}
private void BindDropDownList()
{
BizManager mgr = new BizManager();
DropDownList1.DataSource =
mgr.GetItemSeriesMaster(); //.DefaultView.RowFilter("ID = 201"); //how to filter data
DropDownList1.DataTextField = "DESCRIPTION"; // the items to be displayed in the list items
DropDownList1.DataValueField = "ID"; // the id of the items displayed
DropDownList1.DataBind();
}
public void Dropdownlist1_SelectedIndexChanged(object sender, EventArgs e)
{
//refreshes to the new table on drop selection
}
public void Refreshdata(int selectedProduct)
{
BizManager biz = new BizManager();
GridView1.DataSource = biz.GetPacktstatisticsForShift(new DateTime(2016, 6, 10, 6, 0, 0)
, new DateTime(2016, 6, 10, 13, 59, 59)
, selectedProduct).DefaultView;
GridView1.DataBind();
//rebind the data
}
bizmanager类
public class BizManager
{
public string Shiftstart { get; set; }
public string Shiftend { get; set; }
public string Itemseriesmaster { get; set; }
public string SeriesMasterId { get; set; }
public DataTable GetPacktstatisticsForShift(DateTime shiftStart, DateTime shiftEnd, int seriesMasterId)
{
using (DataManager dmgr = new DataManager())
{
dmgr.Connect(ConfigurationManager.AppSettings["ProductionKey"]);
DataSet dset = dmgr.TargetQuantites(seriesMasterId, shiftStart, shiftEnd);
return CreatePackingStats(dset);
}
}
public DataTable GetItemSeriesMaster()
{
using (DataManager dmgr = new DataManager())
{
dmgr.Connect(ConfigurationManager.AppSettings["ProductionKey"]);
DataSet dset = dmgr.getworkstationnames();//IManSeriesList();
return dset.Tables[0];
}
}
public DataTable CreatePackingStats(DataSet dset)
{
using (DataManager dmgr = new DataManager())
{
DataTable target = dset.Tables[0];
DataTable actual = dset.Tables[1];
DataColumn[] cols = new DataColumn[1];
cols[0] = actual.Columns["Hour"];
actual.PrimaryKey = cols;
DataTable final = new DataTable();
// Create table columns
foreach (DataColumn col in target.Columns)
{
final.Columns.Add(new DataColumn(col.ColumnName, col.DataType));
if (col.ColumnName.Contains("Target"))
{
// Add an equivilant actual column
string newColumnName = col.ColumnName.Replace("Target", "Actual");
final.Columns.Add(newColumnName, col.DataType);
}
}
//// Add rows to new table
foreach (DataRow row in target.Rows)
{
string key = row["Hour"].ToString();
DataRow newRow = final.Rows.Add();
// Store column value
foreach (DataColumn col in final.Columns)
{
if (col.ColumnName.Contains("HOUR") || col.ColumnName.Contains("Target"))
{
newRow[col.ColumnName] = row[col.ColumnName];
}
else
{
// Find actual data
DataColumn actColumn = actual.Columns[col.ColumnName] as DataColumn;
if (actColumn == null)
{
newRow[col.ColumnName] = 0;
}
else
{
if (string.IsNullOrEmpty(actual.Rows.Find(key)[col.ColumnName].ToString()))
{
newRow[col.ColumnName] = 0;
}
else
{
newRow[col.ColumnName] = actual.Rows.Find(key)[col.ColumnName].ToString();
}
}
}
}
}
return final;
}
}
}
}
数据管理器类
namespace WorkStationUITarget.Data
{
public partial class DataManager : DataLayer
{
public DataSet TargetQuantites(int itemSeriesMasterId, DateTime shiftstart, DateTime shiftend)
{
object[] args = new object[3] { itemSeriesMasterId, shiftstart, shiftend };
return CallSp(MethodBase.GetCurrentMethod(), args) as DataSet;
} // CALLS A STORED PROC
public DataSet getworkstationnames() //IManSeriesList() //CHANGE BACK FOR OTHER USP
{
object[] args = new object[] {} ;
return CallSp(MethodBase.GetCurrentMethod(), args) as DataSet;
}
}
}
HTML
@Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WorkStationUITarget.Default" %>
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="CSS/style.css" rel="stylesheet" />
<style>
body {
font-family: Arial;
color: white;
}
.split {
height: 100%;
position: fixed;
z-index: 1;
top: 0;
overflow-x: hidden;
padding-top: 20px;
}
.left {
left: 0;
background-color: #110;
width: 30%;
text-align: center;
}
.right {
right: 0;
background-color: white;
width: 70%;
position: fixed;
text-align: center;
}
.centered {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
text-align: center;
}
.centered img {
width: 150px;
border-radius: 50%;
}
</style>
</head>
<body>
<div class="split left">
<div class="center">
<h1 style="color: red; font-weight: bold; position: static; text-align: center; padding-top: 25px "> TOTAL QUANTITY PACKED
<asp:Label ID="label" runat="server" Text="1" Width="100%" Height="100%" Font-Size="250px" style="position: static; text-align: center; padding-top: 280px; "></asp:Label>
<img src="Logo/siemensblack1.png" style="position: static; top: 110px; padding-top: 300px"; />
</div>
<div class="body-container;">
</div>
</div>
<form runat="server">
<div class="split right">
<div class="center">
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" align="centered" OnSelectedIndexChanged="Dropdownlist1_SelectedIndexChanged" >
<asp:ListItem Value="214">214</asp:ListItem>
<asp:ListItem Value="225">225</asp:ListItem>
</asp:DropDownList>
<h1 style="color: red; font-weight: bold; text-align: center;">CELL TARGETS & ACTUAL FIGURES</h1>
<asp:GridView ID="GridView1" runat="server" BackColor="White" BorderColor="black" BorderStyle="None" BorderWidth="5px" CellPadding="25" ForeColor="Black" GridLines="Horizontal" HorizontalAlign="Center" Font-Size="24px" >
<AlternatingRowStyle BackColor="White"/>
<FooterStyle BackColor="#CCCC99"/>
<HeaderStyle BackColor="white" Font-Bold="True" ForeColor="black"/>
<PagerStyle BackColor="#F7F7DE" ForeColor="Black" HorizontalAlign="Right"/>
<RowStyle BackColor="#F7F7DE"/>
<SelectedRowStyle BackColor="#CE5D5A" Font-Bold="True" ForeColor="black"/>
<SortedAscendingCellStyle BackColor="#FBFBF2"/>
<SortedAscendingHeaderStyle BackColor="#848384"/>
<SortedDescendingCellStyle BackColor="#EAEAD3"/>
<SortedDescendingHeaderStyle BackColor="#575357"/>
</asp:GridView>
<div class="w3-container">
<div class="center">
<style>
div { padding: 30px 10px 15px; }
.newStyle1 {
color: #FF0000;
font-size: 86px;
}
</style>
</div>
<img src="Logo/siemenswhite1.png" style="padding-left: 70px; padding-top: 13px; position: static; text-align: center;"/>
</div>
</div>
</div>
</form>
</body>
</html>
我认为这一定与页面后面的aspx代码有关.
I assume it must be something to do with the aspx code behind page.
推荐答案
您只需要使用所选的产品ID,并将其传递给 Refreshdata
:
You just have to use the selected product-ID and pass it to Refreshdata
:
public void Dropdownlist1_SelectedIndexChanged(object sender, EventArgs e)
{
int productId;
if(int.TryParse(Dropdownlist1.SelectedValue, out productId))
Refreshdata(productId);
}
这篇关于回发后,Dropdownlist不会更改gridview数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!