我有一个下拉列表,用于加载文本文件中的所有列表项,它包含打印机名称,描述,ipaddress和连接字符串ID的列表。所以我向用户显示打印机名称描述,当用户选择打印机时,我传递的值是ip地址或连接字符串,具体取决于具体情况。
protected void Page_Load(object sender, EventArgs e)
{
LoadPrinterList();
}
protected void LoadPrinterList()
{
string CSVFilePathName =
System.Configuration.ConfigurationManager.AppSettings["FilePath"];
string[] Lines = File.ReadAllLines(CSVFilePathName);
string[] Fields;
Fields = Lines[0].Split(new char[] { ',' });
int Cols = Fields.GetLength(0);
DataTable dt = new DataTable();
//1st row must be column names;
//force lower case to ensure matching later on.
for (int i = 0; i < Cols; i++)
dt.Columns.Add(Fields[i].ToLower(), typeof(string));
dt.Columns.Add("nameanddescription",
typeof(string), "name +'-'+ description");
dt.Columns.Add("ipandconnectionstring",
typeof(string), "ip +'-'+ ConncetionStringID");
DataRow Row;
for (int i = 1; i < Lines.GetLength(0); i++)
{
Fields = Lines[i].Split(new char[] { ',' });
Row = dt.NewRow();
for (int f = 0; f < Cols; f++)
Row[f] = Fields[f];
dt.Rows.Add(Row);
}
string hostname = Request.UserHostName.Substring(0, 3);
string[] name = Printerlist.SelectedValue.Split('-');
//string plant = name[0].ToString();
//string plantvalue = plant.Substring(0, 3);
//if(hostname == plantvalue)
//{
Printerlist.DataTextField = "nameanddescription";
Printerlist.DataValueField = "ipandconnectionstring";
//}
Printerlist.DataSource = dt;
Printerlist.DataBind();
}
What the user sees first as an option for the printer list
当用户点击下拉列表时,他们会看到:
所以现在用户选择一台打印机,所以选择的索引是> 0基于此我在后面的代码中执行以下操作。
protected void Printerlist_SelectedIndexChanged(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
}
else
{
if (Printerlist.SelectedItem.Text.Length > 0)
{
TxtItem.Focus();
}
else
{
TxtItem.Text = string.Empty;
TxtQty.Text = string.Empty;
DropDownList2.SelectedIndex = 0;
lbldesc.Visible = false;
//TxtBase.Text = string.Empty;
//TxtBase1.Text = string.Empty;
bestbeforewillbe.Text = string.Empty;
TxtBestBeforeMonths.Text = string.Empty;
TxtRotcode.Text = string.Empty;
zplcode.Text = string.Empty;
string message = "The selected printer is
not a local printer";
System.Text.StringBuilder sb =
new System.Text.StringBuilder();
sb.Append("<script type = 'text/javascript'>");
sb.Append("window.onload=function(){");
sb.Append("alert('");
sb.Append(message);
sb.Append("')};");
sb.Append("</script>");
ClientScript.RegisterClientScriptBlock
(this.GetType(), "alert", sb.ToString());
}
}
}
这是我的aspx页面中的下拉列表值
<asp:DropDownList ID="Printerlist" runat="server"
Height="16px" Width="268px" AutoPostBack="True" OnSelectedIndexChanged="Printerlist_SelectedIndexChanged" OnTextChanged="Printerlist_TextChanged" ViewStateMode="Enabled"></asp:DropDownList>
我有EnableViewState = true;但这也没有帮助。
请求帮助我似乎无法弄清楚,每次用户选择一个值时都会有一个回发,并且在回发后选择“ - ”作为打印机值。
答案 0 :(得分:1)
只需在页面加载时检查ISPOSTBACK
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
LoadPrinterList();
}
答案 1 :(得分:0)
您需要明确检查是否发生了PostBack
,否则只要您的Page_Load
方法被触发,它就会使用初始数据重新填充您的控件(即您在那里丢失了更改)
您只需添加一个if语句,只有在初始加载时才执行LoadPrinterList()
方法:
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
// Only perform this on the initial load
LoadPrinterList();
}
}
此外,您不应该在Printerlist_SelectedIndexChanged
事件中进行类似的检查,因为事件将在页面内触发(即在已经加载之后),因此该语句永远不应评估为真。