473,809 Members | 2,710 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

linkbutton inside datagrid not working C#

daJunkCollector
76 New Member
What I am doing here should be obvious. I have a datagrid that displays data properly. I want to create a linkbutton within each row of the datagrid that toggles a label control in the same row visible/invisible.

Please refer to the following GUI and Codebehind:

[HTML]<body>
<form id="form1" runat="server">
<cc1:ToolkitScr iptManager ID="ToolkitScri ptManager1" runat="server">
</cc1:ToolkitScri ptManager>

<asp:UpdatePane l ID="UpdatePanel 1" runat="server">
<ContentTemplat e>

<asp:DataGrid ID="dgBroken" runat="server" CellPadding="4" ForeColor="#333 333" GridLines="None " AutoGenerateCol umns="false" OnDataBound="Gr idView1_DataBou nd">
<FooterStyle BackColor="#5D7 B9D" Font-Bold="True" ForeColor="Whit e" />
<EditItemStyl e BackColor="#999 999" />
<SelectedItemSt yle BackColor="#E2D ED6" Font-Bold="True" ForeColor="#333 333" />
<PagerStyle BackColor="#284 775" ForeColor="Whit e" HorizontalAlign ="Center" />
<HeaderStyle BackColor="#5D7 B9D" Font-Bold="True" ForeColor="Whit e" />
<AlternatingIte mStyle BackColor="#EEE EEE" ForeColor="#000 000" />
<ItemStyle BackColor="Whit e" ForeColor="#000 000" />
<Columns>
<asp:BoundColum n DataField="JobB EffDte" HeaderText="Dat e Posted" DataFormatStrin g="{0:d}" />
<asp:TemplateCo lumn HeaderText="Job Title">
<ItemStyle Width="150px" />
<ItemTemplate >
<asp:LinkButt on ID="lnkExpand" runat="server" Text="+" ForeColor="#465 D7E" OnClick="lnkExp and_click">
<asp:Label ID="lblTtlDesc " runat="server" Text='<%# DataBinder.Eval (Container,"Dat aItem.TtlDesc") %>'></asp:Label>
</asp:LinkButton>
<asp:Label ID="lblTtlInfo " runat="server" Visible="false" Text='<%# DataBinder.Eval (Container,"Dat aItem.TtlInfo") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateCol umn>
<asp:BoundColum n DataField="DepD esc" HeaderText="Dep artment" />
<asp:TemplateCo lumn HeaderText="Loc ation">
<ItemStyle Width="150px" />
<ItemTemplate >
<asp:Label ID="lblLocDesc " runat="server" Text='<%# DataBinder.Eval (Container,"Dat aItem.BraCity") + ", " + DataBinder.Eval (Container,"Dat aItem.BraName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateCol umn>
</Columns>
</asp:DataGrid>



</ContentTemplate >
</asp:UpdatePanel >



</form>
</body>[/HTML]




CODE BEHIND:


Expand|Select|Wrap|Line Numbers
  1. public partial class postAPosition : System.Web.UI.Page
  2.     {
  3.         protected void Page_Load(object sender, EventArgs e)
  4.         {
  5.             BindDataGrid();
  6.             AddEventHandlers();
  7.         }
  8.  
  9.         private void AddEventHandlers()
  10.         {
  11.             dgBroken.ItemDataBound += new DataGridItemEventHandler(dgBroken_ItemDataBound);
  12.         }
  13.  
  14.         protected void dgBroken_ItemDataBound(object sender, DataGridItemEventArgs e)
  15.         {
  16.             try
  17.             {
  18.                 if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.SelectedItem)
  19.                 {
  20.                     Label lblTtlInfo = (Label)e.Item.FindControl("lblTtlInfo");
  21.                     LinkButton lnkExpand = ((LinkButton)(e.Item.FindControl("lnkExpand")));
  22.  
  23.                     if (lnkExpand.Text == "+")
  24.                     {
  25.                         lblTtlInfo.Visible = false;
  26.                     }
  27.  
  28.                     else
  29.                     {
  30.                         lblTtlInfo.Visible = true;
  31.                     }
  32.  
  33.  
  34.                 }
  35.             }
  36.             catch (Exception ex)
  37.             {
  38.                 throw ex;
  39.             }
  40.  
  41.         }
  42.  
  43.         protected void lnkExpand_click(object sender, EventArgs e)
  44.         {
  45.             LinkButton lb = (LinkButton)sender;
  46.             if (lb.Text == "+")
  47.             {
  48.                 lb.Text = "-";
  49.                 AddEventHandlers();
  50.                 BindDataGrid();
  51.             }
  52.             else if (lb.Text == "-")
  53.             {
  54.                 lb.Text = "+";
  55.                 AddEventHandlers();
  56.                 BindDataGrid();
  57.             }
  58.         }
  59.  
  60.         private void BindDataGrid()
  61.         {
  62.             DataSet dsTrial = BllJobListingsA.Select(1);
  63.  
  64.             dgBroken.DataSource = dsTrial;
  65.             dgBroken.DataBind();
  66.         }
  67.     }
  68. }
