By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,389 Members | 2,010 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,389 IT Pros & Developers. It's quick & easy.

datagrid and checkbox and confirm dialog

P: n/a
Hi All,

I have the following situation and am looking for answer in C#.

I have a datagrid and putting checkbox next to each record. In the
header I have a Delete button.

I want users to checkchekboxes and click the Delete button. That will
show a confirmation dialog message with the items they choose to
delete.

I have spent hours in the groups to find the similar one but without
any luck.
Here is the snippet of my code in page.aspx. The name (or id) of the
datagrid is BoreHolesResult. I will be updating based on the selected
checkbox and this checkbox should relate to the field Bore_Id.
<asp:TemplateColumn HeaderText="Type" >
<ItemTemplate>
<asp:TextBox ID="txtType" Runat="server" text='<
%#DataBinder.Eval(Container.DataItem, "BORE_TYPE") %>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn>
<HeaderTemplate>
<asp:Button ID="btnDeleteBoreId" Runat="server" Text="Delete"
Onclick="DeleteBoreID"></asp:Button>
</HeaderTemplate>
<ItemTemplate>
<center>
<asp:CheckBox ID='chkDelete' Runat="server">
</asp:CheckBox>
</center>
</ItemTemplate>
</asp:TemplateColumn>
In the code file (page.aspx.cs),
I have the following:
public void DeleteBoreID(object sender, System.EventArgs e)
{
string BoreID;
foreach (DataGridItem GridItem in BoreHolesResult.Items)
{
CheckBox myCheckbox =
(CheckBox)GridItem.Cells[8].Controls[1];
if (myCheckbox.Checked == true)
{
BoreID = GridItem.Cells[0].Text;
//here I want to show the confirm message with the
selected Bore Ids
// Cells[8] where the checkboxes are located in the datagrid 9th
item
//after confirm I will perform the delete records here.

}
}

}
I will highly appreciate your help. Thanks a million in advance.

best regards,
mamun

Feb 15 '07 #1
Share this Question
Share on Google+
4 Replies


P: n/a
On Feb 15, 1:32 pm, "mamun" <mamun...@hotmail.comwrote:
Hi All,

I have the following situation and am looking for answer in C#.

I have a datagrid and putting checkbox next to each record. In the
header I have a Delete button.

I want users to checkchekboxes and click the Delete button. That will
show a confirmation dialog message with the items they choose to
delete.

I have spent hours in the groups to find the similar one but without
any luck.

Here is the snippet of my code in page.aspx. The name (or id) of the
datagrid is BoreHolesResult. I will be updating based on the selected
checkbox and this checkbox should relate to the field Bore_Id.

<asp:TemplateColumn HeaderText="Type" >
<ItemTemplate>
<asp:TextBox ID="txtType" Runat="server" text='<
%#DataBinder.Eval(Container.DataItem, "BORE_TYPE") %>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateColumn>

<asp:TemplateColumn>
<HeaderTemplate>
<asp:Button ID="btnDeleteBoreId" Runat="server" Text="Delete"
Onclick="DeleteBoreID"></asp:Button>
</HeaderTemplate>
<ItemTemplate>
<center>
<asp:CheckBox ID='chkDelete' Runat="server">
</asp:CheckBox>
</center>
</ItemTemplate>
</asp:TemplateColumn>

In the code file (page.aspx.cs),

I have the following:

public void DeleteBoreID(object sender, System.EventArgs e)
{
string BoreID;
foreach (DataGridItem GridItem in BoreHolesResult.Items)
{
CheckBox myCheckbox =
(CheckBox)GridItem.Cells[8].Controls[1];
if (myCheckbox.Checked == true)
{
BoreID = GridItem.Cells[0].Text;
//here I want to show the confirm message with the
selected Bore Ids
// Cells[8] where the checkboxes are located in the datagrid 9th
item
//after confirm I will perform the delete records here.

}
}

}

I will highly appreciate your help. Thanks a million in advance.

