简体   繁体   中英

Bind a code-behind method to hyperlink in gridview

I want to redirect the user to profile page of clicked UserId depending on its category.I have the code-behind method as below :

  protected void GetProfile(string UserId)
{
    string CS = ConfigurationManager.ConnectionStrings["ss"].ConnectionString;
    using (SqlConnection con = new SqlConnection(CS))
    {
        con.Open();
        SqlCommand cmd = new SqlCommand("Select Category from tblAllUsers where UserId=@UserId", con);
        cmd.Parameters.AddWithValue("@UserId", UserId);
        string category = (string)cmd.ExecuteScalar();
        if (category == "Player")
        {
            Response.Redirect("~/Profile-Player.aspx?UserId=" + UserId);
        }
        else
        {
            Response.Redirect("~/Profile-Doctor.aspx?UserId=" + UserId);
        }
    }
}

I want the above method to be called on click of a hyperlink which is placed inside a gridview as below :

  <asp:HyperLink ID="lblCreator" runat="server" NavigateUrl='<%# GetProfile(Eval("UserId")) %>' Text='<%#String.Format("{0} {1}",Eval("FirstName"),Eval("LastName"))%>'></asp:HyperLink>

but it throws a number of exceptions like

1.Error 14 Argument 1: cannot convert from 'object' to 'string'
2.Error 13 The best overloaded method match for 'Home.GetProfile(string)' has some invalid arguments 3.Error 15 The best overloaded method match for 'System.Convert.ToString(object, System.IFormatProvider)' has some invalid arguments 4.Error 16 Argument 1: cannot convert from 'void' to 'object'

Well, I dont get It.I am not even returning anything from the method

Eval returns an object, which you need to cast to string, if UserId is represented by string in your data source:

NavigateUrl='<%# GetProfile(Eval("UserId") as string) %>'

Another issue is that Response.Redirect does not return anything, while oyu want to return the URL. So just drop this redirect and return the URl as it is. Also do not forget to change return type of your method.

protected string GetProfile(string UserId)
...
    if (category == "Player")
    {
        return "~/Profile-Player.aspx?UserId=" + UserId;
    }
    else
    {
        return "~/Profile-Doctor.aspx?UserId=" + UserId;
    }

And a couple of notes:

  1. This function GetProfile is called during the data binding, not when the link is clicked. If you really want something to be called when the link is clicked, you need a LinkButton to generate a grid view command, and then handle that. However your current solution should work as well.

  2. Current code as it is will do a database connection for every hyperlink. Consider adding some more info to the datasource of your gridview, like a user type, so that you could fetch all data from DB in one go.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM