如何为转发器控件中的按钮设置单击事件
本文关键字:按钮 设置 单击 事件 转发器 控件 | 更新日期: 2023-09-27 18:35:41
嗨,我正在使用 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();
}
会话 [电子邮件] 是 = 用户登录电子邮件,temmp [电子邮件] 是 = 发送好友请求的用户。
我不知道如何让 temmp[电子邮件] 到我的友谊表
这是我的中继器控制代码
<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;">
<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" />
<asp:Button ID="Reject" runat="server" Text="Reject" Width="98px" />
</div>
<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;">
<br />
<asp:LinkButton ID="LinkEmail" runat="server"CommandArgument=
'<%# Eval("Email") %>' CommandName="mail"><%#Eval("Email") %></asp:LinkButton>
<asp:HiddenField ID="HiddenField1" runat="server" />
</div>
</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);
}
我只希望当有人点击确认按钮时,朋友请求发件人的电子邮件应该等于会话[temmp]
您可以按
如下方式为Button
添加OnClick
事件,无需指定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;
}