1

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);
}
ahmad
  • 39
  • 7

3 Answers3

1

Two things

  1. 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.

  2. 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);
}
M Goward
  • 308
  • 1
  • 12
  • in this case its working fine but its getting the value of the linkbutton but as i was trying to do is to get the value of the first column which is the lblProjectID – ahmad Jul 19 '18 at 15:54
  • yep as you suggested it went into object reference error "Additional information: Object reference not set to an instance of an object.",thanks in advance – ahmad Jul 19 '18 at 15:59
0

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);
 }
Nagib Mahfuz
  • 833
  • 10
  • 19
  • i added it but an error occurs No overload for 'ProjectGridView_SelectedIndexChanged' matches delegate 'System.EventHandler', btw this function is created automatically when you define the function selectedindexchanged in gridview, thanks in advance – ahmad Jul 19 '18 at 15:45
  • @ahmad see my answer – M Goward Jul 19 '18 at 15:49
0

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

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Máster
  • 981
  • 11
  • 23