Onserverclick is not running

Asked

Viewed 63 times

1

I am trying to create dynamic buttons to delete users from a database but onserverclick simply does not run the function.

This is my code so far:

yourHTMLstring += "<td>" + dt.Rows[i]["NivelAcesso"] + "<button id='"+idx+ "' type='submit' runat='server' onserverclick='deleteRow' class='btn btn-light float-right'><i class='fas fa-trash-alt'></i></button></td>";

This is the function:

protected void deleteRow(object sender, EventArgs e)
    {

        Button button = (Button)sender;
        string buttonId = button.ID;

        SQL _sql = new SQL();

        _sql.DeleteUtilizadores(Convert.ToInt32(buttonId));
    }
  • 1

    Make the event public: public void deleteRow(object sender, EventArgs e)

  • Still no running event

  • 1

    Try prefixing the button: <asp:button.... It’s only a two-point

  • Did not solve, I tried to change the name of the event and also still does not work

  • 1

    Just to test, passes the event to Onclick.

  • It didn’t, I don’t understand why it didn’t run Vent, supposedly it’s all for Vent to run properly.

Show 2 more comments

1 answer

1


So that the bind of onserverclick work it needs to be declared physically in your aspx. This is because in the render event and before the Page_load ASP.Net will parse your content and replace your onserverclick='deleteRow' by a javascript call responsible for PostBack to the server, in the click of the element.

See the example below where the control was added to Default.aspx:

<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication.Form._Default" %>

<asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">

    <button id='lbotao' type='submit' runat='server' onserverclick='deleteRow' class='btn btn-light float-right'><i class='fas fa-trash-alt'></i>teste</button>

</asp:Content>

Now see how the component is rendered in the browser:

<button onclick="__doPostBack('ctl00$MainContent$lbotao','')" id="MainContent_lbotao" type="submit" class="btn btn-light float-right"><i class="fas fa-trash-alt"></i></button>

As you are adding control as an html string this conversion will not be performed and you will end up with a simple transcription of what you declared.

<button id="lbotao" type="submit" runat="server" onserverclick="deleteRow" class="btn btn-light float-right"><i class="fas fa-trash-alt"></i></button>
  • 1

    @Afonso, you understand why?

  • So how do I fix this? I’m sorry if I don’t get it very well, I’m still learning and I don’t know many things, thanks anyway

  • Present the rest of the code in your question, if you want to manipulate screen elements in the backend, you should use the appropriate components for this. From the only line you presented it seems to me it should be a GridView or a Repeater...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.