这是TextBox,是事件处理程序:
<asp:Repeater ID="rpt_users" runat="server" OnItemCommand="rpt_users_ItemCommand" OnItemDataBound="rpt_users_ItemDataBound"><ItemTemplate>
<tr class="c0">
<td>
<asp:TextBox runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "daysleft") %>' OnTextChanged="Unnamed_TextChanged" AutoPostBack="true"/>
</td>
</tr>
</ItemTemplate></asp:Repeater>
protected void Unnamed_TextChanged(object sender, EventArgs e)
{
var repeaterItem = (sender as TextBox).NamingContainer as RepeaterItem;
var hiddenFieldKey = repeaterItem.FindControl("LoginField") as HiddenField;
DataTable data = ViewState["Data"] as DataTable;
var dataRow = data.Rows.Find(hiddenFieldKey.Value);
var userName = (dataRow[0].ToString()).Replace("'", "''");
... //more actions below
}
现在无法触发此事件处理程序。它有时候工作得很好,但最近我从代码隐藏中删除了Page_Init事件处理程序并用Page_Load事件处理程序替换它。这些事实可以联系起来吗?应该遵守哪些条件才能使此处理程序可访问?
答案 0 :(得分:1)
转发器数据绑定应该在内部而不是回发条件
public class Test
{
public string daysleft { get; set; }
}
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindRepeaterItems();
}
}
private void BindRepeaterItems()
{
var items = new List<Test>
{
new Test {daysleft = "Deepu"},
new Test {daysleft = "Darsh"}
};
rpt_users.DataSource = items;
rpt_users.DataBind();
}
protected void Unnamed_TextChanged(object sender, EventArgs e)
{
var repeaterItem = (sender as TextBox).NamingContainer as RepeaterItem;
var hiddenFieldKey = repeaterItem.FindControl("LoginField") as HiddenField;
}
}
<asp:Repeater ID="rpt_users" runat="server">
<ItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "daysleft") %>' OnTextChanged="Unnamed_TextChanged" AutoPostBack="true"/><br />
</ItemTemplate>
</asp:Repeater>