best regards,
mamun
Almost there. What happen here in your code is you want the backend
server code to display a message to the client prior to it executing.
Not possible. What you'll need is to fire the client code first, then
trigger the server code (function DeleteBoreID). To do this, you'll
need a javascript function to do the confirmation, then based on this
result, return false (cancel executing the server code) or return true
(proceed executing the server code).

I won't go into details of changing your code due to time constraint,
but the idea is as followed. In your Page_Load function, add the
following line:

protected override void Page_Load(object sender, EventArgs e)
{
....
//Ties javascript to button DeleteBoreId. When it is pressed, the
confirm fires first,
// if it returns true, then server code DeleteBoreID will get
triggered
btnDeleteBoreId.Attributes.Add("onclick", "javascript:return
confirmDeletion('Are you sure you would like to remove the selected
items?');");
....
}

I think that's all.

Quoc Linh

Feb 15 '07 #2

P: n/a
On Feb 15, 2:28 pm, "Quoc Linh" <lequocl...@yahoo.comwrote:
On Feb 15, 1:32 pm, "mamun" <mamun...@hotmail.comwrote:
Hi All,
I have the following situation and am looking for answer in C#.
I have a datagrid and putting checkbox next to each record. In the
header I have a Delete button.
I want users to checkchekboxes and click the Delete button. That will
show a confirmation dialog message with the items they choose to
delete.
I have spent hours in the groups to find the similar one but without
any luck.
Here is the snippet of my code in page.aspx. The name (or id) of the
datagrid is BoreHolesResult. I will be updating based on the selected
checkbox and this checkbox should relate to the field Bore_Id.
<asp:TemplateColumn HeaderText="Type" >
<ItemTemplate>
<asp:TextBox ID="txtType" Runat="server" text='<
%#DataBinder.Eval(Container.DataItem, "BORE_TYPE") %>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn>
<HeaderTemplate>
<asp:Button ID="btnDeleteBoreId" Runat="server" Text="Delete"
Onclick="DeleteBoreID"></asp:Button>
</HeaderTemplate>
<ItemTemplate>
<center>
<asp:CheckBox ID='chkDelete' Runat="server">
</asp:CheckBox>
</center>
</ItemTemplate>
</asp:TemplateColumn>
In the code file (page.aspx.cs),
I have the following:
public void DeleteBoreID(object sender, System.EventArgs e)
{
string BoreID;
foreach (DataGridItem GridItem in BoreHolesResult.Items)
{
CheckBox myCheckbox =
(CheckBox)GridItem.Cells[8].Controls[1];
if (myCheckbox.Checked == true)
{
BoreID = GridItem.Cells[0].Text;
//here I want to show the confirm message with the
selected Bore Ids
// Cells[8] where the checkboxes are located in the datagrid 9th
item
//after confirm I will perform the delete records here.
}
}
}
I will highly appreciate your help. Thanks a million in advance.
best regards,
mamun

Almost there. What happen here in your code is you want the backend
server code to display a message to the client prior to it executing.
Not possible. What you'll need is to fire the client code first, then
trigger the server code (function DeleteBoreID). To do this, you'll
need a javascript function to do the confirmation, then based on this
result, return false (cancel executing the server code) or return true
(proceed executing the server code).

I won't go into details of changing your code due to time constraint,
but the idea is as followed. In your Page_Load function, add the
following line:

protected override void Page_Load(object sender, EventArgs e)
{
...
//Ties javascript to button DeleteBoreId. When it is pressed, the
confirm fires first,
// if it returns true, then server code DeleteBoreID will get
triggered
btnDeleteBoreId.Attributes.Add("onclick", "javascript:return
confirmDeletion('Are you sure you would like to remove the selected
items?');");
...

}

I think that's all.

Quoc Linh
Ah, typo:

return confirmDeletion('Are you sure you would like to remove the
selected
items?');");

should be:

return confirm('Are you sure you would like to remove the selected
items?');");

Quoc Linh

Feb 15 '07 #3

P: n/a
Hi Quoc Linh,

Thanks for replying.

Actually, I wanted client side validation and I changed according to
your suggestion but that did not help.

I would highly appreciate if you know any different way of doing this.
The main objective is users will select the checkboxes and the confirm
dialog box will show what records were selected (here is the bore_id
field) and if press yes then that will perform the delete query.

Many thanks

mamun

On Feb 15, 2:33 pm, "Quoc Linh" <lequocl...@yahoo.comwrote:
On Feb 15, 2:28 pm, "Quoc Linh" <lequocl...@yahoo.comwrote:


On Feb 15, 1:32 pm, "mamun" <mamun...@hotmail.comwrote:
Hi All,
I have the following situation and am looking for answer in C#.
I have a datagrid and putting checkbox next to each record. In the
header I have a Delete button.
I want users to checkchekboxes and click the Delete button. That will
show a confirmation dialog message with the items they choose to
delete.
I have spent hours in the groups to find the similar one but without
any luck.
Here is the snippet of my code in page.aspx. The name (or id) of the
datagrid is BoreHolesResult. I will be updating based on the selected
checkbox and this checkbox should relate to the field Bore_Id.
<asp:TemplateColumn HeaderText="Type" >
<ItemTemplate>
<asp:TextBox ID="txtType" Runat="server" text='<
%#DataBinder.Eval(Container.DataItem, "BORE_TYPE") %>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn>
<HeaderTemplate>
<asp:Button ID="btnDeleteBoreId" Runat="server" Text="Delete"
Onclick="DeleteBoreID"></asp:Button>
</HeaderTemplate>
<ItemTemplate>
<center>
<asp:CheckBox ID='chkDelete' Runat="server">
</asp:CheckBox>
</center>
</ItemTemplate>
</asp:TemplateColumn>
In the code file (page.aspx.cs),
I have the following:
public void DeleteBoreID(object sender, System.EventArgs e)
{
string BoreID;
foreach (DataGridItem GridItem in BoreHolesResult.Items)
{
CheckBox myCheckbox =
(CheckBox)GridItem.Cells[8].Controls[1];
if (myCheckbox.Checked == true)
{
BoreID = GridItem.Cells[0].Text;
//here I want to show the confirm message with the
selected Bore Ids
// Cells[8] where the checkboxes are located in the datagrid 9th
item
//after confirm I will perform the delete records here.
}
}
}
I will highly appreciate your help. Thanks a million in advance.
best regards,
mamun
Almost there. What happen here in your code is you want the backend
server code to display a message to the client prior to it executing.
Not possible. What you'll need is to fire the client code first, then
trigger the server code (function DeleteBoreID). To do this, you'll
need a javascript function to do the confirmation, then based on this
result, return false (cancel executing the server code) or return true
(proceed executing the server code).
I won't go into details of changing your code due to time constraint,
but the idea is as followed. In your Page_Load function, add the
following line:
protected override void Page_Load(object sender, EventArgs e)
{
...
//Ties javascript to button DeleteBoreId. When it is pressed, the
confirm fires first,
// if it returns true, then server code DeleteBoreID will get
triggered
btnDeleteBoreId.Attributes.Add("onclick", "javascript:return
confirmDeletion('Are you sure you would like to remove the selected
items?');");
...
}
I think that's all.
Quoc Linh

Ah, typo:

return confirmDeletion('Are you sure you would like to remove the
selected
items?');");

should be:

return confirm('Are you sure you would like to remove the selected
items?');");

Quoc Linh- Hide quoted text -

- Show quoted text -

Feb 15 '07 #4

P: n/a
On Feb 15, 3:34 pm, "mamun" <mamun...@hotmail.comwrote:
Hi Quoc Linh,

Thanks for replying.

Actually, I wanted client side validation and I changed according to
your suggestion but that did not help.

I would highly appreciate if you know any different way of doing this.
The main objective is users will select the checkboxes and the confirm
dialog box will show what records were selected (here is the bore_id
field) and if press yes then that will perform the delete query.

Many thanks

mamun

On Feb 15, 2:33 pm, "Quoc Linh" <lequocl...@yahoo.comwrote:
On Feb 15, 2:28 pm, "Quoc Linh" <lequocl...@yahoo.comwrote:
On Feb 15, 1:32 pm, "mamun" <mamun...@hotmail.comwrote:
Hi All,
I have the following situation and am looking for answer in C#.
I have a datagrid and putting checkbox next to each record. In the
header I have a Delete button.
I want users to checkchekboxes and click the Delete button. That will
show a confirmation dialog message with the items they choose to
delete.
I have spent hours in the groups to find the similar one but without
any luck.
Here is the snippet of my code in page.aspx. The name (or id) of the
datagrid is BoreHolesResult. I will be updating based on the selected
checkbox and this checkbox should relate to the field Bore_Id.
<asp:TemplateColumn HeaderText="Type" >
<ItemTemplate>
<asp:TextBox ID="txtType" Runat="server" text='<
%#DataBinder.Eval(Container.DataItem, "BORE_TYPE") %>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn>
<HeaderTemplate>
<asp:Button ID="btnDeleteBoreId" Runat="server" Text="Delete"
Onclick="DeleteBoreID"></asp:Button>
</HeaderTemplate>
<ItemTemplate>
<center>
<asp:CheckBox ID='chkDelete' Runat="server">
</asp:CheckBox>
</center>
</ItemTemplate>
</asp:TemplateColumn>
In the code file (page.aspx.cs),
I have the following:
public void DeleteBoreID(object sender, System.EventArgs e)
{
string BoreID;
foreach (DataGridItem GridItem in BoreHolesResult.Items)
{
CheckBox myCheckbox =
(CheckBox)GridItem.Cells[8].Controls[1];
if (myCheckbox.Checked == true)
{
BoreID = GridItem.Cells[0].Text;
//here I want to show the confirm message with the
selected Bore Ids
// Cells[8] where the checkboxes are located in the datagrid 9th
item
//after confirm I will perform the delete records here.
}
}
}
I will highly appreciate your help. Thanks a million in advance.
best regards,
mamun
Almost there. What happen here in your code is you want the backend
server code to display a message to the client prior to it executing.
Not possible. What you'll need is to fire the client code first, then
trigger the server code (function DeleteBoreID). To do this, you'll
need a javascript function to do the confirmation, then based on this
result, return false (cancel executing the server code) or return true
(proceed executing the server code).
I won't go into details of changing your code due to time constraint,
but the idea is as followed. In your Page_Load function, add the
following line:
protected override void Page_Load(object sender, EventArgs e)
{
...
//Ties javascript to button DeleteBoreId. When it is pressed, the
confirm fires first,
// if it returns true, then server code DeleteBoreID will get
triggered
btnDeleteBoreId.Attributes.Add("onclick", "javascript:return
confirmDeletion('Are you sure you would like to remove the selected
items?');");
...
}
I think that's all.
Quoc Linh
Ah, typo:
return confirmDeletion('Are you sure you would like to remove the
selected
items?');");
should be:
return confirm('Are you sure you would like to remove the selected
items?');");
Quoc Linh- Hide quoted text -
- Show quoted text -
Using the way I proposed earlier, you should be able to achieve:
User are able to check the checkboxes, then when click the delete
button, user receives a generic deletion confirmation message. If
user choose Yes, a post-back occurs, and the server will perform the
deletion. Is this correct?

If I understand you correctly, you now want the confirm dialog to
contain the description of the items you'd like to delete. To do
this, it is more tricky.

There are 3 ways I know of:

1. Same principal as I showed you, but replace the confirm("")
function with a Javascript function. Code the javascript function to
loop through your grid (rendered as html table), for each item, get
the checkbox values, build a string, and display a confirmed dialog
box. Return true/false in this function (same principal as the way I
showed you earlier)

2. Use Ajax.
As user clicks the checkbox, keep track of the checkbox states on the
server side.
As user clicked the button, use the server code to build the string,
then display at the client side.

I hear good comments about this one: http://www.ajaxpro.info/

3. Use Remote scripting. The predecessor of Ajax.
Here's a convenient tool to do it: http://www.codeproject.com/aspnet/
AlvaroRemoteScripting.asp

HTH,
Quoc Linh

Feb 16 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.