日期:2014-05-18  浏览次数:20423 次

怎么动态改变链接内容

这段代码怎么不灵呢?那个hyperLink根本没法点击。怎么动态改变链接内容?
C# code
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {


        if (DropDownList1.Text == "qq")                // 如果选择qq 
        {

            HyperLink1.Text = "qq";                 // 文本为qq 
            HyperLink1.NavigateUrl = "http://www.qq.com";        //URL 为qq.com 

        }
        else                          // 选择sohu 
        {
            HyperLink1.Text = "sohu";                // 文本为sohu 
            HyperLink1.NavigateUrl = "http://www.sohu.com";       //URL 为sohu.com 

        }


    }



------解决方案--------------------
DropDownList1的属性AutoPostBack="True"
------解决方案--------------------
为DropDownList加一个属性AutoPostBack,并设为true。

最后重构一下你的代码,去年if。
C# code

 protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (DropDownList1.SelectedIndex == -1) return;

        HyperlinkBind(DropDownList1.SelectedItem.Text);
    }

    private void HyperlinkBind(string name)
    { 
         HyperLink1.Text = name;               
         HyperLink1.NavigateUrl = "http://www." + name + ".com";    
    }

------解决方案--------------------
可以换一种思路:

不用HyperLink控件!直接用一个Label控件,让Label.Text="<a href=.....>qq</a>";就可以了。

在asp.net中,尽量不要使用服务器控件,你懂得!
------解决方案--------------------
参考这里:http://www.cnblogs.com/insus/archive/2012/02/23/2364697.html
对比一下,那里有写得不一样。