【发布时间】: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