Jan 24 '08 #1
5 7379
nateraaaa
663 Recognized Expert Contributor
Are you using a datagrid control (.NET 1.1) or a gridview control (.NET 2.0 or higher) to display your data? The code you posted seemed to mix references to both controls. If you are using a datagrid control you could add a CommandName to the LinkButton and in the _ItemCommand event do the following:

public void datagrid1_ItemC ommand(object source, System.Web.UI.W ebControls.Data GridCommandEven tArgs e)
{
if(e.Item.ItemT ype != ListItemType.He ader && e.Item.ItemType != ListItemType.Pa ger)
{
if(e.CommandNam e == "Expand")
{
LinkButton lnkExpand = ((LinkButton)(e .Item.FindContr ol("lnkExpand") ));
if (lnkExpand .Text == "+")
{
lnkExpand .Text = "-";
AddEventHandler s();
BindDataGrid();
}
else if lnkExpand.Text == "-")
{
lnkExpand.Text = "+";
AddEventHandler s();
BindDataGrid();
}
}

}
}

If you are using gridview you will need to check the CommandName in the RowCommand event of your gridview. Use the bolded code above in the RowCommand event.

I have not tested this but have written code like this many times. If you run into problems let me know.

Nathan
Jan 24 '08 #2
daJunkCollector
76 New Member
Hey Nate,

Thanks for your assistence. Unfortunately, I am still stuck. I am glad to see you understand exactly what I am trying to accomplish. I have been trying alterations of your fix, but to no avail.

When I click the LinkButton at runtime there is no reaction. Its sad. I will repaste my new codebehind:

Expand|Select|Wrap|Line Numbers
  1. namespace USGWEBGUI.employee
  2. {
  3.     public partial class postAPosition : System.Web.UI.Page
  4.     {
  5.         protected void Page_Load(object sender, EventArgs e)
  6.         {
  7.             BindDataGrid();
  8.             AddEventHandlers();
  9.         }
  10.  
  11.         private void AddEventHandlers()
  12.         {
  13.             dgBroken.ItemDataBound += new DataGridItemEventHandler(dgBroken_ItemDataBound);
  14.             dgBroken.ItemCommand += new DataGridCommandEventHandler(dgBroken_ItemCommand);
  15.         }
  16.  
  17.  
  18.         protected void dgBroken_ItemCommand(object sender, DataGridCommandEventArgs e)
  19.         {
  20.  
  21.             if (e.Item.ItemType != ListItemType.Header && e.Item.ItemType != ListItemType.Pager)
  22.             {
  23.                 if (e.CommandName == "Expand")
  24.                 {
  25.                     LinkButton lnkExpand = ((LinkButton)(e.Item.FindControl("lnkExpand")));
  26.                     Label lblTtlInfo = ((Label)(e.Item.FindControl("lblTtlInfo")));
  27.  
  28.                     if (lnkExpand.Text == "+")
  29.                     {
  30.                         lnkExpand.Text = "-";
  31.  
  32.                         AddEventHandlers();
  33.                         BindDataGrid();
  34.                     }
  35.                     else if (lnkExpand.Text == "-")
  36.                     {
  37.                         lnkExpand.Text = "+";
  38.  
  39.                         AddEventHandlers();
  40.                         BindDataGrid();
  41.                     }
  42.                 }
  43.             }
  44.         }
  45.         protected void dgBroken_ItemDataBound(object sender, DataGridItemEventArgs e)
  46.         {
  47.             try
  48.             {
  49.                 if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.SelectedItem)
  50.                 {
  51.                     Label lblTtlInfo = (Label)e.Item.FindControl("lblTtlInfo");
  52.                     LinkButton lnkExpand = (LinkButton)e.Item.FindControl("lnkExpand");
  53.  
  54.                     if (lnkExpand.Text == "+")
  55.                     {
  56.                         lblTtlInfo.Visible = false;
  57.                     }
  58.  
  59.                     else
  60.                     {
  61.                         lblTtlInfo.Visible = true;
  62.                     }
  63.  
  64.  
  65.                 }
  66.             }
  67.             catch (Exception ex)
  68.             {
  69.                 throw ex;
  70.             }
  71.  
  72.         }
  73.  
  74.  
  75.  
  76.         private void BindDataGrid()
  77.         {
  78.             DataSet dsTrial = BllJobListingsA.Select(1);
  79.  
  80.             dgBroken.DataSource = dsTrial;
  81.             dgBroken.DataBind();
  82.         }
  83.     }
  84. }
