I have a strange problem with a Button within a Repeater
when the site is loaded (Dotnetnuke settings page) the repeater is fully created and everything works except the buttons in the repeater. they do just nothing i have set in very line on the code a breakpoint to be sure it doesn't do anything.
After an other Button(runs CreateButton) does a Postback (rebind of the repeater happens) they work as expected.
here is my code:
<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>
and here is code behind:
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);
}
Where i'm doing a mistake?
Well i cloud bang my head...
i finally found a solution why it didn't work, thx to https://siderite.dev/blog/dopostback-works-webformdopostbackwitho.html
Solution I had a validator on this site, something like this:
<asp:RequiredFieldValidator
ControlToValidate="name"
ForeColor="Red"
ErrorMessage="Button Name is requierd"
runat="server">*</asp:RequiredFieldValidator>
this did prevent the button event because the input was empty.