简体   繁体   English

Gridview 保持在编辑模式

[英]Gridview remains in Edit Mode

My Gridview refuses to exit edit mode.我的 Gridview 拒绝退出编辑模式。 When I hit press the Update link, everything works as it should as far as the database being updated, but the Gridview row remains in edit mode.当我点击更新链接时,就正在更新的数据库而言,一切正常,但 Gridview 行仍处于编辑模式。 When I press the Cancel button, the Gridview reflects the new information.当我按下取消按钮时,Gridview 反映了新信息。 Why does the row remain in Edit Mode even after setting the EditIndex to -1?为什么即使将 EditIndex 设置为 -1,该行仍处于编辑模式?

I'm sure it's something really remedial, but I can't find any help with getting a gridview out of edit mode that doesn't just tell me to set the edit index.我确信这是真正的补救措施,但我找不到任何帮助让 gridview 退出编辑模式,而不仅仅是告诉我设置编辑索引。

protected void Gridview_RowUpdating(object sender, GridViewUpdateEventArgs e) {
    // Update code is here and works fine
    try {
        // this works and updates the database w/ no problems
        ObjectDataSource.Update();
    }
    catch (Exception ex) {
    }

    Gridview.EditIndex = -1;
    Gridview.DataBind();            
}

EDIT: I took out the try/catch and nothing seems to fail, but I do still have to explicitly call the ObjectDataSource.Update() So, here's the setup for my GridView:编辑:我取出了 try/catch,似乎没有任何失败,但我仍然必须明确调用 ObjectDataSource.Update() 所以,这是我的 GridView 的设置:

<asp:Gridview ID="Gridview" runat="server" DataSourceID="ObjectDataSource" DataKeyNames="ID" OnDataBound="Gridiew_DataBound"
                AutoGenerateColumns="false" OnRowCommand="Gridview_RowCommand" OnRowUpdating="Gridview_RowUpdating">

And the ObjectDataSource和 ObjectDataSource

<asp:ObjectDataSource ID="ObjectDataSource" runat="server" TypeName="DAL.Class" SelectMethod="SelectMethod" UpdateMethod="UpdateMethod">
    <UpdateParameters>
        <asp:Parameter Name="param1" Type="Int32" />
        <asp:Parameter Name="param2" Type="String" />
        <asp:Parameter Name="param3" Type="String" />
        <asp:Parameter Name="param4" Type="String" />
        <asp:Parameter Name="param5" Type="String" />
        <asp:Parameter Name="param6" Type="Int32" />
        <asp:Parameter Name="param7" Type="String" />
        <asp:Parameter Name="param8" Type="Int32" />
    </UpdateParameters>
</asp:ObjectDataSource>

So, what is set up incorrectly, that it isn't automatically updating?那么,什么设置不正确,它不会自动更新?

EDIT 2: So, I'm now setting the parameter values in the ObjectDataSource_Updating event (which wasn't being called before which is why I had the explicit called to the ods_update()).编辑 2:所以,我现在在 ObjectDataSource_Updating 事件中设置参数值(之前没有被调用,这就是我显式调用 ods_update() 的原因)。 Now the _Updating event is being hit, but it never goes into my DAL method.现在 _Updating 事件被击中,但它从未进入我的 DAL 方法。 Again, no errors are thrown, it just doesn't do anything....同样,没有错误被抛出,它只是不做任何事情......

Maybe something bad is happening like the edit is failing. 可能正在发生某些不良情况,例如编辑失败。 And since you are hiding errors: 并且由于您隐藏了错误:

catch (Exception ex) {
    }

...you can't see what's happening. ...您看不到发生了什么。

If the GridView/DataSource is setup properly, you don't need to explicitly call Update or DataBind. 如果正确设置了GridView / DataSource,则无需显式调用Update或DataBind。

Can you give a shot by updating a dummy table? 您可以通过更新虚拟表来尝试一下吗? (in Gridview_RowUpdating event) (在Gridview_RowUpdating事件中)

 ObjectDataSource.UpdateCommand = @"UPDATE somedummytable set  parameter";   
ObjectDataSource.Update();     
Gridview.EditIndex = -1;       
    ObjectDataSource.DataBind();     
Gridview.DataBind();    

After this step, I am sure it will come out of edit mode. 完成此步骤后,我确定它将退出编辑模式。

In case this helps anyone, make sure you update your Edit Index after update/cancel:如果这对任何人都有帮助,请确保在更新/取消后更新您的编辑索引:

GridView1.EditIndex = -1

That did it for me.那是为我做的。

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

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