473,396 Members | 1,945 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.

linkbutton inside datagrid not working C#

daJunkCollector
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:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server">
</cc1:ToolkitScriptManager>

<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>

<asp:DataGrid ID="dgBroken" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None" AutoGenerateColumns="false" OnDataBound="GridView1_DataBound">
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<EditItemStyle BackColor="#999999" />
<SelectedItemStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<AlternatingItemStyle BackColor="#EEEEEE" ForeColor="#000000" />
<ItemStyle BackColor="White" ForeColor="#000000" />
<Columns>
<asp:BoundColumn DataField="JobBEffDte" HeaderText="Date Posted" DataFormatString="{0:d}" />
<asp:TemplateColumn HeaderText="Job Title">
<ItemStyle Width="150px" />
<ItemTemplate>
<asp:LinkButton ID="lnkExpand" runat="server" Text="+" ForeColor="#465D7E" OnClick="lnkExpand_click">
<asp:Label ID="lblTtlDesc" runat="server" Text='<%# DataBinder.Eval(Container,"DataItem.TtlDesc") %>'></asp:Label>
</asp:LinkButton>
<asp:Label ID="lblTtlInfo" runat="server" Visible="false" Text='<%# DataBinder.Eval(Container,"DataItem.TtlInfo") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateColumn>
<asp:BoundColumn DataField="DepDesc" HeaderText="Department" />
<asp:TemplateColumn HeaderText="Location">
<ItemStyle Width="150px" />
<ItemTemplate>
<asp:Label ID="lblLocDesc" runat="server" Text='<%# DataBinder.Eval(Container,"DataItem.BraCity") + ", " + DataBinder.Eval(Container,"DataItem.BraName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateColumn>
</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 7359
nateraaaa
663 Expert 512MB
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_ItemCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
if(e.Item.ItemType != ListItemType.Header && e.Item.ItemType != ListItemType.Pager)
{
if(e.CommandName == "Expand")
{
LinkButton lnkExpand = ((LinkButton)(e.Item.FindControl("lnkExpand")));
if (lnkExpand .Text == "+")
{
lnkExpand .Text = "-";
AddEventHandlers();
BindDataGrid();
}
else if lnkExpand.Text == "-")
{
lnkExpand.Text = "+";
AddEventHandlers();
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
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
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 Expert 512MB
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
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>Available Job Positions</title>
<link href="../DesignFiles/css/StyleSheet.css" rel="stylesheet" type="text/css">
<script language="JavaScript">
function collapseExpand(somePanel)
{

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



</script>
</head>
<body>
<form id="form1" runat="server">
<cc1:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server">
</cc1:ToolkitScriptManager>

<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>

<asp:DataGrid ID="dgBroken" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None" AutoGenerateColumns="false">
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<EditItemStyle BackColor="#999999" />
<SelectedItemStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<AlternatingItemStyle BackColor="#EEEEEE" ForeColor="#000000" />
<ItemStyle BackColor="White" ForeColor="#000000" />
<Columns>
<asp:BoundColumn DataField="JobBEffDte" HeaderText="Date Posted" DataFormatString="{0:d}" />
<asp:TemplateColumn HeaderText="Job Title">
<ItemStyle Width="300px" />

<ItemTemplate>
<table>
<tr>
<td align="left" style="width:12px">
<a href="javascript:collapseExpand(document.getElemen tById('spanTitleInfo<%# DataBinder.Eval(Container,"DataItem.JobBId") %>'))" ><asp:Label ID="lblExpand" runat="server" ForeColor="#465D7E" Font-Underline="false" Text="+"></asp:Label></a>
</td>
<td style="width:4px">
</td>
<td align="left" style="width:284px">
<a href="javascript:collapseExpand(document.getElemen tById('spanTitleInfo<%# DataBinder.Eval(Container,"DataItem.JobBId") %>'))" >
<asp:Label ID="lblTtlDesc" runat="server" ForeColor="#465D7E" Font-Underline="true" Text='<%# DataBinder.Eval(Container,"DataItem.TtlDesc") %>'></asp:Label></a>
</td>
</tr>
<tr style="width:300px">
<td>
</td>
<td style="width:4px">
</td>
<td align="left">
<span id='spanTitleInfo<%# DataBinder.Eval(Container,"DataItem.JobBId") %>' style="display:none;"><%# DataBinder.Eval(Container, "DataItem.TtlInfo")%></span>
</td>

</tr>
</table>


</ItemTemplate>

</asp:TemplateColumn>
<asp:BoundColumn DataField="DepDesc" HeaderText="Department" />
<asp:TemplateColumn HeaderText="Location">
<ItemStyle Width="150px" />
<ItemTemplate>
<asp:Label ID="lblLocDesc" runat="server" Text='<%# DataBinder.Eval(Container,"DataItem.BraCity") + ", " + DataBinder.Eval(Container,"DataItem.BraName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateColumn>
</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
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",...
1
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",...
1
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...
1
by: Michael | last post by:
How to trap click event of LinkButton inside a Repeater? I'm using C#. Thanks.
1
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...
1
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...
1
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...
1
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...
0
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...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
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
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...
0
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,...

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.