【问题标题】:How to access parameter from Client Script如何从客户端脚本访问参数
【发布时间】:2012-08-30 08:33:30
【问题描述】:

我有一个 Gridview 和 rowDatabound 我正在创建单击该行和 显示模态弹出窗口。我想要的是当单击该行时,应该传递该行的 ID。

protected void grdOrderProduct_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='#ceedfc'");
        e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=''");
        e.Row.Attributes.Add("style", "cursor:pointer;");
        e.Row.Attributes["onClick"] = Page.ClientScript.GetPostBackClientHyperlink(btnPop, "12");
    }
}

如何在我的服务器控件上获取这个“12”。我已将 12 作为静态演示。但它会改变。

【问题讨论】:

    标签: c# javascript asp.net gridview


    【解决方案1】:

    你也可以这样做

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

    【讨论】:

    • 与此代码一起使用的描述,关于它为什么以它的方式工作,可能是有益的。
    【解决方案2】:

    要知道点击了哪一行,你必须使用GridViewRowEventArgsRow属性

        protected void grdOrderProduct_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='#ceedfc'");
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=''");
                e.Row.Attributes.Add("style", "cursor:pointer;");
                e.Row.Attributes["onClick"] = Page.ClientScript.GetPostBackClientHyperlink(btnPop, e.Row.RowIndex.ToString());
    
            }
        }
    

    RowIndex 将作为参数传递给btnPop。为了接收它btnPop 应该实现IPostBackEventHandler

    像这样:

    public class MyControl : Button, IPostBackEventHandler
      {
    
        // Use the constructor to defined default label text.
        public MyControl()
        {
        }
    
        // Implement the RaisePostBackEvent method from the
        // IPostBackEventHandler interface. 
        public void RaisePostBackEvent(string eventArgument)
        {
          //You receive the row number here.
        }
      }
    

    参考ClientScriptManager.GetPostBackClientHyperlink

    【讨论】:

    • 如何从函数中访问这个值?
    • 我明白你的意思。但我想知道我需要创建这个类吗?以及在哪里创建?
    • 如果你想接收自定义事件,那么你需要创建这个类。您可以在解决方案的任何位置在标准 cs 文件中创建它。此类控件称为自定义控件或服务器控件。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多