opening modalpopup with gridview delete - asp.net

I am trying to open a modal popup window when user clicks on the delete image button. when I click on the delete image button, I want the user to go to grdShoppingCart_RowDeleting when Ok is clicked. I am not sure what I am doing wrong. Below is my gridview code:
<asp:UpdatePanel ID="updPnl" runat="server" UpdateMode="Always">
<ContentTemplate>
<asp:GridView ID="grdShoppingCart" runat="server" AutoGenerateColumns="false" class="ui-responsive table-stroke ss-table ui-search-result-table" DataKeyNames="CartID" AllowPaging="false" PageSize="5" GridLines="None" OnRowDataBound="grdShoppingCart_RowDataBound" OnRowDeleting="grdShoppingCart_RowDeleting" >
<Columns>
<!-- other clumns here-->
<asp:TemplateField ShowHeader="False" HeaderStyle-HorizontalAlign="center" ItemStyle-HorizontalAlign="center" ItemStyle-Width="150px" ControlStyle-CssClass="ss-row" >
<ItemTemplate>
<asp:ImageButton ID="imgbtnDelete" runat="server" ImageUrl="~/Images/delete1.png" ToolTip="Click To Delete" AlternateText="Click To delete" CommandName="Delete"/>
<asp:Panel ID="pnlPopUp" runat="server" Style="display: none" CssClass="modalPopup">
<asp:Panel ID="pnlDragPopUp" runat="server" Style="cursor: move;background-color:#DDDDDD;border:solid 1px Gray;color:Black">
<div>
<p>Are you sure you want to delete this item?</p>
</div>
</asp:Panel>
<div>
<p style="text-align: center;">
<asp:Button ID="OkButton" runat="server" Text="Yes" />
<asp:Button ID="CancelButton" runat="server" Text="Cancel" />
</p>
</div>
</asp:Panel>
<ajaxToolkit:ModalPopupExtender ID="ModalPopupExtender" runat="server"
TargetControlID="imgbtnDelete"
PopupControlID="pnlPopUp"
BackgroundCssClass="modalBackground"
CancelControlID="CancelButton"
DropShadow="true"
PopupDragHandleControlID="pnlDragPopUp" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>
Code behind:
protected void grdShoppingCart_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
int cartId = (int)grdShoppingCart.DataKeys[e.RowIndex]["CartID"];
ShoppingCartData scd = new ShoppingCartData();
scd.DeleteShoppingCartData(cartId);
}

These are the changes that I did to fix the code. I passed the CartID to the code behind
<div >
<p style="display:flex;align-items:center;justify-content:space-between;width:220px;">
<asp:Button ID="OkButton" runat="server" Text="Remove" CommandName="delete" CommandArgument='<%#Eval("CartID") %>'/>
<asp:Button ID="CancelButton" runat="server" Text="Cancel" />
</p>
</div>
and these are the changes I did in code behind:
protected void grdShoppingCart_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
// Get current grid view row
GridViewRow row = grdShoppingCart.Rows[e.RowIndex];
// Get command argument from button, also you could use another approach to get parameters
Button btn = (Button)row.FindControl("OkButton");
int cartId = Convert.ToInt32(btn.CommandArgument);
label1.Text = "The row is going to be deleted: " + cartId;
}
that did the trick.

Related

modal popup not showing from code behind

