【问题标题】:Retrieve a value of a row when clicked hyperlink field of that row in a gridview in asp.net using c#使用c#在asp.net的gridview中单击该行的超链接字段时检索该行的值
【发布时间】:2015-07-16 21:17:10
【问题描述】:

我正在制作一个旅游网站。但我遇到了一个问题。我有一个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();

    }

但它显示错误。我是初学者。

【问题讨论】:

    标签: c# asp.net gridview hyperlink


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

      你快到了,但会话使用如下

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

      你会明白的

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

      【讨论】:

        猜你喜欢
        • 2010-10-19
        • 2012-10-01
        • 2015-02-01
        • 2016-09-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-08-05
        • 1970-01-01
        相关资源
        最近更新 更多