我有一个嵌套的转发器 因为我的代码中没有控件是可见的,因此我发现我必须使用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;
}
}
答案 0 :(得分:1)
您收到了标题中发布的错误,因为Click
btnPaid
事件需要一个带有签名的事件处理程序:
protected void btnButton_Click(object sender, EventArgs e)
您将其指向具有以下签名的方法:
protected void rlrtAdres_ItemDataBound(object sender, RepeaterItemEventArgs e)
如果转发器的btnPaid
和lblordernr
位于同一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
}