如何在转发器控件中为按钮设置单击事件

时间:2014-12-31 05:51:58

标签: c# asp.net

您好我正在使用asp.net/c#进行社交网络服务,我接受朋友请求时遇到的问题很少。我在转发器控制上显示那些朋友请求,并且转发器控件具有确认按钮并拒绝底部。一旦用户按下确认按钮,将执行sql查询。

这是sql查询

SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["RegistrationConnectionString"].ConnectionString);
         conn.Open();
         try
         {



             string inserQuery = "Update [Friendship] Set Friend_Status = 1 WHERE Friend1 ='" + Session["Email"] + "' And Friend2='" + Session["temmp"] + "'";
             SqlCommand commm = new SqlCommand(inserQuery, conn);
             commm.Parameters.AddWithValue("@Friend1", Session["Email"].ToString());
             commm.Parameters.AddWithValue("@Friend2", Session["Temmp"].ToString());




             commm.ExecuteNonQuery();


           //  Label1.Text = Session["Email"].ToString();



    }

会话[email]是=用户登录电子邮件,temmp [email]是=发送好友请求的用户。

我不知道如何将temmp [email]发送到我的友谊表

这是我的转发器控制代码

  <asp:Repeater ID="Repeater3" runat="server">
        <ItemTemplate>


    <div style="border-top: thin none #91ADDD; border-bottom: thin none #91ADDD; padding: 10px;  width: 548px; margin-top: 10px; right: 10px; left: 10px; border-left-width: thin; margin-left: 15px; background-color: #F6F6F6; border-left-color: #91ADDD; border-right-color: #91ADDD;">
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            <asp:Label ID="Label8" runat="server"><%#Eval("YourName") %></asp:Label>

            <asp:Label ID="Label9" runat="server" Text="wants to be a friend !!!"></asp:Label>
            <br />
            <div style="width: 58px; height: 40px">
                <asp:Image ID="Image2" runat="server" Height="59px" ImageAlign="Top" ImageUrl='<%#Eval("ProfilePicPath") %> ' Width="55px" />
                <div style="width: 318px; margin-left: 176px; margin-top: -10px; height: 27px;">
                </div>
            </div>
    <div style="margin-top: -20px">
            <asp:Button ID="Confirm" runat="server" style=" margin-left: 250px; margin-top: 11px;" Text="Confirm" Width="98px" CommandName="confirm" />
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            <asp:Button ID="Reject" runat="server" Text="Reject" Width="98px" />
    </div>
&nbsp;<div style="width: 431px;  margin-left: 65px; margin-top: -60px">
                <asp:Label ID="Label6" runat="server" Font-Bold="True" Font-Names="Arial" ForeColor="#3b5998"><%#Eval("YourName") %> </asp:Label>
            </div>
            <div id="status" style=" width: 461px; margin-left: 78px; margin-top: 11px;">
                &nbsp;<br />
                <asp:LinkButton ID="LinkEmail" runat="server"CommandArgument=
    '<%# Eval("Email") %>' CommandName="mail"><%#Eval("Email") %></asp:LinkButton>        
                <asp:HiddenField ID="HiddenField1" runat="server"  />
            </div>
            &nbsp;
        </div>

 </ItemTemplate>
    </asp:Repeater>

这是我尝试代码背后的代码

  private void Repeater3_ItemCommand(object source, RepeaterCommandEventArgs e)

 {
     switch (e.CommandName)
     {
         case "mail":
             string emailId = e.CommandArgument.ToString();
             break;
     }
     Session["temmp"] = e.CommandArgument.ToString();

     if (e.CommandName == "confirm")
     {


         SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["RegistrationConnectionString"].ConnectionString);
         conn.Open();
         try
         {



             string inserQuery = "Update [Friendship] Set Friend_Status = 1 WHERE Friend1 ='" + Session["Email"] + "' And Friend2='" + Session["temmp"] + "'";
             SqlCommand commm = new SqlCommand(inserQuery, conn);
             commm.Parameters.AddWithValue("@Friend1", Session["Email"].ToString());
             commm.Parameters.AddWithValue("@Friend2", Session["Temmp"].ToString());




             commm.ExecuteNonQuery();


           //  Label1.Text = Session["Email"].ToString();



    }

 override protected void OnInit(EventArgs e)
 {
     base.OnInit(e);

     Repeater3.ItemCommand += new RepeaterCommandEventHandler(Repeater3_ItemCommand);


 }

我只希望当有人点击确认按钮时,Friendrequest发件人的电子邮件应该等于Session [temmp]

1 个答案:

答案 0 :(得分:3)

您可以为OnClick添加Button事件,如下所示,无需指定CommandName

<asp:LinkButton ID="LinkEmail" runat="server" CommandArgument='<%# Eval("Email") %>' OnClick="LinkEmail_Click"><%#Eval("Email") %></asp:LinkButton>

在代码隐藏中,您可以添加事件调用

protected void LinkEmail_Click(object sender, EventArgs e)
{
    string emailId = (sender as LinkButton).CommandArgument;
}