<asp:UpdatePanel ID="updtpnlacademic" runat="server">
<ContentTemplate>
<td>
<asp:GridView ID="gdvwAcademic1" runat="server" OnRowCommand="gdvwAcademic1_RowCommand" OnRowDeleting="gdvwAcademic1_RowDeleting" CellPadding="4" ForeColor="#333333" GridLines="None" Width="100%" AllowSorting="True" AutoGenerateColumns="False" HorizontalAlign="Center">
<Columns>
....Other column fields
<asp:TemplateField HeaderText="Delete" ItemStyle-HorizontalAlign="Center" ShowHeader="false">
<ItemTemplate>
<asp:ImageButton ID="imgbtnacademicdelete" CommandName="Delete" OnClick="imgbtnacademicdelete_Click" CommandArgument='<%# ((GridViewRow)Container).RowIndex %>' ToolTip="Delete" runat="server" ImageAlign="Top" ImageUrl="~/Images/delete.png" Width="30" Height="25"/>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:Button ID="btndummy" runat="server" Visible="false" />
<cc1:ModalPopupExtender ID="mpacademic" runat="server" TargetControlID="btndummy" PopupControlID="Panel2" CancelControlID="btnacademicClose" BackgroundCssClass="modalBackground">
</cc1:ModalPopupExtender>
<asp:Panel ID="Panel2" runat="server" CssClass="modalPopup" HorizontalAlign="Center" style = "display:none">
<span style="color:white; font-family:'Bookman Old Style'; font-weight:bold;">Really Want To Delete This Record...!</span><br /><br /><br /><br />
<asp:Button ID="btnconfirmacademicdelete" OnClick="btnconfirmacademicdelete_Click" CssClass="BStyle" Font-Bold="true" runat="server" Text="Yes" />
<asp:Button ID="btnacademicClose" Font-Bold="true" runat="server" CssClass="BStyle" Text="Cancel" />
</asp:Panel>
</td>
</ContentTemplate>
</asp:UpdatePanel>
And following code as in the code behind ....
protected void gdvwAcademic1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
mpacademic.Show();
//System.Windows.Forms.MessageBox.Show("RowDeleting clicked after this modal popup should be displayed.....!");
}
It only show messagebox on event if uncomment messagebox but not showing modal popup
<cc1:ModalPopupExtender ID="mpacademic" runat="server" TargetControlID="btndummy" PopupControlID="Panel2" CancelControlID="btnacademicClose" BackgroundCssClass="modalBackground">
</cc1:ModalPopupExtender>
The TargetControlID property is set to btndummy and
<asp:Button ID="btndummy" runat="server" Visible="false" />
btndummy is not rendering since Visible property is set to false.
Try to replace
<asp:Button ID="btndummy" runat="server" Visible="false" />
With
<asp:Button ID="btndummy" runat="server" style="display:none" />

get label text from DataListItem Template

I am making an item template in a DataList which shows images in it. I have a button in it to display details of the image.
On button click I want to get the label text which is in that template portion . how can I do this?
This is my code :
<asp:DataList ID="DataList1" runat="server" RepeatColumns="3"
DataSourceID="SqlDataSource1">
<ItemTemplate>
<asp:Image ID="Image1" runat="server" Height="217px" Width="221px"
ImageUrl='<%# Eval("image") %>' />
<br />
<asp:Label ID="Label11" runat="server" Text='<%# Eval("image") %>'
></asp:Label>
<br />
<asp:Button ID="Button1" runat="server" Text="Button" Width="90px"
onclick="Button1_Click" />
</ItemTemplate>
Try This:
protected void Button1_Click(object sender, EventArgs e)
{
DataListItem dr = (DataListItem)((Button)sender).Parent;
Image Image1 = (Image)dr.FindControl("Image1");
}

Listbox without values

