Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
I have custom 'Edit' and 'Close' buttons inside a Panel that pops up with the ModalPopupExtender. The issue I am having is that when I click on the Edit button, instead of going to the DetailsViewMode.Edit mode, it just links back to the GridView Page.

Image showing the DetailsView with the ModalPopupExtender:
Image link

Screen fter clicking 'Edit' button:
Image link

I have no clue why it does that.

Could anyone please help me find what is wrong?

Many thanks in advance!

ASPX:
ASP.NET
<pre lang="xml"><asp:Button ID="btnShowPopup" runat="server" style="display:none" />
            <asp:ModalPopupExtender ID="mdlPopup" runat="server" TargetControlID="btnShowPopup" PopupControlID="pnlPopup"
                            CancelControlID="btnClose" BackgroundCssClass="modalBackground" />
            <asp:Panel ID="pnlPopup" runat="server" CssClass="detail" Width="500px">
                <asp:UpdatePanel ID="updPnlStockDetail" runat="server" UpdateMode="Conditional">
                    <ContentTemplate>
                        <asp:Label ID="lblStockDetail" runat="server" Text="Detail" BackColor="#7AB800" Width="95%" />
                        <asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False" OnItemUpdated="DetailsView1_ItemUpdated" BackColor="White" Width="95%" FieldHeaderStyle-Width="200px" OnDataBound="DetailsView1_DataBound" OnItemCommand="DetailsView1_ItemCommand">
                            <FieldHeaderStyle Width="200px" />


                            <Fields>
</pre>
</Fields>
                            
                        </asp:DetailsView>
                    </ContentTemplate>
                </asp:UpdatePanel>
                <div align="right" style="width:95%">
                    <asp:Button ID="btnEdit" runat="server" Text="Edit" CommandName="Edit" Height="26px" Width="50px" BackColor="#7AB800" CssClass="viewdetailbutton" />
                    <asp:Button ID="btnClose" runat="server" Text="Close" CommandName="Close" Height="26px" Width="50px" BackColor="#FF9900" CssClass="viewdetailbutton" />
                </div>
            </asp:Panel>


Code behind:
C#
protected void DetailsView1_ItemCommand(object sender, DetailsViewCommandEventArgs e)
    {
        if (e.CommandName == "Edit")
        {
            this.DetailsView1.ChangeMode(DetailsViewMode.Edit);
            this.DetailsView1.DataBind();
        }
        else if (e.CommandName == "Close")
        {
            this.DetailsView1.ChangeMode(DetailsViewMode.ReadOnly);
            this.DetailsView1.DataBind();
        }
    }
Posted
Updated 22-Oct-14 5:34am
v5
Comments
ZurdoDev 22-Oct-14 10:49am    
Way too much code. Please narrow it down to what the issue is.
ThatGuy7 22-Oct-14 11:36am    
Sorry about that. I believe the issue is mainly caused by the DetailsView or the Edit button itself so I narrowed it down to those parts. Could you please have a look at it? Thanks!
ZurdoDev 22-Oct-14 11:44am    
Put a breakpoint in DetailsView1_ItemCommand to make sure it is changing the mode. Also, I have not used DetailsView in a very long time but are you sure you need to call DataBind after changing the mode? I wonder if that is overwriting it.
ThatGuy7 22-Oct-14 11:57am    
Ok so it turns out that after clicking on the 'Edit' button, it does not even reach the breakpoint in the ItemCommand first if statement.. I have tried with

BtnEdit_OnClick
{
DetailsView1.ChangeMode(DetailsViewMode.Edit);
}

but the output was the same..
ZurdoDev 22-Oct-14 12:03pm    
I don't see anything in your detailsview. Fields is empty.

1 solution

Ryan is correct. For ItemCommand, the Button should be inside the DetailsView.

As I can see, the Edit Button is outside the DetailsView, so add one Click event for that Button. Inside the Button Event, try like...
C#
protected void BtnEdit_OnClick(object sender, EventArgs e)
{
    DetailsView1.ChangeMode(DetailsViewMode.Edit);
    DetailsView1.DataBind();
}
 
Share this answer
 
Comments
ThatGuy7 22-Oct-14 23:50pm    
Thanks for your advice!

Here I added those two buttons in the DetailsView1 inside the FooterTemplate.

<footertemplate>
<asp:Button ID="btnEdit" runat="server" Text="Edit" OnClick="BtnEdit_OnClick" CommandName="Edit" Height="26px" Width="50px" BackColor="#7AB800" CssClass="viewdetailbutton" />
<asp:Button ID="btnClose" runat="server" Text="Close" CommandName="Close" Height="26px" Width="50px" BackColor="#FF9900" CssClass="viewdetailbutton" />


I encountered another problem possibly caused by the code above.
The problem is that as soon as I added these buttons in the DetailsView1, my ModalPopupExtender no longer shows up. I have checked with a breakpoint inside the

protected void ViewDetails2_Click(object sender, EventArgs e)
{
Button btnDetails = sender as Button;
GridViewRow row = (GridViewRow)btnDetails.NamingContainer;

this.SqlDataSource2.SelectParameters.Clear();
this.SqlDataSource2.SelectParameters.Add("stock_ID", Convert.ToString(this.GridView2.DataKeys[row.RowIndex].Value));
this.DetailsView1.DataSource = this.SqlDataSource2;
this.DetailsView1.DataBind();

this.updPnlStockDetail.Update();
this.mdlPopup.Show();
}

It seems to reach .Show(). It works if I have those buttons outside the DetailsView. What could I do to solve this?
Okay first try like ... Just have show call before update.

this.mdlPopup.Show();
this.updPnlStockDetail.Update();

If it won't work, then go for ItemCommand. No need to have one different event for the Button, if that is inside the DetailsView.
ThatGuy7 23-Oct-14 5:05am    
Hi Tadit thanks for the comment! I tried that but still the same issue no ModalPopup.. It seems like this issue occurs when I add things in FooterTemplate for some stupid reasons.

The bottom of my gridview is the following


FooterTemplate
<asp:Button ID="btnEdit" runat="server" Text="Edit" OnClick="BtnEdit_OnClick" CommandName="Edit" Height="26px" Width="50px" BackColor="#7AB800" CssClass="viewdetailbutton" />
<asp:Button ID="btnClose" runat="server" Text="Close" CommandName="Close" Height="26px" Width="50px" BackColor="#FF9900" CssClass="viewdetailbutton" />
/FooterTemplate


If I add for example a ButtonField for each button, it shows the modal but that saying I have these buttons in the different rows (which doesnt look very nice and professional).

I've also tried with the ItemCommand. No luck with that either..
ThatGuy7 23-Oct-14 5:08am    
This is my ItemCommand:

protected void DetailsView1_ItemCommand(object sender, DetailsViewCommandEventArgs e)
{
if (DetailsView1.CurrentMode == DetailsViewMode.ReadOnly)
{
if (e.CommandName == "Edit")
{
this.DetailsView1.ChangeMode(DetailsViewMode.Edit);
this.DetailsView1.DataBind();
}
else if (e.CommandName == "Close")
{
this.DetailsView1.ChangeMode(DetailsViewMode.ReadOnly);
this.DetailsView1.DataBind();
}
}
else
{
if (e.CommandName == "Close")
{
this.DetailsView1.ChangeMode(DetailsViewMode.ReadOnly);
this.DetailsView1.DataBind();
}
}

}
Did you place the modal show code inside the ItemCommand instead of Button Click? First delete the event from markup and then try.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900