在rowediting回发上的Gridview隐藏了Gridview

时间:2013-12-09 00:08:16

标签: c# asp.net gridview postback datasource

这是一种非常奇怪的行为,我无法弄清楚是什么导致它。 基本上,我有一个aspx页面,其中有一个button和一个gridview

button会将数据从数据库中提取到datatabledatasource将用作gridview的{​​{1}}。

gridviewcommandfield showeditbutton="true"

databind成功后gridview有一些记录,我按edit button。编辑按钮在rowediting

上有以下代码
    GridView1.EditIndex = e.NewEditIndex;
    GridView1.DataBind();

令人惊讶的是,按edit button后,girdview变为隐藏状态。

当我再次按下button时,它会将其恢复为编辑模式。奇怪的行为,任何想法?

.aspx页面

<asp:Button ID="Button1" runat="server" Text="Button" 
        CssClass="info-button-flat" onclick="Button1_Click1" />
    <br /><br /><br />
    <asp:GridView ID="GridView1" runat="server" onrowediting="GridView1_RowEditing">
        <Columns>
            <asp:CommandField ShowEditButton="True" />
            <asp:CommandField ShowDeleteButton="True" />
        </Columns>
    </asp:GridView>

.cs页面

protected void Button1_Click1(object sender, EventArgs e)
    {
        string aPara= null;
        aPara= aUser.ID;
        clsDataConduit aConn = new clsDataConduit();
        aDataTable = new DataTable();
        aConn.AddParameter("@aPara", aPara);
        aDataTable = aConn.Return_aDataTable("an_user_sproc");
        GridView1.DataSource = aDataTable;
        GridView1.DataBind();
    }
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {

        GridView1.EditIndex = e.NewEditIndex;
        GridView1.DataBind();
    }

1 个答案:

答案 0 :(得分:1)

GridView1_RowEditing中,您不会重新绑定到任何数据源。除非您设置GridView1.DataBind();

,否则只有GridView1.DataSource = SomeSource;无效

可能的解决方案:

将数据绑定代码移动到名为BindData()的方法:

public void BindData()
{
    string aPara = null;
    aPara = aUser.ID;
    clsDataConduit aConn = new clsDataConduit();
    aDataTable = new DataTable();
    aConn.AddParameter("@aPara", aPara);
    aDataTable = aConn.Return_aDataTable("an_user_sproc");
    GridView1.DataSource = aDataTable;
    GridView1.DataBind();

}

从Button_Click()调用此方法:

protected void Button1_Click1(object sender, EventArgs e)
{
    BindData();
}

GridView1_RowEditing调用此方法:

protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
    GridView1.EditIndex = e.NewEditIndex;
    BindData();
}

您必须处理GridView1_RowUpdating,您将在其中更新基础数据源。希望它有所帮助!