使用c#在asp.net的gridview中单击该行的超链接字段时检索行的值

时间:2015-05-06 15:55:02

标签: c# asp.net gridview hyperlink

我正在制作一个旅行和旅游网站。但是我遇到了问题。我有一个DropDownList,下面是GridView。只要我在DropDownList中选择了一个值,GridView中的相应值就会显示出来。我已在代码隐藏文件中将GridView的列转换为HyperLink。每个HyperLink导航到不同的网址。

标记页:

选择目的地:

<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
            <asp:ListItem>Make a selection</asp:ListItem>
            <asp:ListItem Value="1">jaipur</asp:ListItem>
            <asp:ListItem Value="2">manali</asp:ListItem>
        </asp:DropDownList>
        <br />
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="package_id" DataSourceID="SqlDataSource1" OnRowDataBound="GridView1_RowDataBound1" OnSelectedIndexChanged="GridView1_SelectedIndexChanged1" AutoGenerateSelectButton="false">
            <Columns>
                <asp:BoundField DataField="Title_of_package" HeaderText="Title_of_package" SortExpression="Title_of_package" />
                <asp:BoundField DataField="No_of_Days" HeaderText="No_of_Days" SortExpression="No_of_Days" />
                <asp:BoundField DataField="Details" HeaderText="Details" SortExpression="Details" />
                <asp:BoundField DataField="package_id" HeaderText="package_id" ReadOnly="True" SortExpression="package_id" />
        </Columns>
    </asp:GridView>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:registrationConnectionString %>" SelectCommand="SELECT [Title_of_package], [No_of_Days], [Details], [package_id] FROM [User_choice] WHERE ([Id_of_place] = @Id_of_place)">
        <SelectParameters>
            <asp:ControlParameter ControlID="DropDownList1" Name="Id_of_place" PropertyName="SelectedValue" Type="String" />
        </SelectParameters>
    </asp:SqlDataSource>

C#中的代码隐藏文件:

protected void GridView1_RowDataBound1(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            // Get the value in the hyperlink column.
            string HyperLinkValue = e.Row.Cells[2].Text;

            if (e.Row.Cells[3].Text == "100")
            {    
                HyperLink myLink = new HyperLink();
                myLink.NavigateUrl = "~/afterlogin/ChokiDhaniVisit.aspx";
                myLink.Text = HyperLinkValue;
                // then add the control to the cell.
                e.Row.Cells[2].Controls.Add(myLink);
            }    

            if (e.Row.Cells[3].Text == "101")
            {    
                HyperLink myLink = new HyperLink();
                myLink.NavigateUrl = "~/Manali.aspx";
                myLink.Text = HyperLinkValue;
                // then add the control to the cell.
                e.Row.Cells[2].Controls.Add(myLink);
            }
        }
    }

现在我希望用户点击行的HyperLink列后,将其Title_of_package存储在标签中。我试过了:

 protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
       Label11.Text = ((HyperLink)GridView1.SelectedRow.Cells[0].Controls[0]).Text;
        Label11.Text = Session["destype"].ToString();

    }

但它出现了错误。我是初学者。

2 个答案:

答案 0 :(得分:0)

我建议将以下内容添加到GridView1_SelectedIndexChanged

GridViewRow row = GridView1.SelectedRow;
HyperLink hyperLink = ((HyperLink)row.FindControl["hyperlinkname"]);

Label1.Text = hyperLink.Text;

答案 1 :(得分:0)

你几乎就在那里,但会议的使用如下

Session["desType"] = ((HyperLink)GridView1.SelectedRow.Cells[0].Controls[0]).Text;

你得到它

Label1.Text = Session["desType"].toString();