我正在寻找一种方法让我的CS中的两个EVALS。第一个字符串完美地工作,但另一个字符串不能
这是我的aspx
<asp:ListView ID="lvMaterialsList" runat="server" ondatabound="lvMaterialsList_DataBound"
onpagepropertieschanging="lvMaterialsList_PagePropertiesChanging" OnItemCommand="lvMaterialsList_ItemCommand">
<ItemTemplate>
<tr>
<td>
<asp:Label ID="Label2" runat="server" Text='<%# Eval("SupplierProduct")%> ' Visible="false" /></td>
<td>
<td>
<asp:Label ID="ltRefNo" runat="server" Text='<%# Eval("ReqMatID") %>' Visible="false" />
<%# Eval("SupplierProduct")%>
</td>
<td>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("SupplierProductID") %>' width="1px" Visible="false" />
</td>
<td><%# Eval("Quantity") %></td>
<td><asp:TextBox ID="txtAlloted" runat="server" type="number" Text=''
class="form-control" width="60px" min='1'/></td>
<td><asp:LinkButton ID="btnUpdate" runat="server" OnClientClick='return confirm("Withdraw materials?")' CommandName="updateused">
<i class="fa fa-refresh"></i> </asp:LinkButton>
</td>
<td><asp:Literal ID="ltUsed" runat="server" Text='<%# Eval("Used") %>'
Visible="false" />
<%# Eval("Used")%>
</td>
<td><%# Eval("Status") %></td>
<td><%# Eval("DateAdded", "{0: MMMM dd, yyyy}") %></td>
<td><%# Eval("DateModified", "{0: MMMM dd, yyyy}") %></td>
<td>
<td>
<asp:LinkButton Text="Additional" class="btn btn-primary " ID="lbtnUpdate" runat="server" CommandArgument='<%#Eval("ProjectID")+","+ Eval("ReqMatID")%>'
PostBackUrl='<%# string.Format(" AdditionalAlloc.aspx?ID={0}&MID={1}", Eval("ProjectID"), Eval("ReqMatID"))%>'></asp:LinkButton>
<asp:LinkButton Text="Add req" class="btn btn-primary " ID="lblAdd" runat="server"
PostBackUrl='<%# string.Format(" AddReq.aspx?ID={0}", Eval("ProjectID"))%>'></asp:LinkButton>
<a href='EditMaterial.aspx?ID=<%# Eval("ReqMatID") %>'><i class="fa fa-pencil"></i></a>
<%-- <a href='../Reports/SupplierDetails.aspx?ID=<%# Eval("SupplierID") %>'><i class="fa fa-print"></i></a> --%>
<a href='DeleteMaterial.aspx?ID=<%# Eval("ReqMatID") %>'" onclick='return confirm("Delete record?")'>
<i class="fa fa-trash-o"></i>
</a>
</td>
</tr>
</ItemTemplate>
<EmptyDataTemplate>
<tr>
<td colspan="12"><h2 class="text-center">No records found.</h2></td>
</td>
</tr>
</EmptyDataTemplate>
</asp:ListView>
这是我使用的代码。
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 Admin_Project_AdditionalAlloc : System.Web.UI.Page
{
SqlConnection con = new SqlConnection(Helper.GetCon());
protected void Page_Load(object sender, EventArgs e)
{
if (Request.QueryString["ID"] != null)
{
int userID = 0;
bool validUser = int.TryParse(Request.QueryString["ID"].ToString(), out userID);
int productID = 0;
bool product = int.TryParse(Request.QueryString["MID"].ToString(), out productID);
if (validUser)
{
if (!IsPostBack)
{
GetInfo(userID);
GetMaterialsInfo(productID);
GetProjects();
GetMaterials();
}
}
else
Response.Redirect("Default.aspx");
}
else
Response.Redirect("Default.aspx");
ddlProjectName.Enabled = false;
ddlProjectName.CssClass = "form-control";
ddlMaterials.Enabled = false;
ddlMaterials.CssClass = "form-control";
Panel1.Visible = false;
}
void GetProjects()
{
con.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandText = "SELECT ProjectID, ProjectName FROM Project";
SqlDataReader dr = cmd.ExecuteReader();
ddlProjectName.DataSource = dr;
ddlProjectName.DataTextField = "ProjectName";
ddlProjectName.DataValueField = "ProjectID";
ddlProjectName.DataBind();
con.Close();
ddlProjectName.Items.Insert(0, new ListItem("Select Project", ""));
}
void GetInfo(int ID)
{
con.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandText = @"SELECT ProjectID FROM Project WHERE ProjectID=@ProjectID";
cmd.Parameters.AddWithValue("@ProjectID", ID);
SqlDataReader dr = cmd.ExecuteReader();
if (dr.HasRows)
{
while (dr.Read())
{
ddlProjectName.SelectedValue = dr["ProjectID"].ToString();
}
con.Close();
}
else
{
con.Close();
Response.Redirect("Default.aspx");
}
}
void GetMaterialsInfo(int MID)
{
con.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandText = @"SELECT project.ProjectID, RequiredMaterials.ReqMatID FROM Project Inner join RequiredMaterials on RequiredMaterials.ProjectID = Project.ProjectID WHERE Project.ProjectID=@ProjectID AND RequiredMaterials.ReqMatID=@ReqMatID";
cmd.Parameters.AddWithValue("@ProjectID", ID);
cmd.Parameters.AddWithValue("@ReqMatID", MID);
SqlDataReader dr = cmd.ExecuteReader();
if (dr.HasRows)
{
while (dr.Read())
{
ddlMaterials.SelectedValue = dr["ReqMatID"].ToString();
}
con.Close();
}
else
{
con.Close();
//Response.Redirect("Default.aspx");
}
}
void GetMaterials()
{
con.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandText = "SELECT project.ProjectID, ProjectName, RequiredMaterials.ReqMatID, Products.ProductName + ' (' + SupplierName +')' as SupplierProduct FROM Project Inner join RequiredMaterials on RequiredMaterials.ProjectID = Project.ProjectID INNER JOIN SupplierProducts ON RequiredMaterials.ProductID = SupplierProducts.ProductID INNER JOIN Products ON SupplierProducts.ProductID = Products.ProductID INNER JOIN Supplier ON SupplierProducts.SupplierID = Supplier.SupplierID WHERE Project.ProjectID=@ProjectID AND RequiredMaterials.ReqMatID=@ReqMatID ";
cmd.Parameters.AddWithValue("@ProjectID", ID);
cmd.Parameters.AddWithValue("@ReqMatID", ID);
SqlDataReader dr = cmd.ExecuteReader();
ddlMaterials.DataSource = dr;
ddlMaterials.DataTextField = "SupplierProduct";
ddlMaterials.DataValueField = "ReqMatID";
ddlMaterials.DataBind();
con.Close();
ddlMaterials.Items.Insert(0, new ListItem("Select Project", ""));
}
}
这就是我使用listview的方式
protected void lvMaterialsList_ItemCommand(object sender, ListViewCommandEventArgs e)
{
Label ltRefNo = (Label)e.Item.FindControl("ltRefNo");
Label label1 = (Label)e.Item.FindControl("Label1");
Label label2 = (Label)e.Item.FindControl("Label2");
TextBox txtAlloted = (TextBox)e.Item.FindControl("txtAlloted");
Literal ltUsed = (Literal)e.Item.FindControl("ltUsed");
bool existingSupply = IsExisting();
bool hasquantity = HasInventory(label1.Text);
int alloted = Convert.ToInt32(txtAlloted.Text);
int productid = Convert.ToInt32(label1.Text);
string Label2 = label2.Text;
var inventory = new DataTable();
using (var da = new SqlDataAdapter("SELECT * FROM Inventory", con))
{
da.Fill(inventory);
}
var products = new DataTable();
using (var da = new SqlDataAdapter("SELECT * FROM SupplierProducts", con))
{
da.Fill(products);
}
if (e.CommandName == "updateused")
{
con.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
if (alloted < 0)
{
quantityCheck.Text = "cannot input negative number as quantity";
Panel1.Visible = true;
return;
}
int finalQuantity = Convert.ToInt32(inventory.Rows[0]["Quantity"]) - alloted;
int criticalLevel = Convert.ToInt32(products.Rows[0]["CriticalLevel"]);
bool isSafe = finalQuantity >= criticalLevel;
string status = String.Empty;
if (finalQuantity <= criticalLevel && finalQuantity != 0)
{
status = "Critical";
}
else if (finalQuantity > criticalLevel)
{
status = "Available";
}
else
{
status = "Unavailable";
}
if (existingSupply && hasquantity && isSafe)
{
cmd.CommandText = "UPDATE RequiredMaterials SET Used=Used + @Used, DateModified=@DateModified WHERE ReqMatID=@ReqMatID";
cmd.Parameters.AddWithValue("@Used", alloted);
cmd.Parameters.AddWithValue("@ReqMatID", ltRefNo.Text);
cmd.Parameters.AddWithValue("@DateModified", DateTime.Now);
cmd.ExecuteNonQuery();
cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandText = "UPDATE Inventory SET Quantity = Quantity - @Quantity, Status=@Status " +
"WHERE ProductID=@ProductID";
cmd.Parameters.AddWithValue("@Quantity", alloted);
cmd.Parameters.AddWithValue("@ProductID", productid);
cmd.Parameters.AddWithValue("@Status", status);
cmd.ExecuteNonQuery();
cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandText = "INSERT INTO Withdraw VALUES (@ProjectID, @ProductID, @SupplierID, @Quantity,@UserID, @DateWithdrawn)";
cmd.Parameters.AddWithValue("@ProjectID", ddlProjects.SelectedValue);
cmd.Parameters.AddWithValue("@ProductID", productid);
cmd.Parameters.AddWithValue("@SupplierID", DBNull.Value );
cmd.Parameters.AddWithValue("@Quantity", alloted);
cmd.Parameters.AddWithValue("@UserID", Session["userid"].ToString());
cmd.Parameters.AddWithValue("@DateWithdrawn", DateTime.Now);
cmd.ExecuteNonQuery();
con.Close();
GetMaterialsList();
}
else if (!isSafe)
{
quantityCheck.Text = Label2 + @" Inventory count will be lower than the critical level.";
Panel1.Visible = true;
}
else
{
Panel1.Visible = true;
quantityCheck.Text = Label2 + " Cannot input inventory";
}
}
}
我继续收到错误:
对象引用未设置为对象的实例。
我调试了系统并显示: 将nvarchar值'__Page'转换为数据类型int时,转换失败。
答案 0 :(得分:0)
由于表单上有LinkButton
而不是任何数据绑定控件,因此当前代码无效,因为Eval
方法只能用于数据绑定控件。现在,我真的不确定你是如何让ProjectID
工作的。您应该始终尽量避免使标记代码复杂化。如果你的代码背后的所有内容都隐藏了复杂的东西?只需在您的代码隐藏文件中执行此操作。
MarkUp(aspx页面): -
<asp:LinkButton ID="lbtnUpdate" Text="Additional" class="btn btn-primary"
runat="server"></asp:LinkButton>
代码背后: -
lbtnUpdate.PostBackUrl = String.Format("AdditionalAlloc.aspx?ID={0}&MID={1}",
ProjectID,ReqMatID);