Gridview RowCommand 参数在 Safari 中未正确传递



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

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

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>

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

我认为

您应该首先检查按钮是否从哪个浏览器单击,如果它来自 safari 而不是更改命令名称..

  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;
        }                
    }

最新更新