如何从ASP.NET C中的GridView获取单击的超链接行相邻单元格值#

时间:2014-12-03 21:12:53

标签: c# asp.net gridview hyperlink

我正在使用C#ASP.NET中的一个项目。我在webform页面中有一个GridView,它从数据库中获取数据。它有三列 Checkbox DeptId DeptName DeptName 位于hyperlink字段中我需要当用户点击 deptName 超链接时, DeptId 必须保存为字符串并传递给其他class作为所选部门的参数。我很困惑怎么做!!请帮帮我

我这样做了但没有帮助

 protected void DepartmentTable_SelectedIndexChanged(object sender, EventArgs e)
 {
      string name = DepartmentTable.SelectedRow.Cells[0].Text.ToString();
      Departmentlist obj = new Departmentlist();
      obj.setDeptid(name);
 }

HTML MARKUP CODE

<asp:GridView ID="DepartmentTable" runat="server" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" BackColor="#CCCCCC" BorderColor="#999999" BorderStyle="Solid" BorderWidth="3px" CellPadding="4" DataSourceID="SqlDataSource1" Width="814px" CellSpacing="2" ForeColor="Black" DataKeyNames="DeptId" OnSelectedIndexChanged="DepartmentTable_SelectedIndexChanged">
                <Columns>
                    <asp:TemplateField>
                        <ItemTemplate>
                            <asp:CheckBox ID="CheckBox1" runat="server" />
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:BoundField DataField="DeptId" HeaderText="DeptId" SortExpression="DeptId" ReadOnly="True" InsertVisible="False" />
                    <asp:TemplateField HeaderText="DeptName">
                        <ItemTemplate>
                            <asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%# Eval("DeptId", "Departmentlist.aspx?DeptId={0}") %>' Text='<%# Eval("DeptName") %>'></asp:HyperLink>
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
                <FooterStyle BackColor="#CCCCCC" />
                <HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" />
                <PagerStyle ForeColor="Black" HorizontalAlign="Left" BackColor="#CCCCCC" />
                <RowStyle BackColor="White" />
                <SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />
                <SortedAscendingCellStyle BackColor="#F1F1F1" />
                <SortedAscendingHeaderStyle BackColor="Gray" />
                <SortedDescendingCellStyle BackColor="#CAC9C9" />
                <SortedDescendingHeaderStyle BackColor="#383838" />
            </asp:GridView>

1 个答案:

答案 0 :(得分:0)

您需要将控件转换为超链接并获取超链接控件的文本。类似的东西:

string name =((HyperLink)Department Table.Selected Row.Cells [0] .Controls [0])。Text;