views:

13

answers:

1

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.UrlEncode(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...

A: 

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)";
        //
    }
}
Rubens Farias

related questions