0

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;
    }
}

编辑 我遗漏了回发检查

我也试过在protected void FormView1_DataBound(object sender, EventArgs e)没有运气的情况下使用它

4

3 回答 3

0

我做了很多假设并添加了一些代码来为您制作一个工作示例。如果您认为我没有得到您,请通过评论我编辑您的问题的回答来添加更多信息!

假设

  • Fruit- 代表您的 dataContainer - 简化它,以存储名称
  • Page_Load我绑定了一些演示值
  • 添加了分页功能以提供工作示例

解决方案

  1. 我用 acustomMethod(..)来绑定属性NavigateUrl
  2. 添加string.Format(..)到连接你的字符串

还不清楚

  • SeasonLabel 和 TypeLabel 是从哪里来的?

标记

<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; }
}

结果图片

测试此代码时的结果

于 2013-10-21T20:30:10.427 回答
-1

首先,使用string.Format格式化一个url字符串

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)

于 2013-10-21T18:10:52.073 回答
-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.
}
于 2013-10-21T19:09:11.193 回答