本文介绍了按钮和中继器控制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为我的项目使用ASP.NET,C#,Sql Server 2005和我正在使用转发器控件来构成一个用户表,该用户表是站点的成员,并且每个用户旁边都有一个按钮,以便可以将其添加到您的朋友列表中.除了不知道如何在单击按钮后获取每一行的特定信息外,我已进行了所有设置.我的意思是

I am using ASP.NET, C#, Sql Server 2005 for my project andI'm using the repeater control to form a table of users that are a member of a site and a button next to each user so that they can be added to your friends list. I have everything set up except I dont know how to get the specific info of each row after a button click. I mean,

如何向按钮旁边显示用户名的人发送朋友请求?或者如何访问按钮旁边显示的用户名,以便我将其用于代码?

How to send a friend request to a person whose username is displayed besides the button? orHow to access the the displayed username besides the button so that I can use it for my code?

我的脚本

<asp:Repeater ID="myrepeater" runat="server">
        <HeaderTemplate>
            <table style="font: 8pt verdana">
                <tr style="background-color:#3C78C3">
                    <th>Search Result</th>
                </tr>

        </HeaderTemplate>
        <ItemTemplate>
            <tr>
                <td>
                    <%#DataBinder.Eval(Container,"DataItem.allun")%>
                    <asp:Button ID="Button1" runat="server" Text="Button" />
                </td>
           </tr>
        </ItemTemplate>
        <FooterTemplate>
            </table>
        </FooterTemplate>

隐藏代码

protected void btnSearch_Click(object sender, EventArgs e)
{
    con.Open();
    if (txtSearch.Text != "")
    {
        SqlDataAdapter mycommand = new SqlDataAdapter("select * from WebAdminTable where allun='" + txtSearch.Text + "'", con);
        DataSet ds = new DataSet();
        mycommand.Fill(ds);
        myrepeater.DataSource = ds;
        myrepeater.DataBind();
    }
    else
    {
        //msgbox for entering value
    }
    con.Close();
}

protected void btnAddToMyFList_Click(object sender, EventArgs e)
{
      //?
}

推荐答案

您可以将 userName 绑定到标签控件,然后单击按钮即可访问标签控件的值,例如...

You can bind the userName to a label control and then you can access the value of the label control on the click of button like...

protected void btnAddToMyFList_Click(object sender, EventArgs e)
{
  Button btnAddToMyFList = (Button)sender;
  Label label = (Label)btnAddToMyFList.Parent.FindControl("LabelId");
  String labelText = label.Text; //userName of clicked row
}

这篇关于按钮和中继器控制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 02:19