【发布时间】:2015-10-21 18:03:01
【问题描述】:
我想根据其类别将用户重定向到单击的 UserId 的个人资料页面。我有如下代码隐藏方法:
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);
}
}
}
我希望在单击位于 gridview 内的超链接时调用上述方法,如下所示:
<asp:HyperLink ID="lblCreator" runat="server" NavigateUrl='<%# GetProfile(Eval("UserId")) %>' Text='<%#String.Format("{0} {1}",Eval("FirstName"),Eval("LastName"))%>'></asp:HyperLink>
但它会引发一些异常,例如
1.Error 14 参数 1:无法从 'object' 转换为 'string'
2.Error 13 'Home.GetProfile(string)' 的最佳重载方法匹配有一些无效参数
3.Error 15 'System.Convert.ToString(object, System.IFormatProvider)' 的最佳重载方法匹配有一些无效参数
4.Error 16 Argument 1: cannot convert from 'void' to 'object'
好吧,我不明白。我什至没有从该方法返回任何东西
【问题讨论】: