asp:ImageButton无法按预期工作

时间:2013-05-13 09:37:13

标签: c# asp.net

我对ASP.NET ImageButton控件有问题。

我想要的是:当我点击图片时,我将重定向到Google,Facebook或Twitter的登录页面,但这不会发生。这是我登录页面的代码:

<section id="socialLoginForm">
    <h2>Use another service to log in.</h2>
    <uc:OpenAuthProviders runat="server" ID="OpenAuthLogin" />
    <asp:ListView runat="server" ID="providerDetails" ItemType="Microsoft.AspNet.Membership.OpenAuth.ProviderDetails"
         SelectMethod="GetProviderNames" ViewStateMode="Disabled">
         <ItemTemplate>
              <asp:ImageButton ID="Login"  runat="server" Width="40" Height="40" ImageUrl='<%# Item.ExtraData["Icon"] %>' AlternateText="Log in using your <%#: Item.ProviderDisplayName %> account." />

         </ItemTemplate>
         <EmptyDataTemplate>
             <p>There are no external authentication services configured. </p>
         </EmptyDataTemplate>
     </asp:ListView>
</section>

这是背后的代码:

 public partial class Login : Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        RegisterHyperLink.NavigateUrl = "Register.aspx";
        OpenAuthLogin.ReturnUrl = Request.QueryString["ReturnUrl"];

        var returnUrl = HttpUtility.UrlEncode(Request.QueryString["ReturnUrl"]);
        if (!String.IsNullOrEmpty(returnUrl))
        {
            RegisterHyperLink.NavigateUrl += "?ReturnUrl=" + returnUrl;
        }
    }

    public string ReturnUrl { get; set; }

    public IEnumerable<ProviderDetails> GetProviderNames()
    {
        return OpenAuth.AuthenticationClients.GetAll();
    }

1 个答案:

答案 0 :(得分:0)

您可以添加列表视图“OnItemCommand”命令 Aspx页面

 <asp:ListView runat="server" ID="providerDetails" ItemType="Microsoft.AspNet.Membership.OpenAuth.ProviderDetails"
        SelectMethod="GetProviderNames" ViewStateMode="Disabled" OnItemCommand="ListView_OnItemCommand">
        <ItemTemplate>
            <asp:ImageButton ID="Login" runat="server" Width="40" Height="40" ImageUrl='<%# Item.ExtraData["Icon"] %>'
                AlternateText="Log in using your <%#: Item.ProviderDisplayName %> account." CommandName="Login" />
        </ItemTemplate>
        <EmptyDataTemplate>
            <p>
                There are no external authentication services configured.
            </p>
        </EmptyDataTemplate>
    </asp:ListView>

背后的代码

protected void ListView_OnItemCommand(object sender, ListViewCommandEventArgs e)
  {
    if (String.Equals(e.CommandName, "Login"))
    {
     // do what ever you want
    }
  }

来源:http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.listview.itemcommand.aspx