【问题标题】:How do I get SelectedValue to bother returning the actual selected value如何让 SelectedValue 麻烦返回实际选定的值
【发布时间】:2009-05-28 10:37:56
【问题描述】:

我正在使用以下代码块从 xml 文件中的内容填充页面加载时的下拉框...

foreach (System.Xml.XmlNode item in root.SelectNodes(@"/markers/marker"))
            {
                string tmpValue = item.Attributes["location"].Value + "#" + item.Attributes["lat"].Value + "#" + item.Attributes["lng"].Value;
                destination.Items.Add(new ListItem(item.Attributes["location"].Value, tmpValue));
                tmpNCount++;
            }

然后我有一个按钮,我打算用它来执行搜索。

protected void qsearch_Click(object sender, EventArgs e) {

try
{
    string httpPath = Convert.ToString(UrlMaker.httpURL());
    //sysmessage.Text = "TEST" + destination.SelectedValue.ToString();
    string[] Split = destination.SelectedValue.ToString().Split(new Char[] { '#' });
    string tmpLocation = Convert.ToString(Split[0]);
    string tmpLat = Convert.ToString(Split[1]);
    string tmpLon = Convert.ToString(Split[2]);
    string tmpRad = radius.SelectedValue.ToString();


    Response.Redirect(httpPath + "search.aspx?func=longlat&country=gbr&lng="+tmpLon+"&lat="+tmpLat+"&rad="+tmpRad+"&txt="+tmpLocation+"&test=1");
}
catch(Exception ex) { Response.Write("Error on Redirect"); }

}

搜索运行,但该死的东西返回下拉菜单中第一项的值,而不是我选择的那个。

希望我在这里遗漏了一些非常明显的东西。

谢谢


已解决

将点击功能清除为毫无意义并使用以下内容。

if (IsPostBack)
        {

            try
            {
                string fdata = Request.Form["dest"];
                string[] Split = fdata.ToString().Split(new Char[] { '#' });
                    string tmpLocation = Convert.ToString(Split[0]);
                    string tmpLat = Convert.ToString(Split[1]);
                    string tmpLon = Convert.ToString(Split[2]);
                    string tmpRad = radius.SelectedValue.ToString();                
                Response.Redirect(httpPath + "search.aspx?func=longlat&country=gbr&lng=" + tmpLon + "&lat=" + tmpLat + "&rad=" + tmpRad + "&txt=" + tmpLocation + "&test=1");
            }
            catch(Exception ex) { Response.Write("Error on Redirect" + ex.ToString()); }

        }

【问题讨论】:

    标签: asp.net asp.net-2.0 c#-2.0


    【解决方案1】:

    您猜测您正在清除下拉列表并调用您的代码以在每个页面加载时填充它,这将导致第一个项目被选中。

    您应该确保此代码在 if(!IsPostBack){ ... }

    【讨论】:

    • 简直不敢相信;它在那里,一些笨蛋删除了它。多亏了 SVN,我现在可以用鱼打他们了。谢谢
    • 这最终并没有从技术上解决问题(尽管它丢失了)我只是使用了 Request.Form 这真的很有意义。
    猜你喜欢
    • 2012-11-21
    • 1970-01-01
    • 2011-08-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-04
    • 1970-01-01
    相关资源
    最近更新 更多