Set Gridview DataNavigateUrlFormatString Dynamically inside User Control(ASCX)
Asked Answered
B

1

3

i have a issue that i dont know if it can be done.

i have an in an aspx page , that loads dedpends on user activity a user control (ascx).

the user control has a gridview which one of my columns are hyperlink.

<asp:HyperLinkField  DataNavigateUrlFormatString='<%page %>'
        DataTextField="ReqId" HeaderText="Request No." DataNavigateUrlFields="ReqId" />

i want that on click of that hyperlink , it will direct to the same page with parameters, but i cant do it right. for some reason so i tried this one:

<%string page = Page.Request.Path + "reqid={0}"; %>

but in the page the link refers to %page% as a string . can someone pls direct me how to it.

p.s it used to work when it was like that and the ascx was in root folder of the solution, the problem start when i moved all my controls to a folder in the root named "Controls"

<asp:HyperLinkField  DataNavigateUrlFormatString="?reqid={0}"
        DataTextField="ReqId" HeaderText="מספר בקשה" DataNavigateUrlFields="ReqId" />

thanks in advance.

Bega answered 24/6, 2011 at 14:22 Comment(1)
i forgot to mention that several pages(aspx) load that control , and each time the url is different . so i need to do it per calliing page.Bega
P
8

Use a template field and add hyperlink to it.

<asp:TemplateField HeaderText="Request No.">
   <ItemTemplate>
     <asp:HyperLink ID="EditHyperLink1" runat="server" 
          NavigateUrl='<%# Page.Request.Path + "?reqid=" + Eval("ReqId") %>'
          Text='<%# Eval("ReqId") %>' >
     </asp:HyperLink>
   </ItemTemplate>
</asp:TemplateField>

Or you can use GridView_RowDatabound event handler and change the Navigateurl for the particular control.

protected void myGV_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        HyperLink myHL = (HyperLink)e.Row.FindControl("EditHyperLink1");
        myHL.NavigateUrl = Page.Request.Path + "?reqid=" + e.Row.Cells[0].Text.ToString();
    }
}

I have assumed that ReqId is present in 1st cell.

Plumper answered 24/6, 2011 at 15:42 Comment(3)
can you give me an example of the GridView_RowDatabound?Bega
thanks askids!! just a last question what for your opinion is better?Bega
I think the 1st approach is simpler. But if you need to advanced parsing or some other manipulations, then use rowdatabound. For example, I was generating a link with field value having '+' symbol. The '+' symbol had to be converted to %2B for the link to actually work. In such case, I had to use rowdatabound to string manipulation. So whenever given a chance I would still go for 1st option for its simplicity.Plumper

© 2022 - 2024 — McMap. All rights reserved.