if ((e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["LanguageID"].ToString()) == null)
{
Int32 LangID = -1;
string QuesTypeID = com.Encrypt(TypeID.ToString(), com.KeyCode);
string LanguageID = com.Encrypt(LangID.ToString(), com.KeyCode);
string QuesID = com.Encrypt(ID.ToString(), com.KeyCode);
string PID = com.Encrypt(radGridQues.CurrentPageIndex.ToString(), com.KeyCode.ToString());
Response.Redirect(ROSDAAB.Constants.SiteURL + "editQues/" + "QuesID/" + QuesID + "/" + PID + "/" + QuesTypeID + "/" + LanguageID);
}
else
{
Int32 LangID = Convert.ToInt32(e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["LanguageID"].ToString());
string QuesTypeID = com.Encrypt(TypeID.ToString(), com.KeyCode);
string LanguageID = com.Encrypt(LangID.ToString(), com.KeyCode);
string QuesID = com.Encrypt(ID.ToString(), com.KeyCode);
string PID = com.Encrypt(radGridQues.CurrentPageIndex.ToString(), com.KeyCode.ToString());
Response.Redirect(ROSDAAB.Constants.SiteURL + "editQues/" + "QuesID/" + QuesID + "/" + PID + "/" + QuesTypeID + "/" + LanguageID);
}
[编辑] 这是IF else块中的重复代码...如何缩短它?
答案 0 :(得分:3)
Int32 LangID = -1;
if (e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["LanguageID"] != null)
{
LangID = Convert.ToInt32(e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["LanguageID"].ToString());
}
string QuesTypeID = com.Encrypt(TypeID.ToString(), com.KeyCode);
string LanguageID = com.Encrypt(LangID.ToString(), com.KeyCode);
string QuesID = com.Encrypt(ID.ToString(), com.KeyCode);
string PID = com.Encrypt(radGridQues.CurrentPageIndex.ToString(), com.KeyCode.ToString());
Response.Redirect(ROSDAAB.Constants.SiteURL + "editQues/" + "QuesID/" + QuesID + "/" + PID + "/" + QuesTypeID + "/" + LanguageID);
这是我在这里所做的:
答案 1 :(得分:1)
为什么要使用ToString()?
if (e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["LanguageID"] == null)
请注意基础类型
Int32 LangID = (Int32)e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["LanguageID"];
至于更短的时间:
int LangID = e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["LanguageID"] != null ? (int)e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["LanguageID"] : -1;
string QuesTypeID = com.Encrypt(TypeID.ToString(), com.KeyCode);
string LanguageID = com.Encrypt(LangID.ToString(), com.KeyCode);
string QuesID = com.Encrypt(ID.ToString(), com.KeyCode);
string PID = com.Encrypt(radGridQues.CurrentPageIndex.ToString(), com.KeyCode.ToString());
Response.Redirect(ROSDAAB.Constants.SiteURL + "editQues/" + "QuesID/" + QuesID + "/" + PID + "/" + QuesTypeID + "/" + LanguageID);
如果空检查不起作用,请尝试!Convert.IsDBNull(),
,如果您的值来自数据源。
int LangID = !Convert.IsDBNull(e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["LanguageID"]) ? (int)e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["LanguageID"] : -1;
答案 2 :(得分:1)
您在下面编码: -
bool isOk = String.IsNullOrEmpty(e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["LanguageID"].ToString());
Int32 LangID = isOk ? -1 : Convert.ToInt32(e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["LanguageID"].ToString()); ;
string QuesTypeID = com.Encrypt(TypeID.ToString(), com.KeyCode);
string LanguageID = com.Encrypt(LangID.ToString(), com.KeyCode);
string QuesID = com.Encrypt(ID.ToString(), com.KeyCode);
string PID = com.Encrypt(radGridQues.CurrentPageIndex.ToString(), com.KeyCode.ToString());
Response.Redirect(ROSDAAB.Constants.SiteURL + "editQues/" + "QuesID/" + QuesID + "/" + PID + "/" + QuesTypeID + "/" + LanguageID);
答案 3 :(得分:1)
您还可以使用三元运算符来减少重复:
var tempLangId = e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["LanguageID"];
Int32 LangID = tempLangId == null ? -1 : Convert.ToInt32(tempLangId);
答案 4 :(得分:1)
string QuesTypeID = com.Encrypt(TypeID.ToString(),com.KeyCode); string LanguageID = com.Encrypt(LangID.ToString(),com.KeyCode); string QuesID = com.Encrypt(ID.ToString(),com.KeyCode); string PID = com.Encrypt(radGridQues.CurrentPageIndex.ToString(),com.KeyCode.ToString());
Int32 LangId = -1;
if ((e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["LanguageID"].ToString()) != null)
{
Int32 LangID = Convert.ToInt32(e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["LanguageID"].ToString());
}
Response.Redirect(ROSDAAB.Constants.SiteURL + "editQues/" + "QuesID/" + QuesID + "/" + PID + "/" + QuesTypeID + "/" + LanguageID);
答案 5 :(得分:0)
好的,我试过这个..工作
if ((e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["LanguageID"].ToString()) == String.Empty)