Laziale
Laziale

Reputation: 8225

how to populate modalpopupextender when gridview button is clicked in asp.net

I have one problem, someone maybe can help me. I have a gridview and a button to edit the fields in the gridview from within a modalpopupextender. What I want to achieve is when someone will click the edit button, the popup to have the data from the row which needs to be edited. As it is now, when someone clicks the edit button, the popup comes up but with blank textboxes/dropdowns there. Here is what I have at this moment:

<ItemTemplate>
   <asp:Button ID="btnEdit" CausesValidation="false" 
       runat="server" Text="Edit" OnClick="Activatee" />
  <ajaxToolkit:ConfirmButtonExtender ID="btnDeactivatePopup" runat="server" 
       TargetControlID="btnEdit" DisplayModalPopupID="ModalPopupDeactivate" />
  <ajaxToolkit:ModalPopupExtender 
       ID="ModalPopupDeactivate" runat="server" TargetControlID="btnEdit" 
       PopupControlID="pnlActivate" OkControlID="btnDeactivateOK" 
       CancelControlID="btnDeactivateCancel" BackgroundCssClass="modalBackground" 
       DynamicServicePath="" Enabled="true" />
</ItemTemplate>

I tried with another method when the button is clicked, but when I am clicking the edit button, the popup doesn't come out at the page:

  <ItemTemplate>
    <asp:Button ID="btnEdit" CausesValidation="false" 
          runat="server" Text="Edit" OnClick="getData" />
    <!-- 
    <ajaxToolkit:ConfirmButtonExtender ID="btnDeactivatePopup" 
          runat="server" TargetControlID="btnEdit" 
          DisplayModalPopupID="ModalPopupDeactivate" /> 
    -->
    <ajaxToolkit:ModalPopupExtender ID="ModalPopupDeactivate" 
          runat="server" TargetControlID="btnEdit" PopupControlID="pnlActivate"
          OkControlID="btnDeactivateOK" CancelControlID="btnDeactivateCancel" 
          BackgroundCssClass="modalBackground" DynamicServicePath="" Enabled="true" />
 </ItemTemplate>

And here is the backend code too for the version above but it doesn't show the popup at the end:

 protected void getData(object sender, EventArgs e)
 {
      string coef = "";
      Button btn = (Button)sender;
      GridViewRow gvr = (GridViewRow)btn.NamingContainer;
      int rowindex = gvr.RowIndex;
      GridViewRow roww = gvGDG.Rows[rowindex];
      coef = Convert.ToString(roww.Cells[7].Text);
      txtCoefficient.Text = coef;
      ModalPopupExtender modalPopupExtender1 = 
         (ModalPopupExtender)gvGDG.Rows[rowindex].FindControl("ModalPopupDeactivate");
      modalPopupExtender1.Show();
 }

I hope someone can help me with this. Thanks a lot, Laziale

Upvotes: 3

Views: 15791

Answers (3)

Jon P
Jon P

Reputation: 19772

As Pauls link is no longer working, here is an answer to a similar question on SO

PopUpExtender on ImageButton inside GridView problem

The links in the answer are to the same source as Pauls answer and when I checked they are dead too. But there is a summary of the code in that answer. That should help you.

Upvotes: 1

rick schott
rick schott

Reputation: 20617

What I have done in the past is add a "Edit" UserControl per row set to display:none and bind all the properties, so that when it's shown by the modalpopupextener, it's all good. You then handle the postback in the UserControl that will have all the state you need:

<ItemTemplate>
    <asp:Button ID="btnEdit" CausesValidation="false" runat="server" Text="Edit" OnClick="Activatee" />
    <ajaxToolkit:ConfirmButtonExtender ID="btnDeactivatePopup" runat="server" TargetControlID="btnEdit" DisplayModalPopupID="ModalPopupDeactivate" />
    <ajaxToolkit:ModalPopupExtender OnPreRender="getData" ID="ModalPopupDeactivate" runat="server" TargetControlID="btnEdit" 
    PopupControlID="pnlActivate" OkControlID="btnDeactivateOK" CancelControlID="btnDeactivateCancel" 
    BackgroundCssClass="modalBackground" DynamicServicePath="" Enabled="true" />
   <cc1:YourEditControl id="pnlActivate" runat="server" Propertry1='<%# Eval("data1")%>' Proptery2='<%# Eval("data2")%>' />
    </ItemTemplate>

Upvotes: 0

PMC
PMC

Reputation: 4756

Here is where I learned how to use ModalPopupExtender. Perhaps worth reading this example and seeing if it fits your scenario.

I would think from looking at the code, that setting the modalpopup's associated button to a gridview row control would cause an issue.

Upvotes: 0

Related Questions