Send string with QueryString in Repeater Control in ASP.net

╄→尐↘猪︶ㄣ 提交于 2019-12-01 12:50:09
<asp:HyperLink ID="HyperLink3" NavigateUrl='<%#Eval("ID_Message","~/ADMIN/Reply.aspx?ID={0}") %>' runat="server">OK</asp:HyperLink>

Let's start with the format of the QueryString. The QueryString looks like the following:

http://www.mysite.com/somepage.aspx?id=? 

Right!

Of course you can pass multiple parameters in QueryString using the "&" symbol as shown below:

http://www.mysite.com/somepage.aspx?id=?&foo=?  

Now, you need to do the same thing but inside the Repeater control and using the values from the database.

<asp:Repeater>

<ItemTemplate>
<a href="http://www.mysite.com/somepage.aspx?id=<%# Eval("Id") #>"><Eval("Title")</a>
</ItemTemplate>
</asp:Repeater>

The Eval("Id") is the property from your data source which can be DataSet, DataTable, Entity classes etc.

The <%# Eval("Id") #> will be called when you bind the Repeater control. You bind the Repeater control using the Repeater.DataBind() method.

Getting the id on the other page:

if(Request.QueryString["id"] != null) 
{
   string id = Request.QueryString["id"] as String; 
}

Here's an example from within an ItemTemplate in a Repeater to give you an idea. Latitude and Longitude come from a database

<a target="_blank" class="newwindow" href="http://maps.google.com/maps?saddr=<%=addressTextBox.Text%>&daddr=<%#Eval("Latitude")%>,<%#Eval("Longitude") %>">

Since everyone has posted code with all the evaluation embedded in the aspx page I will post one with all the code required in the code behind (where I prefer all this code to be).

First in your repeater you will need a control:

<asp:Repeater>
    <ItemTemplate>
        <asp:HyperLink ID="hrefLink"
            href="http://www.mysite.com/somepage.aspx?id={0}&more={1}"
            OnDataBinding="hrefLink_DataBinding">
        </asp:HyperLink>
    </ItemTemplate>
</asp:Repeater>

Then in your code behind you implement the databinding to fill in your links details:

protected void hrefLink_DataBinding(object sender, System.EventArgs e)
{
    HyperLink link = (HyperLink)(sender);
    // Fill in your links details
    link.NavigateUrl = string.Format(link.NavigateUrl,
        Eval("ID").ToString(), Eval("More").ToString());
    link.Text = Eval("LinkTitle").ToString();        
}

The advantage to this is that you can easily add more logic when needed without cluttering your aspx page with tons of code. I prefer this method to inline but they are both valid solutions and it's more of a preference.

If you don't want to predefine where the link would go you could change the above databinding code to rewrite the entire NavigateUrl to whatever you want. So based on some evalulated value you could redirect to different pages. It's the most customizeable solution.

Side note: Make sure you turn off ViewState on repeaters if you don't need it as it causes a ton of clutter.

Use Eval method to evaluate a field into your rendered page. For example you have a Products table, and you want a link that passes a product's id to showproduct.aspx. Check the example below :

<a href='showproduct.aspx?productid=<%#Eval("ProductID")%>'>View Details</a>

At showproduct.aspx use Page.Request.QueryString collection to get product :

string productId = Page.Request.QueryString["productid"].ToString();
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!