Reputation: 39
I was trying to get the value in the label of the first column when clicking on the linkbutton in the second column, so i added onselectedindexchanged function and added some code to it but its not firing at all, i tried multiple methods and functions so it would work but nothing happened
<asp:GridView ID="ProjectGridView" runat="server" ShowHeaderWhenEmpty="true" OnSelectedIndexChanged="ProjectGridView_SelectedIndexChanged" AutoGenerateColumns="False" Visible="true" CellPadding="4" ForeColor="#333333" GridLines="None" ShowFooter="true">
<AlternatingRowStyle BackColor="White" Height="20px" />
<RowStyle Height="20px" />
<Columns>
<asp:TemplateField Visible="false">
<ItemTemplate>
<asp:Label Visible="false" ID="lblProjectID" runat="server" Text=' <%# Eval("Project_ID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Project Number">
<ItemTemplate>
<asp:LinkButton ID="LinkProjectNumber" runat="server" Text=' <%# Eval("Project_Number") %>'></asp:LinkButton>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="ProjectNumberTextBox" runat="server" />
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Project Name">
<ItemTemplate>
<%# Eval("Project_Name") %>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="ProjectNameTextBox" runat="server" />
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Location">
<ItemTemplate>
<%# Eval("Project_Location") %>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="ProjectLocationTextBox" runat="server"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Description">
<ItemTemplate>
<%# Eval("Project_Description") %>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="ProjectDescriptionTextBox" runat="server"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField>
<FooterTemplate>
<asp:Button ID="btnAdd" runat="server" Text="Add" OnClick="Add" CommandName="Footer" />
</FooterTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#507CD1" HorizontalAlign="Center" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#2784FC" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#EFF3FB" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#F5F7FB" />
<SortedAscendingHeaderStyle BackColor="#6D95E1" />
<SortedDescendingCellStyle BackColor="#E9EBEF" />
<SortedDescendingHeaderStyle BackColor="#4870BE" />
</asp:GridView>
css code
protected void ProjectGridView_SelectedIndexChanged(object sender, EventArgs e)
{
string projectid = (ProjectGridView.SelectedRow.FindControl("LinkProjectID") as Label).Text;
PopUpMessage(projectid);
}
Upvotes: 1
Views: 1473
Reputation: 1001
You'll have to do that with the GridView's RowCommand
event:
In your ASPX, add the CommandName
attribute to your LinkButton
:
<asp:LinkButton ID="LinkProjectNumber" runat="server" Text=' <%# Eval("Project_Number") %>' CommandName='GetData'></asp:LinkButton>
In your cs
code behind:
protected void ProjectGridView_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "GetData")
{
GridViewRow row = (GridViewRow)(((Button)e.CommandSource).NamingContainer);
Label myLabel = (Label)row.FindControl("LinkProjectID");
PopUpMessage(myLabel.Text);
}
}
Reference: https://stackoverflow.com/a/14255021/1821637
Upvotes: 0
Reputation: 308
Two things
You cannot set the OnSelectedIndexChanged="ProjectGridView_SelectedIndexChanged"
on the gridview and use a button to control it. See an asp linkbutton does not change the selected index on the gridview (the OnSelectedIndexChanged
method is best applied to dropdowns.
Just use a button click event, because you could access the link button's text this way
Front side code
<asp:GridView ID="ProjectGridView" runat="server" ShowHeaderWhenEmpty="true" AutoGenerateColumns="False" Visible="true" CellPadding="4" ForeColor="#333333" GridLines="None" ShowFooter="true">
<AlternatingRowStyle BackColor="White" Height="20px" />
<RowStyle Height="20px" />
<Columns>
<asp:TemplateField Visible="false">
<ItemTemplate>
<asp:Label Visible="false" ID="lblProjectID" runat="server" Text=' <%# Eval("Project_ID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Project Number">
<ItemTemplate>
<asp:LinkButton ID="LinkProjectNumber" runat="server" OnClick="LinkProjectNumber_Click" Text=' <%# Eval("Project_Number") %>'></asp:LinkButton>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="ProjectNumberTextBox" runat="server" />
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Project Name">
<ItemTemplate>
<%# Eval("Project_Name") %>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="ProjectNameTextBox" runat="server" />
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Location">
<ItemTemplate>
<%# Eval("Project_Location") %>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="ProjectLocationTextBox" runat="server"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Description">
<ItemTemplate>
<%# Eval("Project_Description") %>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="ProjectDescriptionTextBox" runat="server"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField>
<FooterTemplate>
<asp:Button ID="btnAdd" runat="server" Text="Add" OnClick="Add" CommandName="Footer" />
</FooterTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#507CD1" HorizontalAlign="Center" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#2784FC" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#EFF3FB" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#F5F7FB" />
<SortedAscendingHeaderStyle BackColor="#6D95E1" />
<SortedDescendingCellStyle BackColor="#E9EBEF" />
<SortedDescendingHeaderStyle BackColor="#4870BE" />
</asp:GridView>
Server side code
protected void LinkProjectNumber_Click(object sender, EventArgs e)
{
string projectid = (sender as LinkButton).Text;
PopUpMessage(projectid);
}
That should take of your problem. Oh as a side note I also noticed you are casting you Link Button ID as a Label (which would cause issues later on).
EDIT: I noticed you are trying to get the Project ID so I have the method below that will do that for you (this should work if you run into any Object Reference errors than let me know and I'll do my best to help, but I ran this code and it works fine)
protected void LinkProjectNumber_Click(object sender, EventArgs e)
{
string projectid = ((sender as LinkButton).NamingContainer.FindControl("lblProjectID") as Label).Text;
PopUpMessage(projectid);
}
Upvotes: 1
Reputation: 833
Your event should be GridViewSelectEventArgs
. like
protected void ProjectGridView_SelectedIndexChanged(object sender, GridViewSelectEventArgs e)
{
string projectid = (ProjectGridView.SelectedRow.FindControl("LinkProjectID") as Label).Text;
PopUpMessage(projectid);
}
Upvotes: 0