大家进来帮帮忙?=========
在数据库中有七条数据由id从小到大已经排列 他们的字段是: 名称、src
页面上有七个LinkButton
我想实现: 页面上的 LinkButton
每个LinkButton1的Text是id=1 字段名称
LinkButton2的Text是id=2 字段名称
·
·
·
LinkButton7的Text是id=7 字段名称
================================】
请各位给的 实例 谢谢
[解决办法]
//aspx
<asp:LinkButton ID= "LinkButton1 " runat= "server " OnClick= "LinkButton_Click "> LinkButton </asp:LinkButton>
<asp:LinkButton ID= "LinkButton2 " runat= "server " OnClick= "LinkButton_Click "> LinkButton </asp:LinkButton>
<asp:LinkButton ID= "LinkButton3 " runat= "server " OnClick= "LinkButton_Click "> LinkButton </asp:LinkButton>
<asp:LinkButton ID= "LinkButton4 " runat= "server " OnClick= "LinkButton_Click "> LinkButton </asp:LinkButton>
<asp:LinkButton ID= "LinkButton5 " runat= "server " OnClick= "LinkButton_Click "> LinkButton </asp:LinkButton>
<asp:LinkButton ID= "LinkButton6 " runat= "server " OnClick= "LinkButton_Click "> LinkButton </asp:LinkButton>
<asp:LinkButton ID= "LinkButton7 " runat= "server " OnClick= "LinkButton_Click "> LinkButton </asp:LinkButton>
//aspx.cs
private void Page_Load(object sender, System.EventArgs e)
{
if (!IsPostBack)
{
SqlConnection cn = new SqlConnection(@ "server=.\sqlExpress;uid=sa;pwd=sa;database=pubs ");
SqlCommand cmd = new SqlCommand( "select top 7 id, src from authors ", cn);
int i = 1;
cn.Open();
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
LinkButton l = (LinkButton)FindControl( "LinkButton " + i);
l.Text = dr[ "id "].ToString();
l.CommandArgument = dr[ "src "].ToString();
i++;
}
dr.Close();
cn.Close();
}
}
protected void LinkButton_Click(object sender, EventArgs e)
{
Response.Redirect( "Details.aspx?id= " + ((LinkButton)sender).CommandArgument);
}