Execute Javascript From Buttons Inside Datagrid

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jon

    Execute Javascript From Buttons Inside Datagrid

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

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

    With objbc
    .DataField = dsAllPastReplie s.Tables(0).Col umns(iCounter). ColumnName
    .HeaderText = dsAllPastReplie s.Tables(0).Col umns(iCounter). ColumnName
    If .DataField = dsAllPastReplie s.Tables(0).Col umns("ReplyID") .ColumnName Then
    .Visible = False
    End If
    End With

    With datagrid1
    .Columns.Add(ob jbc)
    .DataSource = dsAllPastReplie s.Tables(0)
    .DataBind()
    End With
    Next
    dsAllPastReplie s.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.
    --
    Any info will be very appreciated!

    Jon
  • Shiva

    #2
    Re: Execute Javascript From Buttons Inside Datagrid

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

    objbc.Attribute s.Add("onClick" , "window.open('h ttp://www.google.com' ,
    'googleWin');")

    "Jon" <Jon@discussion s.microsoft.com > wrote in message
    news:8505E2E2-C195-4D2D-96AF-53D5B531CFA6@mi crosoft.com...
    Hello,
    I have a datagrid and the data in it is dynamically created at runtime...

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

    With objbc
    .DataField =
    dsAllPastReplie s.Tables(0).Col umns(iCounter). ColumnName
    .HeaderText =
    dsAllPastReplie s.Tables(0).Col umns(iCounter). ColumnName
    If .DataField =
    dsAllPastReplie s.Tables(0).Col umns("ReplyID") .ColumnName Then
    .Visible = False
    End If
    End With

    With datagrid1
    .Columns.Add(ob jbc)
    .DataSource = dsAllPastReplie s.Tables(0)
    .DataBind()
    End With
    Next
    dsAllPastReplie s.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.
    --
    Any info will be very appreciated!

    Jon


    Comment

    • Jon

      #3
      Re: Execute Javascript From Buttons Inside Datagrid

      Yeah, thats what I was originally hoping for, BUT....
      'attributes is not a member of System.Web.UI.W ebControls.Butt onColumn'
      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" wrote:
      [color=blue]
      > Hi,
      > You can use the Attributes collection of the dynamic button as in:
      >
      > objbc.Attribute s.Add("onClick" , "window.open('h ttp://www.google.com' ,
      > 'googleWin');")
      >
      > "Jon" <Jon@discussion s.microsoft.com > wrote in message
      > news:8505E2E2-C195-4D2D-96AF-53D5B531CFA6@mi crosoft.com...
      > Hello,
      > I have a datagrid and the data in it is dynamically created at runtime...
      >
      > For iCounter = 0 To dataset.Tables( 0).Columns.Coun t - 1
      > Dim objbc As New BoundColumn()
      >
      > With objbc
      > .DataField =
      > dsAllPastReplie s.Tables(0).Col umns(iCounter). ColumnName
      > .HeaderText =
      > dsAllPastReplie s.Tables(0).Col umns(iCounter). ColumnName
      > If .DataField =
      > dsAllPastReplie s.Tables(0).Col umns("ReplyID") .ColumnName Then
      > .Visible = False
      > End If
      > End With
      >
      > With datagrid1
      > .Columns.Add(ob jbc)
      > .DataSource = dsAllPastReplie s.Tables(0)
      > .DataBind()
      > End With
      > Next
      > dsAllPastReplie s.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.
      > --
      > Any info will be very appreciated!
      >
      > Jon
      >
      >
      >[/color]

      Comment

      • Shiva

        #4
        Re: Execute Javascript From Buttons Inside Datagrid

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

        E.g.:
        void MyGrid_DataBoun d(Object sender, DataGridItemEve ntArgs e)
        {
        if (e.Item.ItemTyp e != ListItemType.He ader && e.Item.ItemType !=
        ListItemType.Fo oter)
        {
        Button b = (Button)e.Item. Cells[1].Controls[0]; //Cells[1]: 2nd col is
        the button column;
        b.Attributes.Ad d("onClick", "window.open('h ttp://www.google.com' );");
        }
        }

        HTH.

        "Jon" <Jon@discussion s.microsoft.com > wrote in message
        news:D3D6079B-76AD-4D85-89AF-FC624BEF6F3D@mi crosoft.com...
        Yeah, thats what I was originally hoping for, BUT....
        'attributes is not a member of System.Web.UI.W ebControls.Butt onColumn'
        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" wrote:
        [color=blue]
        > Hi,
        > You can use the Attributes collection of the dynamic button as in:
        >
        > objbc.Attribute s.Add("onClick" , "window.open('h ttp://www.google.com' ,
        > 'googleWin');")
        >
        > "Jon" <Jon@discussion s.microsoft.com > wrote in message
        > news:8505E2E2-C195-4D2D-96AF-53D5B531CFA6@mi crosoft.com...
        > Hello,
        > I have a datagrid and the data in it is dynamically created at runtime...
        >
        > For iCounter = 0 To dataset.Tables( 0).Columns.Coun t - 1
        > Dim objbc As New BoundColumn()
        >
        > With objbc
        > .DataField =
        > dsAllPastReplie s.Tables(0).Col umns(iCounter). ColumnName
        > .HeaderText =
        > dsAllPastReplie s.Tables(0).Col umns(iCounter). ColumnName
        > If .DataField =
        > dsAllPastReplie s.Tables(0).Col umns("ReplyID") .ColumnName Then
        > .Visible = False
        > End If
        > End With
        >
        > With datagrid1
        > .Columns.Add(ob jbc)
        > .DataSource = dsAllPastReplie s.Tables(0)
        > .DataBind()
        > End With
        > Next
        > dsAllPastReplie s.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[/color]
        user[color=blue]
        > 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.
        > --
        > Any info will be very appreciated!
        >
        > Jon
        >
        >
        >[/color]


        Comment

        • Jon

          #5
          Re: Execute Javascript From Buttons Inside Datagrid

          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" wrote:
          [color=blue]
          > One option is to handle the ItemDataBound event of the grid and dynamically
          > add the JS for popup window.
          >
          > E.g.:
          > void MyGrid_DataBoun d(Object sender, DataGridItemEve ntArgs e)
          > {
          > if (e.Item.ItemTyp e != ListItemType.He ader && e.Item.ItemType !=
          > ListItemType.Fo oter)
          > {
          > Button b = (Button)e.Item. Cells[1].Controls[0]; //Cells[1]: 2nd col is
          > the button column;
          > b.Attributes.Ad d("onClick", "window.open('h ttp://www.google.com' );");
          > }
          > }
          >
          > HTH.
          >
          > "Jon" <Jon@discussion s.microsoft.com > wrote in message
          > news:D3D6079B-76AD-4D85-89AF-FC624BEF6F3D@mi crosoft.com...
          > Yeah, thats what I was originally hoping for, BUT....
          > 'attributes is not a member of System.Web.UI.W ebControls.Butt onColumn'
          > 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" wrote:
          >[color=green]
          > > Hi,
          > > You can use the Attributes collection of the dynamic button as in:
          > >
          > > objbc.Attribute s.Add("onClick" , "window.open('h ttp://www.google.com' ,
          > > 'googleWin');")
          > >
          > > "Jon" <Jon@discussion s.microsoft.com > wrote in message
          > > news:8505E2E2-C195-4D2D-96AF-53D5B531CFA6@mi crosoft.com...
          > > Hello,
          > > I have a datagrid and the data in it is dynamically created at runtime...
          > >
          > > For iCounter = 0 To dataset.Tables( 0).Columns.Coun t - 1
          > > Dim objbc As New BoundColumn()
          > >
          > > With objbc
          > > .DataField =
          > > dsAllPastReplie s.Tables(0).Col umns(iCounter). ColumnName
          > > .HeaderText =
          > > dsAllPastReplie s.Tables(0).Col umns(iCounter). ColumnName
          > > If .DataField =
          > > dsAllPastReplie s.Tables(0).Col umns("ReplyID") .ColumnName Then
          > > .Visible = False
          > > End If
          > > End With
          > >
          > > With datagrid1
          > > .Columns.Add(ob jbc)
          > > .DataSource = dsAllPastReplie s.Tables(0)
          > > .DataBind()
          > > End With
          > > Next
          > > dsAllPastReplie s.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[/color]
          > user[color=green]
          > > 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.
          > > --
          > > Any info will be very appreciated!
          > >
          > > Jon
          > >
          > >
          > >[/color]
          >
          >
          >[/color]

          Comment

          • Shiva

            #6
            Re: Execute Javascript From Buttons Inside Datagrid

            Hi,
            This is for DataGrid's ItemDataBound event.

            Here is the VB.NET equivalent:
            Sub MyGrid_DataBoun d(sender As Object, e As DataGridItemEve ntArgs) Handles
            MyGrid.ItemData Bound
            Dim b As Button
            If (e.Item.ItemTyp e <> ListItemType.He ader AndAlso e.Item.ItemType <>
            ListItemType.Fo oter) Then
            b = CType (e.Item.Cells(1 ).Controls(0), Button) 'Cells[1]: 2nd col is the
            button column
            b.Attributes.Ad d("onClick", "window.open('h ttp://www.google.com' );")
            End If
            End Sub

            Hope this helps.

            "Jon" <Jon@discussion s.microsoft.com > wrote in message
            news:7BF3A2D0-AED6-47F0-A9C2-2CCD28700903@mi crosoft.com...
            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" wrote:
            [color=blue]
            > One option is to handle the ItemDataBound event of the grid and[/color]
            dynamically[color=blue]
            > add the JS for popup window.
            >
            > E.g.:
            > void MyGrid_DataBoun d(Object sender, DataGridItemEve ntArgs e)
            > {
            > if (e.Item.ItemTyp e != ListItemType.He ader && e.Item.ItemType !=
            > ListItemType.Fo oter)
            > {
            > Button b = (Button)e.Item. Cells[1].Controls[0]; //Cells[1]: 2nd col is
            > the button column;
            > b.Attributes.Ad d("onClick", "window.open('h ttp://www.google.com' );");
            > }
            > }
            >
            > HTH.
            >
            > "Jon" <Jon@discussion s.microsoft.com > wrote in message
            > news:D3D6079B-76AD-4D85-89AF-FC624BEF6F3D@mi crosoft.com...
            > Yeah, thats what I was originally hoping for, BUT....
            > 'attributes is not a member of System.Web.UI.W ebControls.Butt onColumn'
            > 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" wrote:
            >[color=green]
            > > Hi,
            > > You can use the Attributes collection of the dynamic button as in:
            > >
            > > objbc.Attribute s.Add("onClick" , "window.open('h ttp://www.google.com' ,
            > > 'googleWin');")
            > >
            > > "Jon" <Jon@discussion s.microsoft.com > wrote in message
            > > news:8505E2E2-C195-4D2D-96AF-53D5B531CFA6@mi crosoft.com...
            > > Hello,
            > > I have a datagrid and the data in it is dynamically created at[/color][/color]
            runtime...[color=blue][color=green]
            > >
            > > For iCounter = 0 To dataset.Tables( 0).Columns.Coun t - 1
            > > Dim objbc As New BoundColumn()
            > >
            > > With objbc
            > > .DataField =
            > > dsAllPastReplie s.Tables(0).Col umns(iCounter). ColumnName
            > > .HeaderText =
            > > dsAllPastReplie s.Tables(0).Col umns(iCounter). ColumnName
            > > If .DataField =
            > > dsAllPastReplie s.Tables(0).Col umns("ReplyID") .ColumnName Then
            > > .Visible = False
            > > End If
            > > End With
            > >
            > > With datagrid1
            > > .Columns.Add(ob jbc)
            > > .DataSource = dsAllPastReplie s.Tables(0)
            > > .DataBind()
            > > End With
            > > Next
            > > dsAllPastReplie s.Clear()
            > >
            > > I can add a column of buttons at design time, but how can I fire off[/color][/color]
            some[color=blue][color=green]
            > > javascript when a button is clicked? I would ultimately like to have a[/color]
            > user[color=green]
            > > 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.
            > > --
            > > Any info will be very appreciated!
            > >
            > > Jon
            > >
            > >
            > >[/color]
            >
            >
            >[/color]


            Comment

            • Shiva

              #7
              Re: Execute Javascript From Buttons Inside Datagrid

              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.ItemCo mmand Event).

              "Jon" <Jon@discussion s.microsoft.com > wrote in message
              news:D74C7E66-F0B3-4BDC-9275-0BCF73EF7D3E@mi crosoft.com...
              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" wrote:
              [color=blue]
              > Hi,
              > This is for DataGrid's ItemDataBound event.
              >
              > Here is the VB.NET equivalent:
              > Sub MyGrid_DataBoun d(sender As Object, e As DataGridItemEve ntArgs) Handles
              > MyGrid.ItemData Bound
              > Dim b As Button
              > If (e.Item.ItemTyp e <> ListItemType.He ader AndAlso e.Item.ItemType <>
              > ListItemType.Fo oter) Then
              > b = CType (e.Item.Cells(1 ).Controls(0), Button) 'Cells[1]: 2nd col is[/color]
              the[color=blue]
              > button column
              > b.Attributes.Ad d("onClick", "window.open('h ttp://www.google.com' );")
              > End If
              > End Sub
              >
              > Hope this helps.
              >
              > "Jon" <Jon@discussion s.microsoft.com > wrote in message
              > news:7BF3A2D0-AED6-47F0-A9C2-2CCD28700903@mi crosoft.com...
              > 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[/color]
              matter)[color=blue]
              > --
              > Any info will be very appreciated!
              >
              > Jon
              >
              >
              > "Shiva" wrote:
              >[color=green]
              > > One option is to handle the ItemDataBound event of the grid and[/color]
              > dynamically[color=green]
              > > add the JS for popup window.
              > >
              > > E.g.:
              > > void MyGrid_DataBoun d(Object sender, DataGridItemEve ntArgs e)
              > > {
              > > if (e.Item.ItemTyp e != ListItemType.He ader && e.Item.ItemType !=
              > > ListItemType.Fo oter)
              > > {
              > > Button b = (Button)e.Item. Cells[1].Controls[0]; //Cells[1]: 2nd col[/color][/color]
              is[color=blue][color=green]
              > > the button column;
              > > b.Attributes.Ad d("onClick", "window.open('h ttp://www.google.com' );");
              > > }
              > > }
              > >
              > > HTH.
              > >
              > > "Jon" <Jon@discussion s.microsoft.com > wrote in message
              > > news:D3D6079B-76AD-4D85-89AF-FC624BEF6F3D@mi crosoft.com...
              > > Yeah, thats what I was originally hoping for, BUT....
              > > 'attributes is not a member of System.Web.UI.W ebControls.Butt onColumn'
              > > so you cant do it that way. Unless theres a reference or property I[/color][/color]
              might[color=blue][color=green]
              > > be missing??
              > >
              > > --
              > > Any info will be very appreciated!
              > >
              > > Jon
              > >
              > >
              > > "Shiva" wrote:
              > >[color=darkred]
              > > > Hi,
              > > > You can use the Attributes collection of the dynamic button as in:
              > > >
              > > > objbc.Attribute s.Add("onClick" , "window.open('h ttp://www.google.com' ,
              > > > 'googleWin');")
              > > >
              > > > "Jon" <Jon@discussion s.microsoft.com > wrote in message
              > > > news:8505E2E2-C195-4D2D-96AF-53D5B531CFA6@mi crosoft.com...
              > > > Hello,
              > > > I have a datagrid and the data in it is dynamically created at[/color][/color]
              > runtime...[color=green][color=darkred]
              > > >
              > > > For iCounter = 0 To dataset.Tables( 0).Columns.Coun t - 1
              > > > Dim objbc As New BoundColumn()
              > > >
              > > > With objbc
              > > > .DataField =
              > > > dsAllPastReplie s.Tables(0).Col umns(iCounter). ColumnName
              > > > .HeaderText =
              > > > dsAllPastReplie s.Tables(0).Col umns(iCounter). ColumnName
              > > > If .DataField =
              > > > dsAllPastReplie s.Tables(0).Col umns("ReplyID") .ColumnName Then
              > > > .Visible = False
              > > > End If
              > > > End With
              > > >
              > > > With datagrid1
              > > > .Columns.Add(ob jbc)
              > > > .DataSource = dsAllPastReplie s.Tables(0)
              > > > .DataBind()
              > > > End With
              > > > Next
              > > > dsAllPastReplie s.Clear()
              > > >
              > > > I can add a column of buttons at design time, but how can I fire off[/color][/color]
              > some[color=green][color=darkred]
              > > > javascript when a button is clicked? I would ultimately like to have[/color][/color][/color]
              a[color=blue][color=green]
              > > user[color=darkred]
              > > > 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[/color][/color][/color]
              the[color=blue][color=green][color=darkred]
              > > > popup, but I cant figure out how to call it from the dynamically[/color][/color][/color]
              created[color=blue][color=green][color=darkred]
              > > > buttons.
              > > > --
              > > > Any info will be very appreciated!
              > > >
              > > > Jon
              > > >
              > > >
              > > >[/color]
              > >
              > >
              > >[/color]
              >
              >
              >[/color]


              Comment

              Working...