473,396 Members | 1,891 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Inserting a contextual JS function in a DataGrid LinkButton

I hava a DataGrid with a bunch of columns. The first column ([0]) is a
Username and the last column is set up like this:

<asp:TemplateColumn>
<ItemTemplate>
<asp:LinkButton ID="ProfileLBtn" runat="server"
CausesValidation="false" CommandName="Profile"
Text="Profile" />
</ItemTemplate>
</asp:TemplateColumn>

I don't want to actually use this last column to execute a Server-Side
function post-back at all. Instead, I want it to make a client-side
JavaScript call USING A VALUE FROM THE FIRST COLUMN FOR EACH ROW IN MY
DATAGRID.

I've tried doing this as follows...

protected void UsersDG_ItemCreated(object sender, DataGridItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item ||
e.Item.ItemType == ListItemType.AlternatingItem)
{
string Username = e.Item.Cells[0].Text;
LinkButton LBtn = e.Item.FindControl("ProfileLBtn") as LinkButton;
LBtn.OnClientClick = "PopUserProfile('" + Username + "');
return(false);";
}
}

So everything about this works EXCEPT that the value of Username is always
EMPTY. Is this because during execution of "UsersDG_ItemCreated", the value
hasn't yet been populated?

Alex
Jun 18 '06 #1
2 1029
Try using the ItemDataBound event instead of ItemCreated. The item should
definitely be there in that handler.
Otherwise, it could be possible that e.Item.Cells[0] isn't the actual
control that is holding the value, e.g., it might be
e.ItemCells[0].Controls[0].Text, for example.
Peter

--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com


"Alex Maghen" wrote:
I hava a DataGrid with a bunch of columns. The first column ([0]) is a
Username and the last column is set up like this:

<asp:TemplateColumn>
<ItemTemplate>
<asp:LinkButton ID="ProfileLBtn" runat="server"
CausesValidation="false" CommandName="Profile"
Text="Profile" />
</ItemTemplate>
</asp:TemplateColumn>

I don't want to actually use this last column to execute a Server-Side
function post-back at all. Instead, I want it to make a client-side
JavaScript call USING A VALUE FROM THE FIRST COLUMN FOR EACH ROW IN MY
DATAGRID.

I've tried doing this as follows...

protected void UsersDG_ItemCreated(object sender, DataGridItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item ||
e.Item.ItemType == ListItemType.AlternatingItem)
{
string Username = e.Item.Cells[0].Text;
LinkButton LBtn = e.Item.FindControl("ProfileLBtn") as LinkButton;
LBtn.OnClientClick = "PopUserProfile('" + Username + "');
return(false);";
}
}

So everything about this works EXCEPT that the value of Username is always
EMPTY. Is this because during execution of "UsersDG_ItemCreated", the value
hasn't yet been populated?

Alex

Jun 18 '06 #2
Thanks Peter for your input.

Hi Alex,

I agree with Peter on this that you should use the ItemDataBound event
instead of ItemCreated. Besides, you can use the event's parameter
DataGridItemEventArgs.Item.DataItem to access the row data rather than
reading the cell's text. For example, if you are binding the DataGrid to a
DataSource, use following sample code to read the other fields:

protected void DataGrid1_ItemDataBound(object sender,
DataGridItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType ==
ListItemType.AlternatingItem)
{
DataRowView v = (DataRowView) e.Item.DataItem;
object id = v["ProductID"];
}
}

Hope this helps. Please feel free to post here if anything is unclear.

Regards,
Walter Wang
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.

Jun 19 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Fresh Air Rider | last post by:
Hi Could anyone please give me the syntax for calling a function with more than one parameter from an <asp:LinkButton> ? Many thanks John
1
by: Webgour | last post by:
Hi, I'm tring to add a column to a datagrid with a linkbutton as header that can be used to sort the column. The column and the linkbutton are added programmatically (see below). However the...
0
by: sameer mowade via .NET 247 | last post by:
Hello All, I have problem while dynamically removing row from the Datagrid which i have added dynamically as shown in the following code snippet. The problem is that while removing dynamically...
2
by: Fluxray | last post by:
--Background: I have a webform including a datagrid. The datagrid is using template. Its ItemTemplate is used to display a look-up-table with labels. its EditItemTemplate is used to edit a row in...
9
by: A P | last post by:
Hi! I have created a sample datagrid that can update data on a database. Please help me solve the problem, I have attached both aspx and code behind (aspx.vb): ______________________ ...
5
by: Tina | last post by:
the Edit, Update, Cancel, and Delete buttons in my datagrid are causing validation elsewhere on the page. I want to specify that these buttons should not cause validation but they have no design...
4
by: Frank | last post by:
Hello All, I ham using VS.NET 2003. Have followed instructions from http://gridviewguy.com/ArticleDetails.aspx?articleID=26 along with several other articles to no avail. I am pulling my hair...
2
by: Charleees | last post by:
Hi all, I have a DataGrid with Template Columns..... There are LAbels,Linkbuttons in the Single Row.. I have to set the Constant Column width for those Template Columns in Grid... Wat...
2
by: tshad | last post by:
I am having a problem with my Datagrid that displays my Files via GetFiles() If I use a HyperLinkColumn - I can use the column name "Name" which is what the result from GetFiles(). ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.