Execute Javascript From Buttons Inside Datagrid

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hello,
I have a datagrid and the data in it is dynamically created at runtime...

For iCounter = 0 To dataset.Tables(0).Columns.Count - 1
Dim objbc As New BoundColumn()

With objbc
.DataField = dsAllPastReplies.Tables(0).Columns(iCounter).ColumnName
.HeaderText = dsAllPastReplies.Tables(0).Columns(iCounter).ColumnName
If .DataField = dsAllPastReplies.Tables(0).Columns("ReplyID").ColumnName Then
.Visible = False
End If
End With

With datagrid1
.Columns.Add(objbc)
.DataSource = dsAllPastReplies.Tables(0)
.DataBind()
End With
Next
dsAllPastReplies.Clear()

I can add a column of buttons at design time, but how can I fire off some javascript when a button is clicked? I would ultimately like to have a user click one of the buttons and data in the corresponding row for a given column would come up in a popup window. I have the javascript to do the popup, but I cant figure out how to call it from the dynamically created buttons.
 
Hi,
You can use the Attributes collection of the dynamic button as in:

objbc.Attributes.Add("onClick", "window.open('http://www.google.com',
'googleWin');")

Hello,
I have a datagrid and the data in it is dynamically created at runtime...

For iCounter = 0 To dataset.Tables(0).Columns.Count - 1
Dim objbc As New BoundColumn()

With objbc
.DataField =
dsAllPastReplies.Tables(0).Columns(iCounter).ColumnName
.HeaderText =
dsAllPastReplies.Tables(0).Columns(iCounter).ColumnName
If .DataField =
dsAllPastReplies.Tables(0).Columns("ReplyID").ColumnName Then
.Visible = False
End If
End With

With datagrid1
.Columns.Add(objbc)
.DataSource = dsAllPastReplies.Tables(0)
.DataBind()
End With
Next
dsAllPastReplies.Clear()

I can add a column of buttons at design time, but how can I fire off some
javascript when a button is clicked? I would ultimately like to have a user
click one of the buttons and data in the corresponding row for a given
column would come up in a popup window. I have the javascript to do the
popup, but I cant figure out how to call it from the dynamically created
buttons.
 
Yeah, thats what I was originally hoping for, BUT....
'attributes is not a member of System.Web.UI.WebControls.ButtonColumn'
so you cant do it that way. Unless theres a reference or property I might be missing??
 
One option is to handle the ItemDataBound event of the grid and dynamically
add the JS for popup window.

E.g.:
void MyGrid_DataBound(Object sender, DataGridItemEventArgs e)
{
if (e.Item.ItemType != ListItemType.Header && e.Item.ItemType !=
ListItemType.Footer)
{
Button b = (Button)e.Item.Cells[1].Controls[0]; //Cells[1]: 2nd col is
the button column;
b.Attributes.Add("onClick", "window.open('http://www.google.com');");
}
}

HTH.

Yeah, thats what I was originally hoping for, BUT....
'attributes is not a member of System.Web.UI.WebControls.ButtonColumn'
so you cant do it that way. Unless theres a reference or property I might
be missing??
 
Thanks for the info. Could you please tell me what event this is from and make the code in VB .Net? I havent touched any C# (or C++ for that matter)
--
Any info will be very appreciated!

Jon


Shiva said:
One option is to handle the ItemDataBound event of the grid and dynamically
add the JS for popup window.

E.g.:
void MyGrid_DataBound(Object sender, DataGridItemEventArgs e)
{
if (e.Item.ItemType != ListItemType.Header && e.Item.ItemType !=
ListItemType.Footer)
{
Button b = (Button)e.Item.Cells[1].Controls[0]; //Cells[1]: 2nd col is
the button column;
b.Attributes.Add("onClick", "window.open('http://www.google.com');");
}
}

HTH.

Yeah, thats what I was originally hoping for, BUT....
'attributes is not a member of System.Web.UI.WebControls.ButtonColumn'
so you cant do it that way. Unless theres a reference or property I might
be missing??

--
Any info will be very appreciated!

Jon


Shiva said:
Hi,
You can use the Attributes collection of the dynamic button as in:

objbc.Attributes.Add("onClick", "window.open('http://www.google.com',
'googleWin');")

Hello,
I have a datagrid and the data in it is dynamically created at runtime...

For iCounter = 0 To dataset.Tables(0).Columns.Count - 1
Dim objbc As New BoundColumn()

With objbc
.DataField =
dsAllPastReplies.Tables(0).Columns(iCounter).ColumnName
.HeaderText =
dsAllPastReplies.Tables(0).Columns(iCounter).ColumnName
If .DataField =
dsAllPastReplies.Tables(0).Columns("ReplyID").ColumnName Then
.Visible = False
End If
End With

With datagrid1
.Columns.Add(objbc)
.DataSource = dsAllPastReplies.Tables(0)
.DataBind()
End With
Next
dsAllPastReplies.Clear()

I can add a column of buttons at design time, but how can I fire off some
javascript when a button is clicked? I would ultimately like to have a user
click one of the buttons and data in the corresponding row for a given
column would come up in a popup window. I have the javascript to do the
popup, but I cant figure out how to call it from the dynamically created
buttons.
 
Hi,
This is for DataGrid's ItemDataBound event.

Here is the VB.NET equivalent:
Sub MyGrid_DataBound(sender As Object, e As DataGridItemEventArgs) Handles
MyGrid.ItemDataBound
Dim b As Button
If (e.Item.ItemType <> ListItemType.Header AndAlso e.Item.ItemType <>
ListItemType.Footer) Then
b = CType (e.Item.Cells(1).Controls(0), Button) 'Cells[1]: 2nd col is the
button column
b.Attributes.Add("onClick", "window.open('http://www.google.com');")
End If
End Sub

