我目前正在尝试做的事情有一些问题:
- 收集客户记录的 Linq-to-SQL (DBML) 返回匿名类型?这使我无法调用一个方法,在该方法中我可以通过将查询结果作为参数传递来声明页面元素初始化一次。
- 我正在尝试为页面提供下一个/上一个按钮,这将通过存储相关的 ID 字段来增加/减少当前记录。这然后回发并根据回发时按下的按钮显示下一个或上一个记录(我知道 Page_Load 将始终在按钮单击之前首先调用,只是不确定我将如何处理这个。)
到目前为止的后端代码:
DataClassesDataContext db;
string aspControlNotation = "ctl00$ContentPlaceHolder1$";
protected void Page_Load(object sender, EventArgs e)
{
db = new DataClassesDataContext((string)Session["Conn"]);
int nID = 0; //Initialise variable
string hnIDPostedVal = "";
#region If the user has not directly navigated to the page
if (!IsPostBack)
{
var queryIDInitialise = (from r in db.tblQuoteLogs
orderby r.LogDate descending
select new
{
#region Selected Fields
r.nID,
r.LogDate
#endregion
}).First();
hnID.Value = queryIDInitialise.nID.ToString();
nID = Convert.ToInt32(hnID.Value);
var query1 = (from r in db.tblQuoteLogs
where r.nID == nID
join emp in db.tblEmployees on r.QuotedBy equals emp.ID into r1
join cus in db.tblCustomers on r.luLocation equals cus.ID into r2
join qfr in db.tblQuoteFailReasons on r.ReasonForLost equals qfr.ID into r3
from x in r1.DefaultIfEmpty()
from y in r2.DefaultIfEmpty()
from z in r3.DefaultIfEmpty()
orderby r.LogDate descending
select new
{
nID = r.nID,
Quote_Reference = r.Quote_Reference,
Validity = r.Validity,
TMF = r.TMF,
QuotedBy = r.QuotedBy,
EnteredBy = r.EnteredBy,
LogDate = r.LogDate,
luCustomer = r.luCustomer,
luLocation = r.luLocation,
txtContact = r.txtContact,
txtTelephone = r.txtTelephone,
POO = r.POO,
POL = r.POL,
POD = r.POD,
FPOD = r.FPOD,
txtEquipmentType = r.txtEquipmentType,
txtCommodity = r.txtCommodity,
NamedAcct = r.NamedAcct,
Cross_Trade = r.Cross_Trade,
Haz = r.Haz,
Break_Bulk = r.Break_Bulk,
Reefer = r.Reefer,
txtTargetTEU = r.txtTargetTEU,
Frequency = r.Frequency,
FollowUpDueDate = r.FollowUpDueDate,
UKSalesComments = r.UKSalesComments,
Secured = r.Secured,
Lost = r.Lost,
txtLostQuoteReasons = z.txtLostQuoteReasons,
dateOfFollowUp = r.dateOfFollowUp,
txtSalesPerson = x.txtSalesPerson, //Gets ID from the QuotedBy column to get the particular sales person name
txtLocation = y.txtLocation //Gets ID from the luLocation column to get the particular location
}).First();
//Initialise Page Elements (With Checks for null fields)
txtQuoteRef.Text = (!String.IsNullOrEmpty(query1.Quote_Reference)) ? query1.Quote_Reference : "No Data";
txtValidity.Text = (query1.Validity != null) ? query1.Validity.ToString() : "No Date";
lblEnteredByVal.Text = (!String.IsNullOrEmpty(query1.EnteredBy)) ? query1.EnteredBy : "No Data";
lblLogDateVal.Text = (query1.LogDate != null) ? query1.LogDate.ToString() : "No Date";
txtCustomer.Text = (!String.IsNullOrEmpty(query1.luCustomer)) ? query1.luCustomer : "No Data";
txtLocation.Text = (!String.IsNullOrEmpty(query1.txtLocation)) ? query1.txtLocation : "No Data"; //BUGGY DUE TO NULL ENTRIES
txtContact.Text = (!String.IsNullOrEmpty(query1.txtContact)) ? query1.txtContact : "No Data";
txtTelephone.Text = (!String.IsNullOrEmpty(query1.txtTelephone)) ? query1.txtTelephone : "No Data";
txtPOO.Text = (!String.IsNullOrEmpty(query1.POO)) ? query1.POO : "No Data";
txtPOL.Text = (!String.IsNullOrEmpty(query1.POL)) ? query1.POL : "No Data";
txtPOD.Text = (!String.IsNullOrEmpty(query1.POD)) ? query1.POD : "No Data";
txtFPOD.Text = (!String.IsNullOrEmpty(query1.FPOD)) ? query1.FPOD : "No Data";
txtEquipmentType.Text = (!String.IsNullOrEmpty(query1.txtEquipmentType)) ? query1.txtEquipmentType : "No Data";
txtCommodity.Text = (!String.IsNullOrEmpty(query1.txtCommodity)) ? query1.txtCommodity : "No Data";
txtNamedAccount.Text = (!String.IsNullOrEmpty(query1.NamedAcct)) ? query1.NamedAcct : "No Data";
chkCrossTrade.Checked = (query1.Cross_Trade != null) ? query1.Cross_Trade.Value : false;
chkHaz.Checked = (query1.Haz != null) ? query1.Haz.Value : false;
chkBreakBulk.Checked = (query1.Break_Bulk != null) ? query1.Break_Bulk.Value : false;
chkReefer.Checked = (query1.Reefer != null) ? query1.Reefer.Value : false;
txtTargetTEU.Text = (!String.IsNullOrEmpty(query1.txtTargetTEU)) ? query1.txtTargetTEU : "No Data";
txtFrequency.Text = (!String.IsNullOrEmpty(query1.Frequency)) ? query1.Frequency : "No Data";
txtFollowUpTarget.Text = (query1.FollowUpDueDate != null) ? query1.FollowUpDueDate.ToString() : "No Date";
txtUKSalesComments.Text = (!String.IsNullOrEmpty(query1.UKSalesComments)) ? query1.UKSalesComments : "";
chkSecured.Checked = (query1.Secured != null) ? query1.Secured.Value : false;
chkLost.Checked = (query1.Lost != null) ? query1.Lost.Value : false;
txtReasonForLost.Text = (!String.IsNullOrEmpty(query1.txtLostQuoteReasons)) ? query1.txtLostQuoteReasons : "";
#region Get current users' login name and strip it down
string loggedUName = WindowsIdentity.GetCurrent().Name.ToString();
lblFollowedUpByVal.Text = loggedUName.Substring(loggedUName.IndexOf('\\') + 1);
#endregion
lblFollowedUpDateVal.Text = (query1.dateOfFollowUp != null) ? query1.dateOfFollowUp.ToString() : "No Date";
#endregion
#region Drop down box initilisations
#region TMFddl
string[] ddlTMFValues = new string[] { "", "Target", "Maintenance", "Fishing", "Unqualified", "NOT COMPLETE" };
foreach (string val in ddlTMFValues)
{
ddlTMF.Items.Add(val);
}
ddlTMF.SelectedValue = query1.TMF.ToString();
#endregion
#region Quoted By ddl
#region ddlQuotedByQuery
var ddlQuotedByQuery = (from r in db.tblEmployees
where !r.txtSalesPerson.ToUpper().Contains("(")
select new
{
r.ID,
r.txtSalesPerson
}).Distinct().ToList();
#endregion
#region DDL Value Assignment
ddlQuotedBy.DataSource = ddlQuotedByQuery;
ddlQuotedBy.DataTextField = "txtSalesPerson";
ddlQuotedBy.DataValueField = "ID";
ddlQuotedBy.DataBind();
ddlQuotedBy.SelectedValue = query1.QuotedBy.ToString();
#endregion
#endregion
//ddlCustomer
//ddlLocation
#endregion
}
else
{
NameValueCollection nvc = Request.Form;
hnIDPostedVal = nvc[aspControlNotation + "hnID"];
nID = Convert.ToInt32(hnIDPostedVal);
var query1 = (from r in db.tblQuoteLogs
where (r.nID == nID)
join emp in db.tblEmployees on r.QuotedBy equals emp.ID into r1
join cus in db.tblCustomers on r.luLocation equals cus.ID into r2
join qfr in db.tblQuoteFailReasons on r.ReasonForLost equals qfr.ID into r3
from x in r1.DefaultIfEmpty()
from y in r2.DefaultIfEmpty()
from z in r3.DefaultIfEmpty()
orderby r.LogDate descending
select new
{
#region Selected Fields
nID = r.nID,
Quote_Reference = r.Quote_Reference,
Validity = r.Validity,
TMF = r.TMF,
QuotedBy = r.QuotedBy,
EnteredBy = r.EnteredBy,
LogDate = r.LogDate,
luCustomer = r.luCustomer,
luLocation = r.luLocation,
txtContact = r.txtContact,
txtTelephone = r.txtTelephone,
POO = r.POO,
POL = r.POL,
POD = r.POD,
FPOD = r.FPOD,
txtEquipmentType = r.txtEquipmentType,
txtCommodity = r.txtCommodity,
NamedAcct = r.NamedAcct,
Cross_Trade = r.Cross_Trade,
Haz = r.Haz,
Break_Bulk = r.Break_Bulk,
Reefer = r.Reefer,
txtTargetTEU = r.txtTargetTEU,
Frequency = r.Frequency,
FollowUpDueDate = r.FollowUpDueDate,
UKSalesComments = r.UKSalesComments,
Secured = r.Secured,
Lost = r.Lost,
txtLostQuoteReasons = z.txtLostQuoteReasons,
dateOfFollowUp = r.dateOfFollowUp,
txtSalesPerson = x.txtSalesPerson, //Gets ID from the QuotedBy column to get the particular sales person name
txtLocation = y.txtLocation //Gets ID from the luLocation column to get the particular location
#endregion
}).First();
//pageInitialisation(query1); //THIS IS WHERE I WAS GOING TO PASS THE QUERY FOR PAGE ELEMENT INITIALISATION BUT ANONYMOUS TYPE ERROR STOPS ME
#region Initialise Page Elements (With Checks for null fields)
txtQuoteRef.Text = (!String.IsNullOrEmpty(query1.Quote_Reference)) ? query1.Quote_Reference : "No Data";
txtValidity.Text = (query1.Validity != null) ? query1.Validity.ToString() : "No Date";
//txtTMF.Text = (!String.IsNullOrEmpty(query1.TMF)) ? query1.TMF: "No Data";
//txtQuotedBy.Text = (!String.IsNullOrEmpty(query1.txtSalesPerson)) ? query1.txtSalesPerson : "No Data"; //BUGGY DUE TO NULL ENTRIES
lblEnteredByVal.Text = (!String.IsNullOrEmpty(query1.EnteredBy)) ? query1.EnteredBy : "No Data";
lblLogDateVal.Text = (query1.LogDate != null) ? query1.LogDate.ToString() : "No Date";
txtCustomer.Text = (!String.IsNullOrEmpty(query1.luCustomer)) ? query1.luCustomer : "No Data";
txtLocation.Text = (!String.IsNullOrEmpty(query1.txtLocation)) ? query1.txtLocation : "No Data"; //BUGGY DUE TO NULL ENTRIES
txtContact.Text = (!String.IsNullOrEmpty(query1.txtContact)) ? query1.txtContact : "No Data";
txtTelephone.Text = (!String.IsNullOrEmpty(query1.txtTelephone)) ? query1.txtTelephone : "No Data";
txtPOO.Text = (!String.IsNullOrEmpty(query1.POO)) ? query1.POO : "No Data";
txtPOL.Text = (!String.IsNullOrEmpty(query1.POL)) ? query1.POL : "No Data";
txtPOD.Text = (!String.IsNullOrEmpty(query1.POD)) ? query1.POD : "No Data";
txtFPOD.Text = (!String.IsNullOrEmpty(query1.FPOD)) ? query1.FPOD : "No Data";
txtEquipmentType.Text = (!String.IsNullOrEmpty(query1.txtEquipmentType)) ? query1.txtEquipmentType : "No Data";
txtCommodity.Text = (!String.IsNullOrEmpty(query1.txtCommodity)) ? query1.txtCommodity : "No Data";
txtNamedAccount.Text = (!String.IsNullOrEmpty(query1.NamedAcct)) ? query1.NamedAcct : "No Data";
chkCrossTrade.Checked = (query1.Cross_Trade != null) ? query1.Cross_Trade.Value : false;
chkHaz.Checked = (query1.Haz != null) ? query1.Haz.Value : false;
chkBreakBulk.Checked = (query1.Break_Bulk != null) ? query1.Break_Bulk.Value : false;
chkReefer.Checked = (query1.Reefer != null) ? query1.Reefer.Value : false;
txtTargetTEU.Text = (!String.IsNullOrEmpty(query1.txtTargetTEU)) ? query1.txtTargetTEU : "No Data";
txtFrequency.Text = (!String.IsNullOrEmpty(query1.Frequency)) ? query1.Frequency : "No Data";
txtFollowUpTarget.Text = (query1.FollowUpDueDate != null) ? query1.FollowUpDueDate.ToString() : "No Date";
txtUKSalesComments.Text = (!String.IsNullOrEmpty(query1.UKSalesComments)) ? query1.UKSalesComments : "";
chkSecured.Checked = (query1.Secured != null) ? query1.Secured.Value : false;
chkLost.Checked = (query1.Lost != null) ? query1.Lost.Value : false;
txtReasonForLost.Text = (!String.IsNullOrEmpty(query1.txtLostQuoteReasons)) ? query1.txtLostQuoteReasons : "";
#region Get current users' login name and strip it down
string loggedUName = WindowsIdentity.GetCurrent().Name.ToString();
lblFollowedUpByVal.Text = loggedUName.Substring(loggedUName.IndexOf('\\') + 1);
#endregion
lblFollowedUpDateVal.Text = (query1.dateOfFollowUp != null) ? query1.dateOfFollowUp.ToString() : "No Date";
#endregion
#region Drop down box initilisations
#region TMFddl
string[] ddlTMFValues = new string[] { "", "Target", "Maintenance", "Fishing", "Unqualified", "NOT COMPLETE" };
foreach (string val in ddlTMFValues)
{
ddlTMF.Items.Add(val);
}
ddlTMF.SelectedValue = query1.TMF.ToString();
#endregion
//ddlQuotedByQuery which populates drop down list
var ddlQuotedByQuery = (from r in db.tblEmployees
where !r.txtSalesPerson.ToUpper().Contains("(")
select new
{
r.ID,
r.txtSalesPerson
}).Distinct().ToList();
ddlQuotedBy.DataSource = ddlQuotedByQuery;
ddlQuotedBy.DataTextField = "txtSalesPerson";
ddlQuotedBy.DataValueField = "ID";
ddlQuotedBy.DataBind();
ddlQuotedBy.SelectedValue = query1.QuotedBy.ToString();
#endregion
#endregion
//ddlCustomer
//ddlLocation
#endregion
}
#endregion
}
protected void btnNext_Click(object sender, EventArgs e)
{
db = new DataClassesDataContext((string)Session["Conn"]);
int nID = Convert.ToInt32(hnID.Value);
//inverted to work correctly due to query ordering
var queryNext = (from r in db.tblQuoteLogs
where r.nID < nID
orderby r.nID descending
select new
{
r.nID
});
if (queryNext.Count() > 0)
{
hnID.Value = queryNext.Select(o => o.nID).First().ToString();
Response.Redirect("~/QuoteInformation/Default.aspx");
}
}
protected void btnPrevious_Click(object sender, EventArgs e)
{
//db = new DataClassesDataContext((string)Session["Conn"]);
//int nID = Convert.ToInt32(hnID.Value);
////inverted to work correctly due to query ordering
//var queryPrevious = (from r in db.tblQuoteLogs
// where r.nID > nID
// orderby r.nID ascending
// select new
// {
// r.nID
// });
//if (queryPrevious.Count() > 0)
//{
// hnID.Value = queryPrevious.Select(o => o.nID).First().ToString();
// Response.Redirect("~/QuoteInformation/Default.aspx?nID=" + hnID.Value);
//}
}
很抱歉代码很不稳定:s,但我们将不胜感激:)。