this is what happens. i don't understand why its not working, because if i copy the code to a normal template everything works fine :S so i dont think its a code issue. im working with asp and c#![enter image description here][1]
http://s21.postimg.org/3jsi8351z/weird.png here is a prints screen i took to explain the issue
Asp:
<asp:panel ID="pnltopico" runat="server" GroupingText="Topico" Height="100px"
Width="550px" Visible="False" Wrap="False">
<asp:DropDownList ID="droptopico" runat="server" AutoPostBack="True"
DataSourceID="SqlDataSource1" DataTextField="nome" DataValueField="Id_topico"
Height="22px"
onselectedindexchanged="DroptopicoSelectedIndexCha nged" Width="169px">
</asp:DropDownList>
<asp:panel ID="pnlpermissoestopico" runat="server" GroupingText="Permissões"
Height="59px" style="margin-top: 0px" Width="354px" Wrap="False"
ClientIDMode="inherit">
<asp:ListBox ID="lbpermitidotp" runat="server"
EnableViewState="true" Height="80px"
></asp:ListBox>
<asp:Button ID="btnproibirtp" runat="server" onclick="BtnpermitirClick"
Text=">>" />
<asp:Button ID="btnpermitirtp" runat="server" onclick="BtnnaopermitirClick"
Text="<<" Height="26px" />
<asp:ListBox ID="lbproibidotp" runat="server"
EnableTheming="False" EnableViewState="true" Height="80px" onselectedindexchanged="lbproibidotp_SelectedIndex Changed"
></asp:ListBox> </asp:panel>
since the code is working fine i dont think i need to post the c# code but let me know if it could help.
thanks for your help
Hey I just Modified your code a little and Compiled it everything is working fine now:
<asp:panel ID="pnltopico" runat="server" GroupingText="Topico" Height="100px"
Width="550px" Visible="False" Wrap="False">
<asp:DropDownList ID="droptopico" runat="server" AutoPostBack="True"
DataTextField="nome" DataValueField="Id_topico"
Height="22px"
onselectedindexchanged="droptopico_SelectedIndexChanged" Width="169px">
</asp:DropDownList>
</asp:panel>
<asp:panel ID="pnlpermissoestopico" runat="server" GroupingText="Permissões"
Height="59px" style="margin-top: 0px" Width="354px" Wrap="False"
ClientIDMode="inherit">
<asp:ListBox ID="lbpermitidotp" runat="server"
EnableViewState="true" Height="80px"
>
<asp:ListItem Text="Admin" Value="Admin" />
<asp:ListItem Text="Guest" Value="Guest" />
</asp:ListBox>
<asp:Button ID="btnproibirtp" runat="server" OnClick="btnproibirtp_Click"
Text=">>" />
<asp:Button ID="btnpermitirtp" runat="server"
Text="<<" Height="26px" />
<asp:ListBox ID="lbproibidotp" runat="server" EnableTheming="False" EnableViewState="true" Height="80px">
<asp:ListItem Text="Moderator" Value="Moderator" />
<asp:ListItem Text="Utilizer" Value="Utilizer" />
</asp:ListBox> </asp:panel>
and I have put the breakpoint on the Button Click of the btnproibirtp , just to verify and it works normally ,
I think u had some tags formation error or so :
protected void btnproibirtp_Click(object sender, EventArgs e)
{
string lbl1 = lbproibidotp.SelectedValue;
}
regards

Button Outside Gridview not firing once click on Edit button of gridview