Jan 25 '08 #3
daJunkCollector
76 New Member
Nate,

Furthermore, I am using .net 2.0. You are very observant. The reason my code may resonate 1.1 is because the other side of this solution is in 1.1. The old school solution is used to profile the database tables that are then being displayed in this 2.0 solution's datagrid.

If you suggest I start this datagrid from scratch and use a gridview or something along those lines I'm all ears.

Again, my ultimate goal here is to display my SQL Select in a table-like control and I want to toggle the visibility of a label via a +/- link button.

The overall effect should be an expandible/collapsible datagrid.

Thanks again,
Pete
Jan 25 '08 #4
nateraaaa
663 Recognized Expert Contributor
Nate,

Furthermore, I am using .net 2.0. You are very observant. The reason my code may resonate 1.1 is because the other side of this solution is in 1.1. The old school solution is used to profile the database tables that are then being displayed in this 2.0 solution's datagrid.

If you suggest I start this datagrid from scratch and use a gridview or something along those lines I'm all ears.

Again, my ultimate goal here is to display my SQL Select in a table-like control and I want to toggle the visibility of a label via a +/- link button.

The overall effect should be an expandible/collapsible datagrid.

Thanks again,
Pete
I would recommend using the gridview since you are using .NET 2.0. I don't know how .NET 2.0 handles the datagrid control. You may be having compatability issues in the event handlers of your datagrid control. In addition the gridview should make your life much easier because of the new template fields. I have not created an expandable/collaspable gridview before but I think that using the gridview instead of the datagrid control will be your best bet. Let me know how it goes or if you get stuck on something.

Nathan
Jan 25 '08 #5
daJunkCollector
76 New Member
Ok Nate. Thanks for the advise. I actually got it going completely on the client side. The following code works really nice. The only problem now is that I want the .text property of lblExpand to change from "+" to "-" depending on whether the row is expanded or not. I will be trying to f with this for the rest of the day.

Here is the code that work for the curious. If you have any suggestion on how to manipulate the +/- sign please post. Thanks.

[HTML]<head runat="server">
<title>Availabl e Job Positions</title>
<link href="../DesignFiles/css/StyleSheet.css" rel="stylesheet " type="text/css">
<script language="JavaS cript">
function collapseExpand( somePanel)
{

// Check if expand or collapse is required
if (somePanel.styl e.display == "none")
{
somePanel.style .display = "";
}
else
{
somePanel.style .display = "none";
}
}



</script>
</head>
<body>
<form id="form1" runat="server">
<cc1:ToolkitScr iptManager ID="ToolkitScri ptManager1" runat="server">
</cc1:ToolkitScri ptManager>

<asp:UpdatePane l ID="UpdatePanel 1" runat="server">
<ContentTemplat e>

<asp:DataGrid ID="dgBroken" runat="server" CellPadding="4" ForeColor="#333 333" GridLines="None " AutoGenerateCol umns="false">
<FooterStyle BackColor="#5D7 B9D" Font-Bold="True" ForeColor="Whit e" />
<EditItemStyl e BackColor="#999 999" />
<SelectedItemSt yle BackColor="#E2D ED6" Font-Bold="True" ForeColor="#333 333" />
<PagerStyle BackColor="#284 775" ForeColor="Whit e" HorizontalAlign ="Center" />
<HeaderStyle BackColor="#5D7 B9D" Font-Bold="True" ForeColor="Whit e" />
<AlternatingIte mStyle BackColor="#EEE EEE" ForeColor="#000 000" />
<ItemStyle BackColor="Whit e" ForeColor="#000 000" />
<Columns>
<asp:BoundColum n DataField="JobB EffDte" HeaderText="Dat e Posted" DataFormatStrin g="{0:d}" />
<asp:TemplateCo lumn HeaderText="Job Title">
<ItemStyle Width="300px" />

