0

我有一个带有gridview 的asp 页面。我还为每个绑定行添加了一个客户端脚本,以突出显示/取消突出显示鼠标悬停/移出。我添加了一个 asp:button 作为模板字段并将一个值绑定到 CommandArgument。在 IE 和 FIrefox 中,我得到了将 CommandName 传递给 _RowCommand 事件的预期行为。但是,在 Safari 中,我只看到传递给 RowCommand 的“Select”的 CommanName。

预期的行为是,当单击绑定行时,“选择”参数将传递给 RowCommand 事件。单击该行中的按钮时,将传递参数“Remove”。

protected void gvContacts_RowDataBound(object sender, GridViewRowEventArgs e)
{

    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        e.Row.Attributes.Add("onMouseOver", "Highlight(this)");
        e.Row.Attributes.Add("onMouseOut", "UnHighlight(this)");

        e.Row.Attributes["onclick"] = Page.ClientScript.GetPostBackClientHyperlink(gvContacts, "Select$" + e.Row.RowIndex);

        e.Row.Attributes["style"] = "cursor:pointer";
    }
}


protected void gvContacts_RowCommand(object sender, GridViewCommandEventArgs e)
{                

    switch (e.CommandName)  //Always "Select" when browser is Safari.  
    {
        case "Select":
            Session["clientID"] = gvContacts.Rows[Convert.ToInt32(e.CommandArgument)].Cells[0].Text;
            Response.Redirect("../Contacts/ContactEdit.aspx?readin=1");
            break;
        case "Remove":
            //Remove the client from the list
            Company company = new Company();
            company.Get(Int32.Parse(Session["CompanyID"].ToString()), ((Model)Session["model"]).ConnectionString);
            company.RemoveUser(Int32.Parse(e.CommandArgument.ToString()));
            BindGrid(company.ID);
            break;
    }                
}

项目模板的 HTML

<asp:TemplateField>
    <ItemTemplate>
        <asp:Button ID="btnRemove" runat="server" Text="Remove" CommandName="Remove" OnClientClick="return confirmRemove();"
                                    CommandArgument='<%# Eval("ID") %>' />
   </ItemTemplate>

任何想法将不胜感激。谢谢

4

1 回答 1

0

我认为您应该首先检查 Button 是从哪个浏览器单击的,以及它是否来自 safari,而不是 Change Command Name ..

  protected void gvContacts_RowCommand(object sender, GridViewCommandEventArgs e)
    {                
    System.Web.HttpBrowserCapabilities browser = Request.Browser;
      if(browser.Browser=="Safari") //Check The Browser 
       {
          e.CommandName="Select";
       }
        switch (e.CommandName)  //Always "Select" when browser is Safari.  
        {
            case "Select":
                Session["clientID"] = gvContacts.Rows[Convert.ToInt32(e.CommandArgument)].Cells[0].Text;
                Response.Redirect("../Contacts/ContactEdit.aspx?readin=1");
                break;
            case "Remove":
                //Remove the client from the list
                Company company = new Company();
                company.Get(Int32.Parse(Session["CompanyID"].ToString()), ((Model)Session["model"]).ConnectionString);
                company.RemoveUser(Int32.Parse(e.CommandArgument.ToString()));
                BindGrid(company.ID);
                break;
        }                
    }
于 2015-01-23T04:05:10.823 回答