Hope this helps.

Thanks for the info. Could you please tell me what event this is from and
make the code in VB .Net? I havent touched any C# (or C++ for that matter)
--
Any info will be very appreciated!

Jon


Shiva said:
One option is to handle the ItemDataBound event of the grid and dynamically
add the JS for popup window.

E.g.:
void MyGrid_DataBound(Object sender, DataGridItemEventArgs e)
{
if (e.Item.ItemType != ListItemType.Header && e.Item.ItemType !=
ListItemType.Footer)
{
Button b = (Button)e.Item.Cells[1].Controls[0]; //Cells[1]: 2nd col is
the button column;
b.Attributes.Add("onClick", "window.open('http://www.google.com');");
}
}

HTH.

Yeah, thats what I was originally hoping for, BUT....
'attributes is not a member of System.Web.UI.WebControls.ButtonColumn'
so you cant do it that way. Unless theres a reference or property I might
be missing??

--
Any info will be very appreciated!

Jon


Shiva said:
Hi,
You can use the Attributes collection of the dynamic button as in:

objbc.Attributes.Add("onClick", "window.open('http://www.google.com',
'googleWin');")

Hello,
I have a datagrid and the data in it is dynamically created at runtime...

For iCounter = 0 To dataset.Tables(0).Columns.Count - 1
Dim objbc As New BoundColumn()

With objbc
.DataField =
dsAllPastReplies.Tables(0).Columns(iCounter).ColumnName
.HeaderText =
dsAllPastReplies.Tables(0).Columns(iCounter).ColumnName
If .DataField =
dsAllPastReplies.Tables(0).Columns("ReplyID").ColumnName Then
.Visible = False
End If
End With

With datagrid1
.Columns.Add(objbc)
.DataSource = dsAllPastReplies.Tables(0)
.DataBind()
End With
Next
dsAllPastReplies.Clear()

I can add a column of buttons at design time, but how can I fire off some
javascript when a button is clicked? I would ultimately like to have a user
click one of the buttons and data in the corresponding row for a given
column would come up in a popup window. I have the javascript to do the
popup, but I cant figure out how to call it from the dynamically created
buttons.
 
Since the ButtonColumn is created at runtime, you should create them all
again on postback also(using the same logic used for initial creation). This
would fix the 2nd problem.

For the 1st one, you have handle the ItemCommand event of the DataGrid.
Refer to MSDN for a sample (search for DataGrid.ItemCommand Event).

That did it! Two last things before I put this puppy to bed...
1. How do I grab a value from the grid in the row upon which a button was
pressed?

2. When I click one of the buttons, the column containing the button
disappears on the post back. Is there a setting/property I need to change
to prevent this from happening?

Thanks for your help!
--
Any info will be very appreciated!

Jon


Shiva said:
Hi,
This is for DataGrid's ItemDataBound event.

Here is the VB.NET equivalent:
Sub MyGrid_DataBound(sender As Object, e As DataGridItemEventArgs) Handles
MyGrid.ItemDataBound
Dim b As Button
If (e.Item.ItemType <> ListItemType.Header AndAlso e.Item.ItemType <>
ListItemType.Footer) Then
b = CType (e.Item.Cells(1).Controls(0), Button) 'Cells[1]: 2nd col is the
button column
b.Attributes.Add("onClick", "window.open('http://www.google.com');")
End If
End Sub

Hope this helps.

Thanks for the info. Could you please tell me what event this is from and
make the code in VB .Net? I havent touched any C# (or C++ for that matter)
--
Any info will be very appreciated!

Jon


Shiva said:
One option is to handle the ItemDataBound event of the grid and dynamically
add the JS for popup window.

E.g.:
void MyGrid_DataBound(Object sender, DataGridItemEventArgs e)
{
if (e.Item.ItemType != ListItemType.Header && e.Item.ItemType !=
ListItemType.Footer)
{
Button b = (Button)e.Item.Cells[1].Controls[0]; //Cells[1]: 2nd col is
the button column;
b.Attributes.Add("onClick", "window.open('http://www.google.com');");
}
}

HTH.

Yeah, thats what I was originally hoping for, BUT....
'attributes is not a member of System.Web.UI.WebControls.ButtonColumn'
so you cant do it that way. Unless theres a reference or property I might
be missing??

--
Any info will be very appreciated!

Jon


Shiva said:
Hi,
You can use the Attributes collection of the dynamic button as in:

objbc.Attributes.Add("onClick", "window.open('http://www.google.com',
'googleWin');")

Hello,
I have a datagrid and the data in it is dynamically created at runtime...

For iCounter = 0 To dataset.Tables(0).Columns.Count - 1
Dim objbc As New BoundColumn()

With objbc
.DataField =
dsAllPastReplies.Tables(0).Columns(iCounter).ColumnName
.HeaderText =
dsAllPastReplies.Tables(0).Columns(iCounter).ColumnName
If .DataField =
dsAllPastReplies.Tables(0).Columns("ReplyID").ColumnName Then
.Visible = False
End If
End With

With datagrid1
.Columns.Add(objbc)
.DataSource = dsAllPastReplies.Tables(0)
.DataBind()
End With
Next
dsAllPastReplies.Clear()

I can add a column of buttons at design time, but how can I fire off some
javascript when a button is clicked? I would ultimately like to have
a
user
click one of the buttons and data in the corresponding row for a given
column would come up in a popup window. I have the javascript to do the
popup, but I cant figure out how to call it from the dynamically created
buttons.
 
Back
Top