I have a gridview and i have two buttons edit and delete.Once i click on edit button, i take the values and put those in textboxes outside grid and visible update and cancel button. but that does not fires..
<asp:UpdatePanel ID="UpdatePanel2" runat="server">
<ContentTemplate>
<asp:Panel ID="pnladdproduct" runat="server">
<div class="mws-form-col-1-8">
<asp:Label ID="Label11" runat="server" AssociatedControlID="btnAddProduct"> </asp:Label>
<div class="mws-form-item large">
<asp:Button ID="btnAddProduct" ValidationGroup="grpProduct" runat="server" CssClass="mws-button red" Text="Add" OnClick="btnAddProduct_Click" />
</div>
</div>
</asp:Panel>
<asp:Panel ID="pnlUpdateProduct" runat="server">
<div class="mws-form-col-1-8">
<asp:Label ID="Label12" runat="server" AssociatedControlID="btnUpdateProduct"> </asp:Label>
<div class="mws-form-item large">
<asp:Button ID="btnUpdateProduct" runat="server" CausesValidation="False" CssClass="mws-button blue" Text="Update" OnClick="btnUpdateProduct_Click" />
</div>
</div>
</asp:Panel>
<asp:Panel ID="pnlCancel" runat="server">
<div class="mws-form-col-1-8">
<asp:Label ID="Label13" runat="server" AssociatedControlID="btnCancelUpdate"> </asp:Label>
<div class="mws-form-item large">
<asp:Button ID="btnCancelUpdate" CausesValidation="False" runat="server" CssClass="mws-button gray" Text="Cancel" OnClick="btnCancelUpdate_Click" />
</div>
</div>
</asp:Panel>
<div class="mws-form-col-8-8">
<asp:GridView ID="grdProduct" ShowHeaderWhenEmpty="True" CssClass="mGrid" runat="server" AutoGenerateColumns="False" AllowPaging="True" PagerStyle-CssClass="pgr" AlternatingRowStyle-CssClass="alt" OnPageIndexChanging="grdProduct_PageIndexChanging" OnRowDeleting="grdProduct_RowDeleting" OnRowEditing="grdProduct_RowEditing">
<Columns>
<asp:BoundField DataField="Name" HeaderText="Product" SortExpression="Product" />
<asp:BoundField DataField="Quantity" HeaderText="Quantity" SortExpression="Quantity" />
<asp:BoundField DataField="Rate" HeaderText="Rate" SortExpression="Rate" />
<asp:BoundField DataField="Amount" HeaderText="Amount" SortExpression="Amount" />
<asp:ButtonField ButtonType="Image" CommandName="Edit" HeaderText="Edit" ImageUrl="~/css/icons/16/edit.png" ShowHeader="True" Text="Edit" />
<asp:ButtonField ButtonType="Image" CommandName="Delete" HeaderText="Delete" ImageUrl="~/css/icons/16/delete.png" ShowHeader="True" Text="Delete" />
<asp:TemplateField HeaderText="HiddenColumn">
<HeaderTemplate>
</HeaderTemplate>
<ItemTemplate>
<asp:HiddenField ID="hdfProductID" runat="server" Value='<%# Bind("ProID") %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
<EmptyDataTemplate>
No Products Added !!
</EmptyDataTemplate>
</asp:GridView>
<div class="mws-form-row inline">
<asp:Label ID="Label15" runat="server" AssociatedControlID="txtQuantity">Total Amount</asp:Label>
<div class="mws-form-item medium">
<asp:TextBox ID="txtTotalAmount" BackColor="Beige" BorderColor="red" ReadOnly="True" CssClass="mws-textinput" runat="server"></asp:TextBox>
</div>
</div>
</div>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="btnUpdateProduct" EventName="Click"/>
<asp:PostBackTrigger ControlID="btnCancelUpdate" />
</Triggers>
</asp:UpdatePanel>
Server side Code..
protected void grdProduct_RowEditing(object sender, GridViewEditEventArgs e)
{
Session["proID"] = StepGetHiddenValuesFromGridControl(e.NewEditIndex);
txtQuantity.Text = grdProduct.Rows[e.NewEditIndex].Cells[1].Text;
txtRate.Text = grdProduct.Rows[e.NewEditIndex].Cells[2].Text;
txtAmount.Text = grdProduct.Rows[e.NewEditIndex].Cells[3].Text;
txtQuantity.BackColor = Color.Beige;
txtRate.BackColor = Color.Beige;
txtAmount.BackColor = Color.Beige;
//pnladdproduct.Visible = false;
//pnlUpdateProduct.Visible = true;
//pnlCancel.Visible = true;
}
protected void btnUpdateProduct_Click(object sender, EventArgs e)
{
// this code not firing
}
I don't think there's a need for triggers.

update panel not working

