如何通过GridView列值创建超链接?

时间:2011-11-10 15:05:53

标签: c# asp.net gridview

enter image description here

我希望用户可以通过选择heading来查看问题的答案。问题应该是在Answer.aspx页面上重定向我的超链接。我的意思是说,当用户将光标放在How to do this?上时,它应该将用户重定向到所需的页面。我怎么能这样做?

这是代码

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
       DataSourceID="SqlDataSource1" Width="100%" BorderStyle="None">
       <Columns>  
     <asp:BoundField DataField="QuestionHEAD" HeaderText="Question"
                                        SortExpression="QuestionHEAD" HeaderStyle-ForeColor="white" HeaderStyle-BackColor="Brown"/>
                                    <asp:BoundField DataField="Problem" HeaderText="Problem" 
                                        SortExpression="Problem" HeaderStyle-ForeColor="white" HeaderStyle-BackColor="Brown" />
                                    <asp:BoundField DataField="Forum" HeaderText="Forum" SortExpression="Forum" HeaderStyle-ForeColor="white" HeaderStyle-BackColor="Brown"/>
                                    <asp:BoundField DataField="Username" HeaderText="Asked By" 
                                        SortExpression="Username" HeaderStyle-ForeColor="white" HeaderStyle-BackColor="Brown" />
                                </Columns>
                            </asp:GridView>
                            <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
                                ConnectionString="<%$ ConnectionStrings:connectionstring %>" 
                                SelectCommand="SELECT [QuestionHEAD], [Problem], [Forum], [Username] FROM [Question]">
                            </asp:SqlDataSource>

2 个答案:

答案 0 :(得分:0)

您可以使用HyperLinkField

<asp:HyperLinkField DataNavigateUrlFormatString="/somepage.aspx?a={0}&b={1}" DataNavigateUrlFields="Column1, Column2" />

答案 1 :(得分:0)

尝试这样的事情

Eval("id") should be the question id and Eval("Question") should be the question field in your database


 <asp:TemplateField HeaderText="Question" HeaderStyle-HorizontalAlign="Center"  ItemStyle-HorizontalAlign="Center">
        <ItemTemplate>
            <a href="Answers.aspx?ID=<%#Eval("id")%>"> <%#Eval("Question")%></a>
        </ItemTemplate>
    </asp:TemplateField>