我在转发器中的按钮有一个奇怪的问题
当网站加载(Dotnetnuke设置页面)时,转发器已完全创建,除转发器中的按钮外,一切正常。他们没有做任何事情,我已经在代码上设置了断点,以确保它不会做任何事情。
在另一个Button(运行CreateButton)执行回发(转发器的重新绑定发生)后,它们按预期工作。
这是我的代码:
<asp:Repeater ID="rptForm" runat="server" EnableViewState="true" ItemType="ButtonObject" >
<%--OnItemCommand="rpt_ItemCommand" OnItemDataBound="Repeater_ItemDataBound"--%>
<HeaderTemplate>
<table class="ButtonTable">
<tr class="ButtonTableHeader">
<td>Id</td>
<td>Name</td>
<td>Link</td>
<td></td>
<td></td>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<%# Item.Id %>
</td>
<td>
<%# Item.Name %>
</td>
<td>
<%# Item.Link %>
</td>
<td>
<asp:Button ID="EditBtn" runat="server" Text="Editieren"
data-ButtonId="<%# Item.Id %>"
CssClass="uk-button uk-button-primary"
OnClick="EditButton"/>
<%--CommandName="edit" CommandArgument="<%# Item.Id %>" --%>
</td>
<td>
<asp:Button ID="DeleteBtn" runat="server" Text="Button Löschen"
data-ButtonId="<%# Item.Id %>"
CssClass="uk-button uk-button-primary"
OnClick="DeleteButton"/>
<%--CommandName="delete" CommandArgument="<%# Item.Id %>"--%>
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
以下是代码:
private static ButtonSettings BtnSettings = new ButtonSettings();
protected void Page_Init(object sender, EventArgs e)
{
if (!IsPostBack)
{
BtnSettings = new ButtonSettings();
if (Settings.Contains("ButtonSettings"))
{
BtnSettings.BuildButtonSettings(Settings["ButtonSettings"].ToString());
}
BindRepeater();
}
}
private void BindRepeater()
{
this.rptForm.DataSource = BtnSettings.Buttons;
this.rptForm.DataBind();
}
protected void rpt_ItemCommand(object sender, RepeaterCommandEventArgs e)
{
//This Method will not be executed first time when site loads
if (e.CommandName == "edit") // add this
{
EditButtonDiv.Visible = true;
}
}
protected void Repeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item)
{
Button button = (Button)e.Item.FindControl("DeleteBtn");
if (button != null)
{
button.Click += DeleteButton;
}
}
}
protected void DeleteButton(Object sender, EventArgs e)
{
//This Method will not be executed first time when site loads
Button button = sender as Button;
var buttonID = Convert.ToInt32(button.Attributes["data-ButtonId"].ToString());
BtnSettings.RemoveButton(buttonID);
BindRepeater();
}
protected void EditButton(Object sender, EventArgs e)
{
//This Method will not be executed first time when site loads
/* TODO:: Get ButtonId - Set values */
EditButtonDiv.Visible = true;
}
protected void CreateButton(Object sender, EventArgs e)
{
var buttonObj = new ButtonObject();
...
BindRepeater();
ScriptManager.RegisterStartupScript(Page, this.GetType(),
"script", "ReRegisterDivEvents()", true);
ScriptManager.RegisterStartupScript(Page, this.GetType(),
"script", "ClearForm()", true);
}
我在哪里做错了?
答案 0 :(得分:0)
转发器内的按钮与放置其他软件的按钮完全不同。您需要使用CommandName和CommandAttributes字段来标识您的按钮和记录。然后使用转发器的ItemCommand事件响应该特定记录的特定按钮。
答案 1 :(得分:0)
嗯,我砰的一声摇头......
我终于找到了一个解决方案,为什么它不起作用,thx到http://siderite.blogspot.com/2007/03/dopostback-works-webformdopostbackwitho.html
<强>解决方案强> 我在这个网站上有一个验证器,如下所示:
<asp:RequiredFieldValidator
ControlToValidate="name"
ForeColor="Red"
ErrorMessage="Button Name is requierd"
runat="server">*</asp:RequiredFieldValidator>
这确实阻止了按钮事件,因为输入是空的。