i am having two list box which perform add remove item functionality which are controlled by four buttons and o each button click there happen to be post back but i don't want it to be flicker for which i am using update panel like this but it still made post back wats wrong with this explain me this
<asp:UpdatePanel ID="button" runat="server" UpdateMode="Always">
<ContentTemplate>
<asp:Button ID="ButtonAdd" runat="server" Text=">" OnClick="ButtonAdd_Click" Width="50px"/><br />
<asp:Button ID="ButtonRemove" runat="server" Text="<" OnClick="ButtonRemove_Click" Width="50px"/><br />
<asp:Button ID="ButtonAddAll" runat="server" Text =">>>" OnClick="ButtonAddAll_Click" Width="50px"/><br />
<asp:Button ID="ButtonRemoveAll" runat="server" Text ="<<<" OnClick="ButtonRemoveAll_Click" Width="50px"/>
</ContentTemplate>
</asp:UpdatePanel>
I wrote a quick example that does work. You do not need your buttons in the UpdatePanel. You only need the ListBox since they are the only controls being refresh. Setup the Trigger for the UpdatePanel will cause the refreshes to occur without the 'flicker'.
aspx code:
<asp:ScriptManager ID="ScriptManager1" runat="server" />
<div>
<asp:Button ID="ButtonAdd" runat="server" Text=">" OnClick="ButtonAdd_Click" Width="50px"/><br />
<asp:Button ID="ButtonRemove" runat="server" Text="<" OnClick="ButtonRemove_Click" Width="50px"/><br />
<asp:Button ID="ButtonAddAll" runat="server" Text =">>>" OnClick="ButtonAddAll_Click" Width="50px"/><br />
<asp:Button ID="ButtonRemoveAll" runat="server" Text ="<<<" OnClick="ButtonRemoveAll_Click" Width="50px"/>
<asp:UpdatePanel ID="button" runat="server" UpdateMode="Always">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="ButtonAdd" EventName="Click" />
<asp:AsyncPostBackTrigger ControlID="ButtonRemove" EventName="Click" />
<asp:AsyncPostBackTrigger ControlID="ButtonAddAll" EventName="Click" />
<asp:AsyncPostBackTrigger ControlID="ButtonRemoveAll" EventName="Click" />
</Triggers>
<ContentTemplate>
<asp:ListBox ID="ListBox1" runat="server"></asp:ListBox>
<asp:ListBox ID="ListBox2" runat="server"></asp:ListBox>
</ContentTemplate>
</asp:UpdatePanel>
</div>
cs (codebehind) code:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ListBox1.Items.Add(new ListItem("Test1", "1"));
ListBox1.Items.Add(new ListItem("Test2", "2"));
ListBox1.Items.Add(new ListItem("Test3", "3"));
ListBox1.Items.Add(new ListItem("Test4", "4"));
ListBox1.Items.Add(new ListItem("Test5", "5"));
}
}
protected void ButtonRemove_Click(object sender, EventArgs e)
{
if (ListBox2.SelectedItem != null)
{
ListBox1.Items.Add(ListBox2.SelectedItem);
ListBox2.Items.RemoveAt(ListBox2.SelectedIndex);
ListBox2.ClearSelection();
ListBox1.ClearSelection();
}
}
protected void ButtonAdd_Click(object sender, EventArgs e)
{
if (ListBox1.SelectedItem != null)
{
ListBox2.Items.Add(ListBox1.SelectedItem);
ListBox1.Items.RemoveAt(ListBox1.SelectedIndex);
ListBox1.ClearSelection();
ListBox2.ClearSelection();
}
}
I have tested this and it does work. I only implemented 2 of the Buttons to present a complete example.
Add the asp:ListBox to the ContentTemplate of your asp:UpdatePanel, or add a new asp:UpdatePanel and add it to its ContentTemplate. The control won't be updated using an asynchronous postback if it's not placed within an asp:UpdatePanel.
With the following code snippets the newly added listBox will be updated with new content using an asynchronous postback:
Page.aspx:
<asp:UpdatePanel ID="button" runat="server" UpdateMode="Always">
<ContentTemplate>
<asp:Button ID="ButtonAdd" runat="server" Text=">" OnClick="ButtonAdd_Click" Width="50px" /><br />
<asp:Button ID="ButtonRemove" runat="server" Text="<" OnClick="ButtonRemove_Click"
Width="50px" />
<br />
<asp:Button ID="ButtonAddAll" runat="server" Text=">>>" OnClick="ButtonAddAll_Click"
Width="50px" />
<br />
<asp:Button ID="ButtonRemoveAll" runat="server" Text="<<<" OnClick="ButtonRemoveAll_Click"
Width="50px" />
<asp:ListBox runat="server" ID="listBox"></asp:ListBox>
</ContentTemplate>
</asp:UpdatePanel>
Sample event handler in the code-behind file, Page.aspx.cs:
protected void ButtonAddAll_Click(object sender, EventArgs e)
{
listBox.DataSource = new List<string>() { "Test" };
listBox.DataBind();
}
<asp:UpdatePanel ID="button" runat="server" UpdateMode="Always">
<ContentTemplate>
<asp:Button ID="ButtonAdd" runat="server" Text=">" OnClick="ButtonAdd_Click" Width="50px" /><br />
<asp:Button ID="ButtonRemove" runat="server" Text="<" OnClick="ButtonRemove_Click"
Width="50px" />
<br />
<asp:Button ID="ButtonAddAll" runat="server" Text=">>>" OnClick="ButtonAddAll_Click"
Width="50px" />
<br />
<asp:Button ID="ButtonRemoveAll" runat="server" Text="<<<" OnClick="ButtonRemoveAll_Click"
Width="50px" />
<asp:ListBox runat="server" ID="listBox"></asp:ListBox>
</ContentTemplate>
</asp:UpdatePanel>

Resources