C# DropDownlist PostBack释放选定值
我有一个下拉列表,它从文本文件中加载所有列表项,其中包含打印机名称、说明、ip地址和连接字符串id的列表。因此,我向用户显示打印机名称说明,当用户选择打印机时,我传递的值是ip地址或连接字符串,具体取决于情况C# DropDownlist PostBack释放选定值,c#,asp.net,text-files,postback,C#,Asp.net,Text Files,Postback,我有一个下拉列表,它从文本文件中加载所有列表项,其中包含打印机名称、说明、ip地址和连接字符串id的列表。因此,我向用户显示打印机名称说明,当用户选择打印机时,我传递的值是ip地址或连接字符串,具体取决于情况 protected void Page_Load(object sender, EventArgs e) { LoadPrinterList(); } protected void LoadPrinterList() { string CSVFilePathName =
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();
}
受保护的无效页面加载(对象发送方,事件参数e)
{
LoadPrinterList();
}
受保护的void LoadPrinterList()
{
字符串CSVFilePathName=
System.Configuration.ConfigurationManager.AppSettings[“文件路径”];
string[]Lines=File.ReadAllLines(CSVFilePathName);
字符串[]字段;
字段=行[0]。拆分(新字符[]{',});
int Cols=Fields.GetLength(0);
DataTable dt=新的DataTable();
//第一行必须是列名;
//强制使用小写字母,以确保以后匹配。
for(int i=0;i
当用户单击下拉列表时,他们会看到:
因此,现在用户选择一台打印机,因此所选索引>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());
}
}
protectedvoid Printerlist\u SelectedIndexChanged(对象发送方,事件参数e)
{
如果(!Page.IsPostBack)
{
}
其他的
{
如果(Printerlist.SelectedItem.Text.Length>0)
{
焦点();
}
其他的
{
Text=string.Empty;
TxtQty.Text=string.Empty;
DropDownList2.SelectedIndex=0;
lbldesc.Visible=false;
//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=“所选打印机已关闭。”
不是本地打印机”;
System.Text.StringBuilder sb=
新System.Text.StringBuilder();
某人加上(“”);
sb.Append(“window.onload=function(){”);
某人加上(“警告(”);
某人附加(信息);
某人加上(“)};”;
某人加上(“”);
ClientScript.RegisterClientScriptBlock
(this.GetType(),“alert”,sb.ToString());
}
}
}
这是我的aspx页面中的dropdownlist值
<asp:DropDownList ID="Printerlist" runat="server"
Height="16px" Width="268px" AutoPostBack="True" OnSelectedIndexChanged="Printerlist_SelectedIndexChanged" OnTextChanged="Printerlist_TextChanged" ViewStateMode="Enabled"></asp:DropDownList>
我有EnableViewState=true;但这也没用
请提供帮助,我似乎无法理解,每次用户选择一个值时,都会有一个回发,并且在回发之后,会选择“-”作为打印机值。只需在页面加载时选中
ISPOSTBACK
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
LoadPrinterList();
}
您需要明确检查是否发生了
PostBack
,否则在触发Page\u Load
方法的任何时候,它都会用控件的初始数据重新填充控件(即,您在那里丢失了更改)
您只需添加一条if语句,并且仅在初始加载时执行LoadPrinterList()
方法:
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
// Only perform this on the initial load
LoadPrinterList();
}
}
此外,您不需要在
Printerlist\u SelectedIndexChanged
事件中进行类似的检查,因为事件将从页面内触发(即,在已加载事件之后),因此该语句的计算结果不应为true。我讨厌这种情况。。。。如果(!Page.IsPostBack){}其他{//do stuff}。。。为什么不只是如果(Page.IsPostBack){//do stuff}哦,是的,你说的对,我在检查每个控件是否是回发的,你的方法工作得更好,谢谢你的及时回复