0

我遇到的问题是我需要通过单击按钮传递多个参数。我有一个从 sqlquery 打印出值的转发器对象,并且该按钮具有来自同一查询的参数

这是我的中继器:

 <asp:Repeater ID="Edit_Repeater" runat="server" DataSourceID="Edit_Repeater_Source">
                <HeaderTemplate>
                    <table class="u_Repeater">
                        <tr>
                            <th>
                                Group Name:
                            </th>
                            <th>
                                Permission Level:
                            </th>
                            <th>
                                Remove:
                            </th>
                        </tr>
                </HeaderTemplate>
                <ItemTemplate>
                    <tr>
                        <td>
                            <%#DataBinder.Eval(Container.DataItem, "Group_Name")%>
                        </td>
                        <td>
                            <%#DataBinder.Eval(Container.DataItem, "Name")%>
                        </td>
                        <td>
                            <asp:Button ID="Submit_Remove" runat="server" Text="Remove Permission" OnClick="Remove_Permission" CommandArgument='<%# Eval("GID") + ";" + Eval("PID") %>' />
                        </td>
                    </tr>
                </ItemTemplate>
                <FooterTemplate>
                    </table>
                </FooterTemplate>
            </asp:Repeater>

这是我背后的代码:

protected void Remove_Permission(object sender, CommandEventArgs e)
    {
        string[] arg = new string[2];            
        string[] commandArgs = e.CommandArgument.ToString().Split(';');
        float GID = float.Parse(arg[0]);
        float PID = float.Parse(arg[1]);
        string UID = User_ID.SelectedValue;

        using (SqlConnection connection = new SqlConnection(WebConfigurationManager.ConnectionStrings["app_migrationConnectionString"].ConnectionString))
        {
            SqlCommand cmd = new SqlCommand("DELETE FROM Membership WHERE GID = @GID AND PID = @PID AND UID = @UID", connection);
            cmd.Parameters.Add("@GID", SqlDbType.BigInt, -1);
            cmd.Parameters["@GID"].Value = GID;
            cmd.Parameters.Add("@PID", SqlDbType.BigInt, -1);
            cmd.Parameters["@PID"].Value = PID;
            cmd.Parameters.Add("@UID", SqlDbType.NVarChar, -1);
            cmd.Parameters["@UID"].Value = UID;

            try
            {
                connection.Open();
                cmd.ExecuteNonQuery();
                connection.Close();
            }

            catch (Exception ex)
            {

                throw ex;
            }
        }
    }

我没有收到任何构建错误,但是当我运行该页面时,我收到一个编译错误消息:

“Remove_Permission”没有重载匹配委托“System.EventHandler”

我几乎肯定代码是正确的,那么问题是什么?

4

2 回答 2

1

将您的更改CommandEventArgsEventArgs - 然后将您的发件人转换为 Button 并从那里获取命令参数。

protected void Remove_Permission(object sender, EventArgs e)
{
    var button = sender as Button;
    if (button !=null)
    {
        var commandArg = button.CommandArgument;
        //etc....
    }
}

或者使用命令事件而不是Click事件。

于 2013-01-22T16:26:13.467 回答
0

看来您的 Remove_Permission 函数应该是以下类型:

public delegate void EventHandler(
Object sender,
EventArgs e
)
于 2013-01-22T16:26:57.927 回答