繁体   English   中英

selectedindex在动态创建的gridview上更改

[英]selectedindexchanged on dynamically created gridview

我已经动态创建了一个gridview,现在我想在selectedindex更改时触发一个事件。

 GridView NewDg = new GridView();
 NewDg.ID = "SubGridView" + e.Row.RowIndex.ToString();
 NewDg.DataKeyNames = new string[]{"logentry_id"};
 NewDg.SelectedIndexChanged += new EventHandler(NewDg_SelectedIndexChanged);
 NewDg.RowDataBound += new GridViewRowEventHandler(NewDg_RowDataBound);

RowDataBound有效,但我猜它不能产生正确的回发URL。 在RowDataBound中,我有以下代码:

GridView sendingGridView = (GridView)sender;
e.Row.Attributes["onclick"] = ClientScript.GetPostBackClientHyperlink(sendingGridView, "Select$" + e.Row.RowIndex);

这将产生以下代码:

javascript:__doPostBack('SubGridView4','Select$0')

仅此不会导致此函数的回发:

        void NewDg_SelectedIndexChanged(object sender, EventArgs e)
    {

        GridView sendingGridView = (GridView)sender;
        ViewDetails(Convert.ToInt32(sendingGridView.SelectedDataKey["logentry_id"].ToString()));
    }

有人知道我在做什么错吗?

首先,您是在每次页面加载时重新创建网格吗? 这是以这种方式创建网格的要求。 其次,尝试进入RowCommand,然后以这种方式查找命令名称; 也许那将成功触发; 您可以通过command参数获得对命令的引用,如下所示:

void rowcmd(..) {
   if (e.CommandName != null && e.CommandName.StartsWith("Select")) {
       //Dothis
   }
}

我找到了答案,我的问题代码项目

我现在在我的GridView控件使用GridView

            <asp:TemplateField>
            <ItemTemplate>
                <asp:GridView ID="SubGridView" 

由于使用了GridView的扩展程序,因此当我单击加号时将显示gridview(请参阅链接)

在页面加载时,我执行以下操作:

        protected void Page_Load(object sender, EventArgs e)
    {
        GridView1.RowCreated += new GridViewRowEventHandler(GridView1_RowCreated);
        GridView1.DataSource = dc.GetLogEntriesWithUsername();
        GridView1.DataBind();

我已经在此gridview上有一个DataBound和一个Selected Index Changed事件。

该行创建的事件我执行以下操作:

        void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            GridView SubGridView = e.Row.FindControl("SubGridView") as GridView;
            List<GetLogEntriesWithUsernameByParentIdResult> subLogEntries = dc.GetLogEntriesWithUsernameByParentId(((GetLogEntriesWithUsernameResult)e.Row.DataItem).logentry_id).ToList();
            if (subLogEntries.Count > 0)
            {
                SubGridView.DataSource = subLogEntries;
                SubGridView.DataBind();
                (e.Row as ExtGridViewRow).ShowExpand = SubGridView.Rows.Count > 0;
            }
        }
    }

在子网格视图上,我还具有DataBound和SelectedIndex Changed事件。 现在可以使用!

我使用此DataBound事件:

        protected void GridView_RowDataBound(object sender, GridViewRowEventArgs e)
    {        // only apply changes if its DataRow
        GridView sendingGridView = (GridView)sender;
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            // when mouse is over the row, save original color to new attribute, and change it to highlight yellow color
            e.Row.Attributes.Add("onmouseover",
          "this.originalstyle=this.style.backgroundColor;this.style.backgroundColor='#C0C0C0';this.style.cursor='pointer';");

            // when mouse leaves the row, change the bg color to its original value   
            e.Row.Attributes.Add("onmouseout",
            "this.style.backgroundColor=this.originalstyle;this.style.cursor='cursor'");
            //e.Row.Attributes.Add("onclick", ClientScript.GetPostBackClientHyperlink(sendingGridView, "Select$" + e.Row.RowIndex));

            e.Row.Cells[1].Attributes["onclick"] = ClientScript.GetPostBackClientHyperlink(sendingGridView, "Select$" + e.Row.RowIndex);
            e.Row.Cells[2].Attributes["onclick"] = ClientScript.GetPostBackClientHyperlink(sendingGridView, "Select$" + e.Row.RowIndex);
            e.Row.Cells[3].Attributes["onclick"] = ClientScript.GetPostBackClientHyperlink(sendingGridView, "Select$" + e.Row.RowIndex);
            e.Row.Cells[4].Attributes["onclick"] = ClientScript.GetPostBackClientHyperlink(sendingGridView, "Select$" + e.Row.RowIndex);
            e.Row.Cells[5].Attributes["onclick"] = ClientScript.GetPostBackClientHyperlink(sendingGridView, "Select$" + e.Row.RowIndex);

并选择索引更改事件:

        protected void GridView_SelectedIndexChanged(object sender, EventArgs e)
    {
        GridView sendingGridView = (GridView)sender;
        ViewDetails(Convert.ToInt32(sendingGridView.SelectedDataKey["logentry_id"].ToString()));         
    }

该ViewDetails功能:显示在不同的DIV选择logentry的细节。 现在我正忙于最后一步,那就是继续显示数据,就像我单击一行之前一样。

感谢您的帮助,但这是解决我的问题的方法。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM