开发者

Set querystring parameter in Hyperlink from selected.item.text in dropdownlist

I have a HyperLink control in a ListView, and I need to pass a couple of querystring parameters.

<asp:HyperLink ID="HyperLink" runat="server" NavigateUrl='<%# string.Format("Page.aspx?Param1={0}", Server.Ur开发者_StackOverflow中文版lEncode(Container.DataItem.ToString())) %>'  
Text='<%# Container.DataItem %>' />  

The first parameter is based on the current DataItem value in the LV.

Suppose I want to set a second parameter with a value from Selected.Item.Text in a DropDownList on the same page, how would the HyperLink look?

I've spent too much time on this, so I need some help...

Thnx...


Try this:

<form id="form1" runat="server">
    <asp:DropDownList runat="server" ID="DropDownList">
        <asp:ListItem Text="1" Value="1" />
        <asp:ListItem Text="2" Value="2" />
        <asp:ListItem Text="3" Value="3" />
    </asp:DropDownList>
    <asp:GridView runat="server" ID="sample" AutoGenerateColumns="true">
        <Columns>
            <asp:TemplateField>
                <ItemTemplate>
                    <asp:HyperLink ID="HyperLink" runat="server"
                        OnDataBinding="HyperLink_DataBinding"
                        NavigateUrl='<%# String.Format(
                            "Default.aspx?Param1={0}", 
                            Server.UrlEncode(Container.DataItem.ToString())) %>'
                        Text='<%# Container.DataItem %>' />  
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>
</form>

And this code behind:

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            sample.DataSource = new string[] { "A", "B", "C" };
            sample.DataBind();
        }
    }

    protected void HyperLink_DataBinding(object sender, EventArgs e)
    {
        ((HyperLink)sender).Attributes["onclick"] =
            "alert(this.href + '&Param2='+ " + DropDownList.ClientID + ".value)";
        //
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