1

当我在 UserControl 的 GridView 处选择行时,此 gridview 消失。

主页(aspx 页面)

1.1.HTML

<body>
    <form id="form1" runat="server">
    <div>
    <asp:PlaceHolder ID="plh" runat="server"></asp:PlaceHolder>
    </div>
    </form>
</body>

1.2.CodeBehind

protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            Test2.Admin.WebUserControl1[] wuc = new Test2.Admin.WebUserControl1[2];
            for (int i = 0; i < 2; i++)
            {
                plh.Controls.Add(new LiteralControl("<div id='dvChannel' runat='server' style='width: 200px;float:left;padding-left:20px;'>"));
                wuc[i] = (Test2.Admin.WebUserControl1)LoadControl("WebUserControl1.ascx");
                wuc[i].ID = "wuc" + i.ToString();
                plh.Controls.Add(wuc[i]);
                plh.Controls.Add(new LiteralControl("</div>"));
            }
        }
    }

用户控制

2.1.html

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="WebUserControl1.ascx.cs" Inherits="Test2.Admin.WebUserControl1" %>
<asp:GridView ID="GridView1" runat="server" AutoGenerateSelectButton="true"
OnSelectedIndexChanged="gvSelected">
</asp:GridView>

2.2.代码隐藏

protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                var list = (new[] { 
                new { Price = 30 } ,
                new { Price = 30 },
                new { Price = 30 },
                new { Price = 30 }
                });
                GridView1.DataSource = list;
                GridView1.DataBind();
            }
        }
        protected void gvSelected(object sender,EventArgs e)
        {
            GridViewRow row = GridView1.Rows[GridView1.SelectedIndex];
            row.BackColor = Color.Red;//Set red color for this row on gridview
        }

当我在 UserControl 的 GridView 处选择行时,此 gridview 消失。我应该怎么做才能让它正常工作。你有什么解决办法吗?

4

1 回答 1

0

即使在回发中,您也需要加载 GridView。尝试删除条件 - if (!Page.IsPostBack)

或者,在 else 块中编写不同的代码片段 - 用于设置数据源和绑定网格。

于 2013-11-27T11:45:48.723 回答