[英]How to get clicked hyperlink row adjacent cell value from GridView in ASP.NET C#
I'm working with one of my projects in C# ASP.NET. 我正在使用C#ASP.NET中的一个项目。 I have a
GridView
in webform page which is fetching data from database. 我在Webform页面中有一个
GridView
,它正在从数据库中获取数据。 it has three columns Checkbox , DeptId and DeptName . 它具有Checkbox , DeptId和DeptName三列。 DeptName is in
hyperlink
field i need that when user clicks deptName hyperlink its DeptId must be saved to a string and passed to other class as a parameter of selected department.i am confused how to do that !! DeptName在
hyperlink
字段中,我需要当用户单击deptName超链接时,它的DeptId必须保存为字符串并作为选定部门的参数传递给其他类。我很困惑该怎么做! Please Help me 请帮我
I did this but it didn't helped 我这样做了,但是没有帮助
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 HTML标记代码
<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>
You need to cast the control as a hyperlink and get thje text of the hyperlink control. 您需要将控件强制转换为超链接,并获取超链接控件的文本。 Something like:
就像是:
string name = ((HyperLink)DepartmentTable.SelectedRow.Cells[0].Controls[0]).Text; 字符串名称=((HyperLink)DepartmentTable.SelectedRow.Cells [0] .Controls [0])。Text;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.