我有两个aspx页面,“ source.aspx”和“ destination.aspx”。我已经在place.aspx中放置了一个占位符控件,然后以编程方式用linkbuttons填充它(这是source.aspx.cs文件):
LinkButton[] link = new LinkButton[n]; //where n is the no. of records in my databse
OleDbCommand cmd = new OleDbCommand("select column from table", con);
OleDbDataReader rd = cmd.ExecuteReader(); //reading column from the database
int i = 0;
while (rd.Read())
{
link[i] = new LinkButton();
link[i].ID = "link" + i;
link[i].Text = rd[0].ToString();//value read from the database becomes the text for linkbutton
PlaceHolder1.Controls.Add(link[i]);//adding the link to the placeholder
i++;
}
此页面的源(source.aspx)是:
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<p>
<br />
<asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
</p>
</asp:Content>
现在,在destination.aspx中,我想访问在source.aspx上单击的链接按钮的文本。我该怎么做呢?
可能会有更好的方法来解释您为什么要这样做。通常,除非您已更改表单的回发位置,否则链接按钮应提交回其自己的页面。在这里,更多的信息将是有用的;您真的需要回发到其他页面吗?
而且,动态生成的控件要使用起来有些棘手。除非您在每次页面加载时(即在初始加载和回发时)调用代码以重新生成控件,否则控件属性将不可用。
如果您要提交回同一页面,并重新生成所有链接按钮,则在单击事件处理程序中,text属性可以用作
((LinkButton)sender).Text
您将发送方对象转换为链接按钮,然后访问其Text属性。
根据下面的评论进行编辑:
好的,最简单的方法是使用超链接而不是链接按钮,然后在查询字符串中传递超链接文本。
您也似乎不会从数组中的链接中获得任何好处,因此您可以抛弃该变量,然后在循环中执行此操作:
while (rd.Read())
{
HyperLink link = new HyperLink();
link.ID = "link" + i;
link.Text = rd[0].ToString();//value read from the database becomes the text for linkbutton
link.NavigateUrl = "Destination.aspx?linktext=" + rd[0].ToString();
PlaceHolder1.Controls.Add(link);//adding the link to the placeholder
i++;
}
在destination.aspx页中,该值可作为Request.QueryString(“ linktext”)使用。还要注意,您可以随意调用查询字符串变量(将“ linktext”替换为所需的任何变量)。而且,如果可以的话,最好是处理诸如id之类的短内容而不是链接的文本。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句