5

This is my code behind in C# .

protected void Page_Load(object sender, EventArgs e)
    {
           Name = "Nitin";                      
    } 

I have a GridView in which there is a Hyperlinkfield.I want to send Name from C# page(one in code behind) to next page via HyperLinkField but it is not one of the BoundField's of the GridView(i.e. Which I get from EntityDataSource). This is my asp.net code.

code:

   <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="Id" DataSourceID="WorkItemsEntityDataSource">
    <Columns>
        <asp:hyperlinkfield datatextfield="Id"                    
                datanavigateurlfields="Id"
                datanavigateurlformatstring="~\WorkItems.aspx?Id={0}"          
                headertext="Id"/> 
        <asp:BoundField DataField="Description" HeaderText="Description" SortExpression="Description" />
        <asp:BoundField DataField="TFSId" HeaderText="TFSId" SortExpression="TFSId" />
        <asp:CheckBoxField DataField="IsBillable" HeaderText="IsBillable" SortExpression="IsBillable" />
    </Columns>
</asp:GridView>
1
  • post your grid view code and elaborate ur problem clearly.. Commented Apr 10, 2013 at 10:42

4 Answers 4

4

You can pass navigation URL from code behind also like this

In Aspx Page

              <asp:TemplateField>
                    <ItemTemplate>
                        <asp:HyperLink ID="hyp" runat="server" Text="Navigation"></asp:HyperLink>
                    </ItemTemplate>
                </asp:TemplateField>    

In Code behind User grid view data bound event like this

protected void grddata_RowDataBound(object sender, GridViewRowEventArgs e)
    {    
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            int ID = Convert.Int32(DataBinder.Eval(e.Row.DataItem, "ID"));
            HyperLink hyp = (HyperLink)e.Row.FindControl("hyp");
            hyp.NavigateUrl = "Test.aspx?Name='" + Request.QueryString["test"] + "'&&ID"+ ID +"";
        }
    }

i think this will help you...

Sign up to request clarification or add additional context in comments.

1 Comment

Hi Rajpurohit ,I am wondering how to send Id from bound field of GridView with Name also via HyperLinkField.
0

I'm not sure about using it in a data-bound control actually, but the url you are building definitely doesn't contain the QueryString parameter 'Name'. It should look like this:

~\WorkItems.aspx?Id={0}&Name={1}

of just replace the Id with Name if Id is not needed:

~\WorkItems.aspx?Name={0}

as for substitution - I don't know what properties does your data-object have and how to bind to them. I think you have no problem with it since binding to Id works (probably).

UPDATE

If Name is not returning from datasource, you should either create new entity which contains Name field as well, or use some constant name, like you are declaring in a page (not sure it this same name should be shown in every row). The second option can be implemented like this:

~\WorkItems.aspx?Id={0}&Name=<%= Name %>

2 Comments

Actually Name in not the one which I am getting from EntityDataSource thats the code behind variable .So, you know how to send that please reply.Any help would be appreciated.
Posted an update about embedding server-variable into ASP page
0

You can use:

<asp:TemplateField HeaderText="test">
     <ItemTemplate>
           <asp:HyperLink runat="server" ID="temp" NavigateUrl='<%# String.Format("~\WorkItems.aspx?Id={0}&Name={1}",  Eval("Name"), Name)%>' ></asp:HyperLink>
      </ItemTemplate>
</asp:TemplateField>

Comments

0

You can modify a HyperLinkField's url by doing this:

protected void pendingAccountsGrid_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        ((HyperLink) e.Row.Cell[0].Controls[0]).NavigateUrl = "http://stackoverflow.com";
    }
}

Just change it to specify the correct index in e.Row.Cell[0].

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.