gpt4 book ai didi

c# - 如何将 BUTTON 值插入数据库

转载 作者:行者123 更新时间:2023-11-29 07:46:34 25 4
gpt4 key购买 nike

您好,我正在尝试将按钮的值插入代码中:

这是按钮的代码:

<asp:Button ID="addFriend" runat="server" ' CommandArgument ='<%# Eval("ProfileId") %>' 
CommandName='<%# Eval("ProfileId") %>' Value='<%# Eval("ProfileId") %>Text="Add Friend"
CssClass="btn btn-info" OnClick="addFriend_Click" />

这里是插入代码:

    protected void addFriend_Click(object sender, EventArgs e)
{
// Get the UserId of the just-added user
MembershipUser currentUser = Membership.GetUser();
Guid currentUserId = (Guid)currentUser.ProviderUserKey;
var addFriend = sender as Button;
var value = addFriend.Text;


// Insert a new record into UserPro
string connectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;

string insertSql = "INSERT INTO User_Friend(ProfileId1, ProfileId) VALUES (@FriendProfileId, (SELECT ProfileId FROM User_Profile WHERE UserId = @UserId))";

using (SqlConnection myConnection = new SqlConnection(connectionString))
{
myConnection.Open();
SqlCommand myCommand = new SqlCommand(insertSql, myConnection);
myCommand.Parameters.AddWithValue("@FriendProfileId", addFriend.Text);
myCommand.Parameters.AddWithValue("@UserId", currentUserId);

myCommand.ExecuteNonQuery();
myConnection.Close();
}
}

它可以工作,但它插入按钮文本而不是值。

最佳答案

asp:Button类没有 Value 属性。

相反,在您的表单中添加一个包含配置文件值的附加隐藏控件

<asp:HiddenField runat="server" ID="hdnFriend" Value='<%# Eval("ProfileId") %>' />

然后您可以在后面的按钮处理程序代码中使用它:

myCommand.Parameters.AddWithValue("@FriendProfileId", hdnFriend.Value);

关于c# - 如何将 BUTTON 值插入数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27646606/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com