Question

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?

Was it helpful?

Solution 2

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.

OTHER TIPS

Buttons inside a repeater don't work exactly the same as a button placed elseware. You need to use the CommandName and CommandAttributes fields to identify your button and your record. Then you use the ItemCommand event of the repeater to react to that specific button for that specific record.

Here is a good example: http://www.developer.com/net/asp/article.php/3609466/ASPNET-Tip-Responding-to-the-Repeater-Controls-ItemCommand-Event.htm

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top