Как добавить запрос подтверждения удаления для поля команды в подробном представлении? - PullRequest
5 голосов
/ 16 февраля 2012

Я хочу запросить у пользователя подтверждение, когда он пытается удалить запись в подробном представлении? Я подал команду, в которой showDeletebutton установлен в true.

Я нашел, как сделать подтверждение для gridview, но как я могу изменить, чтобы соответствовать подробному представлению?

Код:

protected void GridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
    // loop all data rows
    foreach (DataControlFieldCell cell in e.Row.Cells)
    {
       // check all cells in one row
       foreach (Control control in cell.Controls)
       {
            // Must use LinkButton here instead of ImageButton
            // if you are having Links (not images) as the command button.
            ImageButton button = control as ImageButton;
            if (button != null && button.CommandName == "Delete")
                // Add delete confirmation
                button.OnClientClick = "if (!confirm('Are you sure " + 
                       "you want to delete this record?')) return;";
        }
    }
}
}

Кто-нибудь?

Ответы [ 5 ]

8 голосов
/ 19 февраля 2012
     <asp:DetailsView ID="DetailsView1" runat="server" Height="50px" Width="125px" 
        .....
            <asp:BoundField DataField="price" HeaderText="price" SortExpression="price" />
            <asp:BoundField DataField="Quantity" HeaderText="Quantity" 
                SortExpression="Quantity" />
            <asp:TemplateField ShowHeader="False">
                 <ItemTemplate>
                    <asp:LinkButton  ID="LinkButton2" runat="server" CausesValidation="False" 
                        CommandName="New" Text="New"></asp:LinkButton>

                    <asp:LinkButton ID="lnkDelete" runat="server" CausesValidation="False" 
                        CommandName="Delete" Text="Delete" OnClientClick="return confirm('Are you sure you want to delete this record');"></asp:LinkButton>

                </ItemTemplate>
            </asp:TemplateField>
        </Fields>
    </asp:DetailsView

Это легко сделать с помощью кода разметки.Я просто добавил код js в свойство onClientClick кнопки удаления:

OnClientClick="return confirm('Are you sure you want to delete this record');"

Или, если вы хотите сделать это в коде:

 protected void DetailsView1_DataBound(object sender, EventArgs e)
  {
    LinkButton bttn = (LinkButton)DetailsView1.FindControl("lnkDelete");
    bttn.OnClientClick = "return confirm('Are you sure you want to delete this record!');";
  }
3 голосов
/ 21 февраля 2012

Я нашел ответ на свой вопрос.

Мой ответ:

 protected void DViewComputer_DataBound1(object sender, EventArgs e)
{
    int noRow = DViewComputer.Rows.Count - 1;//get the no of record

    if (noRow >0)
    {
        Button button = (Button)(DViewComputer.Rows[noRow].Cells[0].Controls[2]);

        // Add delete confirmation
        ((System.Web.UI.WebControls.Button)(button)).OnClientClick = "if (!confirm('Are you sure " +
                               "you want to delete this record?')) return;";

    }
}

В любом случае, спасибо за вашу помощь, ребята.

1 голос
/ 19 февраля 2012
   foreach (Control control in cell.Controls)
   {
        // Must use LinkButton here instead of ImageButton
        // if you are having Links (not images) as the command button.
        ImageButton button = control as ImageButton;
        if (button != null && button.CommandName == "Delete")
            // Add delete confirmation
            button.Attributes.Add("onclick","your javascript here");
    }
0 голосов
/ 19 июня 2013

Это исправляет решение ОП. Код был переведен с кода, найденного здесь: http://forums.aspfree.com/net-development-11/confirm-button-when-deleting-detailsview-120113-2.html

protected void dvEvent_DataBound(object sender, EventArgs e)
{

    int commandRowIndex = dvEvent.Rows.Count - 1;
    if (commandRowIndex > 0)
    {
        DetailsViewRow commandRow = dvEvent.Rows[commandRowIndex];
        DataControlFieldCell cell = (DataControlFieldCell)commandRow.Controls[0];

        foreach (Control ctrl in cell.Controls)
        {
            if (ctrl is ImageButton)
            {
                ImageButton ibt = (ImageButton)ctrl;
                if (ibt.CommandName == "Delete")
                {
                    ibt.ToolTip = "Click here to Delete";
                    ibt.CommandName = "Delete";
                    ibt.Attributes["onClick"] = "if (!confirm('Are you sure " +
                                "you want to delete this record?')) return;";
                }
            }
        }
    }
}
0 голосов
/ 18 февраля 2013
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...