问题描述
我正在处理一个接受扫描输入到文本框中的页面.在文本框中输入输入后,将通过自动回发动态生成包含输入项其他字段的用户控件.用户控件还包含一个链接按钮,旨在删除该项目(本身).单击此按钮将生成一个删除"事件,该事件应该冒泡到父级.
I am working on a page that accepts scanned input into a textbox. Once input is entered into the textbox, a user control that contains additional fields for the input item is dynamically generated via autopostback. The user control also contains a link button that is intended to delete the item (itself). Clicking this button generates a "delete" event that is supposed to bubble up to the parent.
不幸的是,这似乎没有发生-删除处理代码从未到达过.我的理解是,这是因为尚未在页面加载时生成控件,因此尚未创建事件处理程序.但是由于我不知道在页面加载时需要生成哪些控件(因为尚未处理用户输入),因此无法移动代码以将用户控件生成到PageLoad.
Unfortunately this does not seem to happen - the deletion handling code is never reached. My understanding is that it is because the controls have not been generated yet at page load, so the event handlers have not been created yet. But since I don't know what controls need to be generated at page load (since the user input hasn't been processed yet), I can't move the code to generate the user controls to PageLoad.
应如何处理?我会以错误的方式处理吗?我的大部分相关代码如下.
How should this be handled? Am I going about this the wrong way? Most of my relevant code is below.
感谢您的帮助!
page.aspx:
page.aspx:
Enter SKU (tab to enter):
<asp:TextBox ID="EntryTextBox" CssClass="textbox" AutoPostBack="true" OnTextChanged="newItem" runat="server"></asp:TextBox>
<asp:Panel ID="itempanel" runat="server">
</asp:Panel>
page.aspx.cs:
page.aspx.cs:
protected void newItem(object sender, EventArgs e)
{
// we need to store item entries in ViewState so they comeback on postback;
// they can't be stored in the controls themselves as the controls will
// disappear
ViewState["skus"] += "\t" + EntryTextBox.Text;
ViewState["descs"] += "\t" + itemLookup(EntryTextBox.Text);
// ...more item descriptors...
updateItemPanel();
}
protected void updateItemPanel()
{
// generate a control for each item entered in ViewState
itempanel.Controls.Clear();
List<string> skus = new List<string>(ViewState["items"].ToString().Substring(1).Split('\t'));
List<string> descs = new List<string>(ViewState["descs"].ToString().Substring(1).Split('\t'));
// ...more item descriptors...
int i = 0;
foreach (string sku in skus)
{
item newitemctrl = (item)Page.LoadControl("~/item.ascx");
newitemctrl.line = (i + 1).ToString();
newitemctrl.sku = skus[i];
newitemctrl.description = descs[i];
// ...more item descriptors...
newitemctrl.deleteLinkClicked += new EventHandler(deleteClicked);
itempanel.Controls.Add(newitemctrl);
i++;
}
}
protected void deleteClicked(object sender, EventArgs e)
{
List<string> skus = new List<string>(ViewState["skus"].ToString().Substring(1).Split('\t'));
List<string> descs = new List<string>(ViewState["descs"].ToString().Substring(1).Split('\t'));
// ...more item descriptors...
item olditemctrl = (item)sender;
skus.RemoveAt(Convert.ToInt32(olditemctrl.number) - 1);
descs.RemoveAt(Convert.ToInt32(olditemctrl.number) - 1);
ViewState["skus"] = skus.ToString();
ViewState["descs"] = descs.ToString();
updateItemPanel();
}
item.ascx:
item.ascx:
<asp:LinkButton ID="DeleteLinkButton" runat="server" onclick="DeleteLinkButton_Click">Delete</asp:LinkButton>
item.ascx.cs:
item.ascx.cs:
public event EventHandler deleteLinkClicked;
protected void DeleteLinkButton_Click(object sender, EventArgs e)
{
if (this.deleteLinkClicked != null)
{
this.deleteLinkClicked(new object(), new EventArgs());
}
}
推荐答案
您可以通过将其添加为onclick事件来在javascript中调度回发事件:
You can dispatch a postback event in javascript by adding this as onclick event:
__doPostBack("<%= button.ClientID %>", "");
DoPostBack有两个参数,第一个是ID,第二个是事件名称.我成功使用了此解决方案.
DoPostBack has two arguments, the first is the ID, the second is the event name.I used this solution successfully.
您会在此处找到更多信息.
注意:这本身不会自动触发事件,但是您可以在页面的加载事件"中看到想要发生的事情.您可以获取如下参数::Request.Form ["__ EVENTTARGET"]您具有对表单数据的完全访问权限,因此还可以从动态创建的控件中获取值
Note:This itself does not fire the event automatically, but you can see what you want to happen in the Load Event of your page.You can get the arguments like this: : Request.Form["__EVENTTARGET"]You have full access to the form data, so you can also get the values from the dynamically created controls
这篇关于处理事件以动态生成.NET用户控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!