- Using VS 2003 datagrid
Is there a way to look at the data of a row in the ItemCreated event so I can conditionally create the javascript for a delete button in my datagrid? All of the examples I see add the onclick attribute in the ItemDataBound event but when I put it there, it never fires. I have viewed the source and the script shows up appropriatly but just never displays the dialog when I put it in the ItemDataBound event.
I have the following in the ItemCreated event:
if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item) { TableCell tc = e.Item.Cells[3]; LinkButton tLink = (LinkButton)tc.Controls[0]; tLink.Attributes.Add("onclick", _Global.BuildScript("deletegroup")); }e.Item.Cells[4] is a hidden column that contains either a 1 or 0. If the value is 1, I want to add a different script to the LinkButton.
Am I missing something obvious - I've always just had a need for a single script so this was never an issue before.
Thanks,
Greg
"Providing software solutions for your world!"
Excellent .NET Hosting here!
![]() |
0 |
![]() |
Hi, friend:
To create js in datagrid, here is an example for you:
<ItemTemplate>
<asp:TextBox runat="server" ID="tx1" onmouseover='<%# "ChangeValue(" +((GridViewRow)Container).FindControl("tx1").ClientID + ")"%>'></asp:TextBox>
</ItemTemplate>
in js:function ChangeValue( i)
{
var t=i.id
document.getElementById(t).value="AAAAAA";
}Regards
Sincerely,
Allen Chen
Microsoft Online Community Support
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
![]() |
0 |
![]() |
Thanks but I'm not quite sure what you are telling me to do. I do not have a textbox that would use a mouseover event. The column is a ButtonColumn and I am creating the javascript popup script in the ItemCommand event of the DataGrid.
Can you please elaborate further?
Thanks,
Greg
"Providing software solutions for your world!"
Excellent .NET Hosting here!
![]() |
0 |
![]() |
Hi, Greg:
ItemCommand is fired when the button is clicked, so it's a bad idea to write js in it because js doesn't work until you click it and post back to server. So, the better way is to write js in ItemCreated event:
protected void GridView1_ItemCreated(object sender, DataGridItemEventArgs e)
{Button btn;
if (e.Item.Cells.Count > 2 && e.Item.Cells[2].Controls.Count > 0)
{
btn = (Button)e.Item.Cells[2].Controls[0];
btn.Attributes.Add("onclick",
"return confirm('Hello')");
}
//Response.Write(e.Item.Cells[0].Controls[0].GetType());}
aspx:
<asp:DataGrid ID="GridView1" AllowSorting="false" AllowPaging="false" Runat="server"
DataSourceID="SqlDataSource1"
AutoGenerateColumns="False" OnItemCommand="GridView1_ItemCommand" OnItemCreated="GridView1_ItemCreated">
<Columns>
<asp:BoundColumn DataField="theID"
HeaderText="Number"></asp:BoundColumn>
<asp:BoundColumn DataField="theName"
HeaderText="Name"></asp:BoundColumn>
<asp:ButtonColumn Text="Delete" ButtonType="PushButton"
CommandName="Delete"></asp:ButtonColumn>
</Columns>
</asp:DataGrid>--%>Here, note that to check whether current control is a button since this event will fire whenever a control is created.
Regards.
Sincerely,
Allen Chen
Microsoft Online Community Support
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
![]() |
0 |
![]() |
Hi Allen,
I could very be mistaken, as I didn't very often use CSharp in 2003. However, while we know that the DataGrid was upgraded to support datasources and maintained for backward compatibility, was declarative event delegation supported (OnItemCreated) in 2003? Did you not have to explicitly wire the delegates or rely on AutoEventWireUp and appropriate method signatures?
Gregg,
The ItemDataBound should be doing the job. Can you post a very small extract of the HTML Source with the javascript declaration. The reason you would use ItemDatabound is to append or format properties, or else the potential exists that properties could be overritten if applied earlier in ItemCreated. Of course this shouldn't impact the Button.
Rgds,
Martin.
For the benefit of all users please mark any post answers as appropriate.
![]() |
0 |
![]() |
Hi Martin:
Thank you for your supplement. I just assumed that this event has been bound to this method and listed key point:)
I appreciate your suggestion to write these code in ItemDataBound instead, It seems that I didn't consider the possibility of potential overritten.
So, Greg, If you like, you can also put these code in ItemDataBound instead to avoid some potential mistakes.
Regards.
Sincerely,
Allen Chen
Microsoft Online Community Support
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
![]() |
0 |
![]() |