我有一个嵌套的中继器
因此,我的代码中看不到任何控件,因此,我发现必须使用FindControl才能使控件可见。
我想在按钮单击后传递一个标签的值,但是我很难把它正确

带有值的标签

<asp:Label ID="lblordernr" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.sid").ToString()%>'></asp:Label>


我使用的按钮:

<asp:Button ID="btnPaid" runat="server" Text="Paid" OnClick="rlrtAdres_ItemDataBound"
                        Style="height: 26px" CssClass="knop" CommandName="btnPaid" />


RepeaterItemDataBound

protected void rlrtAdres_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    Label lblordernr = e.Item.FindControl("lblordernr") as Label;
    Repeater myRepeater = (Repeater)sender;
    Button btn = (Button)sender;
    switch (btn.CommandName)
    {
        case "btnPaid":
            MutateSold("paid", lblordernr.Text);
            break;
    }
}


我首先有一个普通的buttonOclick控件,但我不知道如何将其与标签值组合在一起,并使用嵌套的转发器和此转发器中的每个不可见控件来调用最终方法“ MutateSold”以实现数据库目的

protected void btnButton_Click(object sender, EventArgs e)
{
    Control myControllblordernr = FindControl("lblordernr");
    Button btn = (Button)sender;
    switch (btn.CommandName)
    {
        case "btnPaid":
            MutateSold("paid", myControllblordernr.ToString());
            break;
     }
}

最佳答案

您收到标题中张贴的错误,因为ClickbtnPaid事件需要具有签名的事件处理程序,例如:

protected void btnButton_Click(object sender, EventArgs e)

您将其指向具有以下特征的方法:

protected void rlrtAdres_ItemDataBound(object sender, RepeaterItemEventArgs e)

如果btnPaidlblordernr与转发器位于同一ItemTemplate中,则可以在按钮上调用FindControl以获得对lblordernr的引用。例如,如果这样声明btnPaid

<asp:Button ID="btnPaid" runat="server" Text="Paid" OnClick="btnPaid_Click"
    Style="height: 26px" CssClass="knop"  />


以下代码将为您提供对lblOrderNr的引用。

protected void btnPaid_Click(object sender, EventArgs e)
{
    var btnPaid = (Button)sender;
    var lblOrderNr = (Label)btnPaid.FindControl("lblordernr");
    var labelText = lblOrderNr.Text;

    //Do whatever else needs to be done
}


另外,您也可以将lblOrderNr中的值添加到按钮的CommandArgument属性中,然后从那里获取它。

<asp:Button ID="btnPaid" runat="server" Text="Paid" OnClick="btnPaid_Click"
    CommandArgument='<%# DataBinder.Eval(Container, "DataItem.sid").ToString()%>'
    Style="height: 26px" CssClass="knop" />


protected void btnPaid_Click(object sender, EventArgs e)
{
    var btnPaid = (Button)sender;
    var labelText = btnPaid.CommandArgument;

    //Do whatever else needs to be done
}

关于c# - 'ItemDataBound'的重载不匹配委托(delegate)'System.EventHandler',我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/6754422/

10-12 05:07