我在应用程序的formview中设置了asp:Hyperlink
和标签
<br />
<b>Posting Site:</b>
<asp:Label ID="AppleLabel" runat="server" Text='<%# Bind("Apple") %>' />
<br />
<asp:HyperLink ID="hplPostIt" Text="Text" runat="server"/>
在我的Page_Load
事件中,我尝试找到标签和超链接:
Label Apple = FormView1.FindControl("Apple") as Label;
HyperLink hplPostIt = FormView1.FindControl("hplPostIt") as HyperLink;
然后我尝试使用if语句根据标签的文本更改超链接的NavigateURL
属性,并且Visual Studio抱怨未设置对象引用。这是我的if else条件:
if (!Page.IsPostBack)
{
lblRow.Text = Request.QueryString["num"];
hplPostIt.Text = "Eat Now";
if (Fruit.Text == "Fruit")
{
hplPostIt.NavigateUrl =
"https://www.mysite.com/Fruit/Apples.aspx?Season=" +
SeasonLabel.Text + "&Color_Date=" + TypeLabel.Text +
"&num=" + SeasonLabel.Text;
}
else
{
hplPostIt.NavigateUrl =
"Fruit/Apples.aspx?Season=" + SeasonLabel.Text +
"&Color_Date=" + TypeLabel.Text + "&num=" + SeasonLabel.Text;
}
}
被修改 我遗漏了Postback检查
我也尝试在protected void FormView1_DataBound(object sender, EventArgs e)
中使用它而没有运气
答案 0 :(得分:0)
我做了很多假设并添加了一些代码来为您制作一个实用的示例。如果您认为我没有找到您,请通过评论
<强>假设强> <强>解决方案强> 目前还不清楚 <强>标记强> <强>代码隐藏强> 结果图
Fruit
- 代表您的dataContainer - 简化它,以存储名称Page_Load
我绑定了一些演示值
customMethod(..)
绑定属性NavigateUrl
string.Format(..)
以结束字符串
<form id="form1" runat="server">
<div>
<asp:FormView ID="fvFruits" runat="server" AllowPaging="True"
OnPageIndexChanging="fvFruits_PageIndexChanging">
<ItemTemplate>
<asp:Label ID="lblFruit" runat="server" Text='<%# Bind("Name") %>' />
<asp:HyperLink ID="hplPostIt" Text="yourText"
NavigateUrl='<%# customMethod(Eval("Name")) %>' runat="server"/>
</ItemTemplate>
</asp:FormView>
</div>
</form>
protected void Page_Load(object sender, EventArgs e)
{
// demo purposes to add some data
if (!Page.IsPostBack)
bindDemoData();
}
private void bindDemoData()
{
List<Fruit> fruits = new List<Fruit>();
fruits.Add(new Fruit() { Name = "Apple" });
fruits.Add(new Fruit() { Name = "Banana" });
fruits.Add(new Fruit() { Name = "Orange" });
fvFruits.DataSource = fruits;
fvFruits.DataBind();
}
/// <summary>
/// Custom method to check for a given parameter value, which will be given
/// by the dataBinding within markup code.
/// You might even pass more parameter values
/// </summary>
/// <param name="fruit">the name of the fruit</param>
/// <returns>custom link for each given fruitName</returns>
public string customMethod(object fruit)
{
if (fruit != null)
{
string fruitName = fruit.ToString();
// insert custom binding here!
string url = "https://www.mysite.com/Fruit/";
if (fruitName == "Apple")
url += "Apples.aspx";
else if (fruitName == "Banana")
url += "Banana.aspx";
else if (fruitName == "Orange")
url += "Orange.aspx";
/*else
url += "defaultFruit.aspx";; // up to you*/
// can't see where SeasonLabel and TypeLabel are defined??? please add a comment if I did get you wrong
url += string.Format("?Season={0}&Color_Date={1}&num={2}", SeasonLabel.Text, TypeLabel.Text, SeasonLabel.Text);
//uncomment this line and comment out the line above to get a working example
//url += string.Format("?Season={0}&Color_Date={1}&num={2}", "a", "b", "c");
return url;
}
return "https://www.mysite.com/error.aspx"; // probably - but up to you
}
protected void fvFruits_PageIndexChanging(object sender, FormViewPageEventArgs e)
{
fvFruits.PageIndex = e.NewPageIndex;
bindDemoData();
}
// demo data container
public class Fruit
{
public string Name { get; set; }
}
答案 1 :(得分:-1)
首先,使用string.Format
格式化网址字符串
hplPostIt.NavigateUrl = string.Format("https://www.mysite.com/Fruit/Apples.aspx?Season={0}&Color_Date={1}&num={2}", SeasonLabel.Text, TypeLabel.Text, SeasonLabel.Text);
<强>第二强> 如果它直接位于Page上,则不需要FindControl来访问hplPostIt。请参阅“youpagename.aspx.design.cs”以查找控制声明
<强>第三强> 可能由文本控件之一(SeasonLabel,TypeLabel)抛出的null引用异常
答案 2 :(得分:-1)
您是否尝试在formview数据绑定事件中运行它而不是页面加载?
类似的东西:
<asp:FormView ID="FormView1" runat="server" OnDataBound="FormView1_DataBound" ...>
并在
背后的代码中protected void FormView1_DataBound(object sender, EventArgs e)
{
Label Apple = FormView1.FindControl("Apple") as Label;
HyperLink hplPostIt = FormView1.FindControl("hplPostIt") as HyperLink;
// etc.
}
作为解决方法
<asp:HyperLink ID="hplPostIt" runat="server" NavigateUrl='<%# getLink(Eval("Apple")) >' />
然后
protected string getLink(object obj)
{
string fruit = obj.ToString();
// if else with fruit string.
}