DataGrid ButtonColumn


R

rn5a

A DataGrid is populated with the records existing in a database. Each
of the row in this DataGrid has a ButtonColumn. Assume that the
DataGrid displays 10 records (i.e. 10 DataGridItems/rows). Each row in
the DataGrid (of course, except the Header & the Footer) is
accompanied by a ButtonColumn i.e. there are 10 Buttons in the
DataGrid.

The Buttons that get rendered by the ButtonColumns - how do I get the
ID of each of these Buttons?
 
Ad

Advertisements

A

Alexey Smirnov

A DataGrid is populated with the records existing in a database. Each
of the row in this DataGrid has a ButtonColumn. Assume that the
DataGrid displays 10 records (i.e. 10 DataGridItems/rows). Each row in
the DataGrid (of course, except the Header & the Footer) is
accompanied by a ButtonColumn i.e. there are 10 Buttons in the
DataGrid.

The Buttons that get rendered by the ButtonColumns - how do I get the
ID of each of these Buttons?

The IDs for each button are autogenerated by the ButtonColumns. If you
need to find which row has to be updated (which button has been
clicked) you don't need to have IDs, you should use the CommandName
property and OnItemCommand event...

For example:

protected void DataGrid1_OnItemCommand(Object sender,
DataGridCommandEventArgs e)
{
if(e.CommandName=="delete")
{
sql = "DELETE FROM... WHERE id=" +
DataGrid1.DataKeys(CInt(e.Item.ItemIndex))
}
}
 
R

rn5a

The IDs for each button are autogenerated by the ButtonColumns. If you
need to find which row has to be updated (which button has been
clicked) you don't need to have IDs, you should use the CommandName
property and OnItemCommand event...

For example:

protected void DataGrid1_OnItemCommand(Object sender,
DataGridCommandEventArgs e)
{
if(e.CommandName=="delete")
{
sql = "DELETE FROM... WHERE id=" +
DataGrid1.DataKeys(CInt(e.Item.ItemIndex))



}
}- Hide quoted text -

- Show quoted text -

Well, Alexey, my main intention to get the IDs of each of the buttons
is to disable the button immediately after the button is clicked for
the first time so that I can prevent users from clicking the button
again & again to avoid posting the same data over & over again.

Any idea how could I do this?

Thanks for the prompt response.
 
A

Alexey Smirnov

Well, Alexey, my main intention to get the IDs of each of the buttons
is to disable the button immediately after the button is clicked for
the first time so that I can prevent users from clicking the button
again & again to avoid posting the same data over & over again.

Any idea how could I do this?

Thanks for the prompt response.- Hide quoted text -

- Show quoted text -

I think something like this

protected void DataGrid1_OnItemCommand(Object sender,
DataGridCommandEventArgs e)
{
if(e.CommandName=="delete")
{
 
R

rn5a

I think something like this

protected void DataGrid1_OnItemCommand(Object sender,
DataGridCommandEventArgs e)
{
if(e.CommandName=="delete")
{

---------------------------------------------------------------------------­-
Button myButton = (Button) e.CommandSource;
myButton.Enabled = false;
---------------------------------------------------------------------------­-

sql = "DELETE FROM... WHERE id=" +
DataGrid1.DataKeys(CInt(e.Item.ItemIndex))- Hide quoted text -

- Show quoted text -

Alexey, what is the VB equivalent of (Button) e.CommandSource?

Also what if I replace the ButtonColumn with an EditCommandColumn?
 
A

Alexey Smirnov

Alexey, what is the VB equivalent of (Button) e.CommandSource?

Dim myButton as Button = CType(e.CommandSource, Button)

Also what if I replace the ButtonColumn with an EditCommandColumn?- Hide quoted text -

I think it should be similar
 
Ad

Advertisements

R

rn5a

Dim myButton as Button = CType(e.CommandSource, Button)


I think it should be similar- Hide quoted text -

- Show quoted text -
Dim myButton as Button = CType(e.CommandSource, Button)

Yeah....that's exactly what I did but overall, your suggestion doesn't
work, Alexey.

Since I am working on my local intranet where clicking a button
immediately executes a sub, I added the following code in the
DataGrid's ItemCommand event:

Sub dg1_ItemCommand(.......)
Dim i As Integer
Dim btn As Button

If (ea.CommandName = "delete") Then
btn = CType(ea.CommandSource, Button)
btn.Enabled = False

For i = 0 To 1000000000
Response.Write(i)
Next
End If
End Sub

Note the For.....Next loop. I added that loop to prolong the Form
submission to verify whether the button actually gets disabled or not
after it is clicked for the first time but it doesn't get disabled; it
remains enabled even after the button is clicked for the first time.

Any other ideas/suggestions?
 
A

Alexey Smirnov

Yeah....that's exactly what I did but overall, your suggestion doesn't
work, Alexey.

Since I am working on my local intranet where clicking a button
immediately executes a sub, I added the following code in the
DataGrid's ItemCommand event:

Sub dg1_ItemCommand(.......)
Dim i As Integer
Dim btn As Button

If (ea.CommandName = "delete") Then
btn = CType(ea.CommandSource, Button)
btn.Enabled = False

For i = 0 To 1000000000
Response.Write(i)
Next
End If
End Sub

Note the For.....Next loop. I added that loop to prolong the Form
submission to verify whether the button actually gets disabled or not
after it is clicked for the first time but it doesn't get disabled; it
remains enabled even after the button is clicked for the first time.

Any other ideas/suggestions?- Hide quoted text -

- Show quoted text -

For.....Next has nothing to do with the button, don't you think?

Enabled = False should change the button to grey, and user will not be
able to click it once more (For.....Next can be executed only once per
row item)
 
R

rn5a

For.....Next has nothing to do with the button, don't you think?

Enabled = False should change the button to grey, and user will not be
able to click it once more (For.....Next can be executed only once per
row item)- Hide quoted text -

- Show quoted text -

But Alexey Enabled = False DOESN'T disable the button after the button
is clicked. & I am saying this after trying out Enabled = False
repeatedly. The button just refuses to grey out after the first click.
 
Ad

Advertisements

A

Alexey Smirnov

But Alexey Enabled = False DOESN'T disable the button after the button
is clicked. & I am saying this after trying out Enabled = False
repeatedly. The button just refuses to grey out after the first click.- Hide quoted text -

For me it works.

My code:

The grid

<asp:DataGrid ID="DataGrid1" OnItemCommand="DataGrid1_ItemCommand"...
<Columns>
....
<asp:ButtonColumn Text="Delete" CommandName="Delete"
ButtonType="PushButton"></asp:ButtonColumn>
</Columns>
</asp:DataGrid>

Code-behind:

Sub DataGrid1_ItemCommand(ByVal Source As Object, ByVal e As
DataGridCommandEventArgs)
If (e.CommandName = "Delete") Then
Dim btn As Button = CType(e.CommandSource, Button)
btn.Enabled = False
End If
End Sub

The code above disable the button once it clicked.

Note, that the ButtonColumn can have another ButtonType="LinkButton".
For this type of button you should use another cast

Dim btn As LinkButton = CType(e.CommandSource, LinkButton)

This will disable the button (actually the link) too.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top