• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 639
  • Last Modified:

asp.net javascript confirm how to

Hi,

I have a repeater control, I am retrieving names of people to the repeater control from sql table and showing it and i also made a delete button called DEL next to each name on the page.

when someone hits it. I want to pop open a confirm box and ask if the user really wants to delete specified user.

i can pop open a confirm box and ask a question but how will i put the name in the question?

question is :

1) how can i put the name of the user next to the question in the confirm box since page does not know which button is related with which name?(you can see the file attached for to see names next to buttons)

2)if the user hits OK in the confirm box i want to check if name has any other records and if there are records i want to warn user again...

i am using the confirm box in the source section(html part) but to delete name from the table and to make other controls i need to go to code behind. How will i do that jump from confirm box's OK answer to code behind to make some controls and delete specified user?


In the below code you can see the confirm box but the question is missing the name that i retrieved with EVAL and also if user hits OK where should i write other code to check?;

  <asp:Repeater ID="Repeater1" runat="server">
    <ItemTemplate>
    <div>
    <div>
    <table>
    <tr>
    <td style="width:180px">
    <%#DataBinder.Eval(Container.DataItem, "hasta_adi")%>
    </td>
    <td><button onclick="confirm('Do you really want to delete?...');">DEL</button></td>
    </tr>
    </table>
    </div>
   
    </div>
    </ItemTemplate>
    </asp:Repeater>
del.JPG
0
nicedone
Asked:
nicedone
  • 3
  • 3
1 Solution
 
informaniacCommented:
<td><button onclick="confirm('Do you really want to delete?'+<%#DataBinder.Eval(Container.DataItem, "hasta_adi")%>+'');">DEL</button></td>

Open in new window

0
 
nicedoneAuthor Commented:
Thank you informaniac,

if the user hits OK to that question i need to do SQL db operations like

checking if records exists for this name

if yes again confirm box to confirm deletion, if OK again

then delete operation should be performed !!!

Can i do that through javascript?
0
 
informaniacCommented:
Please try this piece of code
<asp:Repeater ID="Repeater1" runat="server" 
            onitemcommand="Repeater1_ItemCommand" onitemdatabound="Repeater1_ItemDataBound">
            <ItemTemplate>
                <asp:Button ID="btnDelete" runat="server" CommandArgument="hasta_adi" Text="DEL" />
            </ItemTemplate>
        </asp:Repeater>

Open in new window


protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName=="Delete")
            {
                string hasta_adi = e.CommandArgument.ToString();
                // Write code to delete data from SQL
            }
        }

        protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                Button btnDelete = e.Item.FindControl("btnDelete") as Button;
                btnDelete.Attributes.Add("onclick", "javascript:return confirm('Are you sure you want to delete:-" + btnDelete.CommandArgument.ToString() + "');");
            }
        }

Open in new window

0
Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

 
nicedoneAuthor Commented:
informaniac tnx for the good code,

in your code in confirm pop up i was not able to get the names so i modified it as below instead CommandArgument="hasta_adi";

<asp:Button ID="btnDelete" runat="server" CommandArgument=<%#DataBinder.Eval(Container.DataItem,"hasta_adi")%> Text="DEL" />

Also e.CommandName for the btnDelete was empty since we did not give a name i also gave CommandName=Delete to asp button and it all worked... thank you very much...

One final thing if you do i wd appreciate, i did not know those properties as Repeater1_ItemCommand and Repeater1_ItemDataBound

where can i get broad information about them, tutorial or smthgn since i dont have deep knowledge on that?

i did those with your help but i want it to make sense to me too, i need to learn things about them... thank you..

thank you again for your help...
0
 
nicedoneAuthor Commented:
tnx
0
 
informaniacCommented:
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.repeater.itemdatabound.aspx

ItemDataBound is called for each record that gets bound to the Repeater.

And ItemCommand would be called whenever any control inside the repeater causes a postback.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now