Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Adding custom client script events 2

Status
Not open for further replies.

Joulius

Programmer
Dec 16, 2003
215
RO
I've been playing lately with DataGrid binding and i wanted to make a button that will open another page and display me something based on an ID (data from database).

The main problem was that i've used
Code:
<asp:ButtonColumn ButtonType=&quot;LinkButton&quot;...>
and i searched to add a custom client side click event without using server side event ItemCommand of the datagrid object, with no luck. There was no method of adding and client event handler even if i've used RegisterClientScriptBlock, this worked with only the asp controls that had the onclick event in the attributes list.

I've found another example of using Attributes.Add method of the objects but as i expected i couldnt get to the LinkButton i wanted. Therefor i've tryed my luck with something else and as i suspected it WORKED!

I hope that this little trick can help anybody else in solving most of their problems. This works even with dynamic created elements.

I'll post here some of my working code.
Code:
on the aspx file this was the boundcolumn from the datagrid

<asp:ButtonColumn HeaderText=&quot;Del&quot; Text=&quot;X&quot; ButtonType=&quot;LinkButton&quot; CommandName=&quot;delete&quot;></asp:ButtonColumn>

on the server side i will show the old and the new method used.
Old method

Private Sub dataGridObject_Click(ByVal sender As Object, ByVal e As DataGridCommandEventArgs) Handles dataGridObject.ItemCommand
Select Case e.CommandName
            Case &quot;download&quot;
             'myold code which opened an window by using response.write, looking not too good
End Select
End sub

New method

Private Sub dataGridObject_TestSub(ByVal sender As Object, ByVal e As DataGridItemEventArgs) Handles dataGridObject.ItemDataBound
e.Item.Cells(7).Attributes.Add(&quot;onclick&quot;, &quot;alert(&quot; & e.Item.Cells(5).Text & &quot;);return false;&quot;)
End Sub

This solved all my problems.
If i look at the html code generated i'll see this <TD onclick=&quot;alert(100);return false;&quot;>(it actualy works there) which works and override my default handler created by the server due to the LinkButton. This is what i've wanted all along.
I've used ItemDataBound event cuz i wanted to change the way how the datagrid handles its elements binding. I've placed the ID's returned from database.
I've placed return false at the end of the script code cuz i wanted to cance the refresh of the page(to postback).

This worked on IE, i hope it will work on others script enabled browsers also.

Hope this helps someone in the future.
Good luck and post here your oppinions.
 
Just a little addition: when you need to get a reference to a primary key while executing a datagrid command, it's useful to use the DataKeyField property of DataGrid to bind the key column to. This way we avoid having a hidden column with a Label or other control in the grid to store the value in order to retrieve it later in the code behind.
Code:
<asp:datagrid ID=&quot;myGrid&quot; DataKeyField=&quot;CustomerId&quot;>
..........
private void myGrid_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
 // get key bound to the clicked row
 int customerId = Convert.ToInt32(myGrid.DataKeys[e.Item.ItemIndex]);
 e.Item.Cells[7].Attributes.Add(&quot;onclick&quot;, &quot;alert(&quot; + customerId.ToString() + &quot;);return false;&quot;);
}
 
Are you a mind reader?
:))
 
Are you a mind reader? :))
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top