<ItemTemplate >
<table>
<tr>
<td align="left" style="width:12 px">
<a href="javascrip t:collapseExpan d(document.getE lementById('spa nTitleInfo<%# DataBinder.Eval (Container,"Dat aItem.JobBId") %>'))" ><asp:Label ID="lblExpand" runat="server" ForeColor="#465 D7E" Font-Underline="fals e" Text="+"></asp:Label></a>
</td>
<td style="width:4p x">
</td>
<td align="left" style="width:28 4px">
<a href="javascrip t:collapseExpan d(document.getE lementById('spa nTitleInfo<%# DataBinder.Eval (Container,"Dat aItem.JobBId") %>'))" >
<asp:Label ID="lblTtlDesc " runat="server" ForeColor="#465 D7E" Font-Underline="true " Text='<%# DataBinder.Eval (Container,"Dat aItem.TtlDesc") %>'></asp:Label></a>
</td>
</tr>
<tr style="width:30 0px">
<td>
</td>
<td style="width:4p x">
</td>
<td align="left">
<span id='spanTitleIn fo<%# DataBinder.Eval (Container,"Dat aItem.JobBId") %>' style="display: none;"><%# DataBinder.Eval (Container, "DataItem.TtlIn fo")%></span>
</td>

</tr>
</table>


</ItemTemplate>

</asp:TemplateCol umn>
<asp:BoundColum n DataField="DepD esc" HeaderText="Dep artment" />
<asp:TemplateCo lumn HeaderText="Loc ation">
<ItemStyle Width="150px" />
<ItemTemplate >
<asp:Label ID="lblLocDesc " runat="server" Text='<%# DataBinder.Eval (Container,"Dat aItem.BraCity") + ", " + DataBinder.Eval (Container,"Dat aItem.BraName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateCol umn>
</Columns>
</asp:DataGrid>


</ContentTemplate >
</asp:UpdatePanel >



</form>
</body>
</html>[/HTML]
Jan 25 '08 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

0
1762
by: Dude | last post by:
I add a link button to the header of my datagrid as follows: If e.Item.ItemType = ListItemType.Header Then Dim myLink As New LinkButton() myLink.Text = "Add New" myLink.Attributes.Add("runat", "server") myLink.Attributes.Add("id", "btnAddNew") myLink.Attributes.Add("CommandName", "btnAddNew") myLink.Attributes.Add("CommandArgument", "btnAddNew") e.Item.Cells(2).Controls.Add(myLink) AddHandler myLink.Click, AddressOf Me.btnAddNew_Click
1
1904
by: Dude | last post by:
I add a link button to the header of my datagrid as follows: If e.Item.ItemType = ListItemType.Header Then Dim myLink As New LinkButton() myLink.Text = "Add New" myLink.Attributes.Add("runat", "server") myLink.Attributes.Add("id", "btnAddNew") myLink.Attributes.Add("CommandName", "btnAddNew") myLink.Attributes.Add("CommandArgument", "btnAddNew") e.Item.Cells(2).Controls.Add(myLink) AddHandler myLink.Click, AddressOf Me.btnAddNew_Click
1
1096
by: Eamon | last post by:
In the code-behind page I am building a table programmatically so that I can do some on-the-fly manipulation. I had found no way to use a datagrid with the solution I was seeking. is it possible to use a linkbutton in this scenario, or is the linkbutton limited to the datagrid? If it is possible, could an example be posted or pointed to? Warm Regards, kc
1
1493
by: Michael | last post by:
How to trap click event of LinkButton inside a Repeater? I'm using C#. Thanks.
1
1619
by: Danny Ni | last post by:
Hi, I have a LinkButton inside a datagrid, when this LinkButton is clicked, the program deleete a record in database. I would like to add a confirmation on client side. I know I can do Attributes.Add("onclick",...) to Buttons not in datagrid. But how do I do it for LinkButton in datagrid? TIA
1
3826
by: Chumley Walrus | last post by:
I am trying to have the contents inside a datagrid cell line up properly, either center, align right or align left. But using something like HorizontalAlign="right" is illegal syntax for boundcolumns. Is there something I can use to format alignment inside datagrid cells? thanx chumley
1
2173
by: smash2004 | last post by:
I have VS 2005 CTP. I created a page with one repeater. Inside i put a linkbutton. Usually when i put linkbutton on a page I doubleclick it in designview and event gets created in codebehind file. Now i can't click on anything because my linkbutton is not visible in the designer. So i moved my linkbutton outside the repeater and doubleclicked it and code gets created. With code created I moved my linkbutton again inside
1
6102
by: settyv | last post by:
Hi, I need to open PDF document in new window when i click on linkbutton in datagrid.For that i have written code as below: But the problem with this code is that it opens the new window ,but not loading the pdf.Please let me know how can i solve this issue. private void grdTest_ItemCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
0
1108
by: Syed81 | last post by:
Hi All, I have used one textbox inside datagrid's itemtemplate.During its textchanged event i have did some calculations in code behind using add handler.I have set autopostback=true.But its textchanged event is not firing when i move to the next cell.But for another textbox in the same row it is firing. Anybody plz give me the solution Regards Syed
0
9721
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10640
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10376
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10387
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10120
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9200
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7662
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6881
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
2
3861
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.