簡體   English   中英

使用C#在asp.net中的gridview中單擊該行的超鏈接字段時,檢索該行的值

[英]Retrieve a value of a row when clicked hyperlink field of that row in a gridview in asp.net using c#

我正在建立一個旅游網站。 但是我遇到了一個問題。 我有一個DropDownList及其下面的GridView 一旦在DropDownList選擇一個值,就會顯示GridView中的各個值。 我已經在文件后面的代碼中將GridView的列轉換為HyperLink 每個HyperLink導航到一個不同的URL。

標記頁面:

選擇目的地:

<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();

    }

但它顯示錯誤。 我是初學者。

我建議將以下內容添加到GridView1_SelectedIndexChanged

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

Label1.Text = hyperLink.Text;

您快到那兒了,但是該會話的使用方式如下

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

你會得到像

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM