首页 文章

DropDownlist PostBack失去选定的值

提问于
浏览
1

我有一个下拉列表,从文本文件加载所有列表项,它包含打印机名称,描述,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

当用户点击下拉列表时,他们会看到:

when drop down is clicked

所以现在用户选择一台打印机,所以选择的索引是> 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;但这也没有帮助 .

plese help I cant seem to figure out, every time the user selects a value there is a postback and after the postback "-" is selected as the printer value.

2 回答

  • 1

    只需在页面加载时检查 ISPOSTBACK 即可

    protected void Page_Load(object sender, EventArgs e)
    {
      if(!IsPostBack) 
       LoadPrinterList();
    }
    
  • 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 事件中进行类似的检查,因为事件将在页面内触发(即在已经加载之后),因此该语句永远不应该评估为true .

相关问题