Get line data after button click on an ASP.NET Repeater

Asked

Viewed 150 times

0

Good evening, my question is this... I want to make a "select" of the data of a line of the Repeater control according to the clicked Linkbutton (the white pencil of blue background), thus storing this data in different Sessions (name, user, sector, position, shift and extension), so to throw this data in a form (which is on another page, redirected after pressing Linkbutton) so that the form Textboxes receive these Sessions in String format so that they can be edited. That’s my goal, but what’s stopping me from going on is just getting the data from the button line clicked. Follow images for better understanding.

inserir a descrição da imagem aqui

This is the result of the page, now for example, if I click on the Edit button (pencil) of Zeca Amaral I wanted to get the Session["name"] = "Zeca Amaral", Session["user"] = "Zeca.eletro", Session["sector"] = "Electric", and so on.

I’ll pass my current code

<asp:Repeater ID="Repeater1" runat="server">
        <HeaderTemplate>
            <table class="table">
                <tr>
                    <th>
                        ID</th>
                    <th>
                        Nome</th>
                    <th>
                        Usuário</th>
                    <th>
                        Setor</th>
                    <th>
                        Cargo</th>
                    <th>
                        Turno</th>
                    <th>
                        Ramal</th>
                </tr>
        </HeaderTemplate>
        <ItemTemplate>
            <tr>
                <td>
                    <asp:Label ID="lblID" runat="server" class="lbl" Text='<% #DataBinder.Eval(Container.DataItem, "id") %>'></asp:Label></td>
                <td>
                    <asp:Label ID="lblNome" runat="server" class="lbl" Text='<% # DataBinder.Eval(Container.DataItem, "nome") %>'></asp:Label></td>
                <td>
                    <asp:Label ID="lblUsuario" runat="server" class="lbl" Text='<% #DataBinder.Eval(Container.DataItem, "usuario") %>'></asp:Label></td>
                <td>
                    <asp:Label ID="lblSetor" runat="server" class="lbl" Text='<% #DataBinder.Eval(Container.DataItem, "setor") %>'></asp:Label></td>
                <td>
                    <asp:Label ID="lblCargo" runat="server" class="lbl" Text='<% #DataBinder.Eval(Container.DataItem,"cargo") %>'></asp:Label></td>
                <td>
                    <asp:Label ID="lblTurno" runat="server" class="lbl" Text='<% #DataBinder.Eval(Container.DataItem,"turno") %>'></asp:Label></td>
                <td>
                    <asp:Label ID="lblRamal" runat="server" class="lbl" Text='<% #DataBinder.Eval(Container.DataItem,"ramal") %>'></asp:Label></td>
                <td>
                    <asp:LinkButton ID="btnEditar" runat="server" class="btn-sm btn-info" ToolTip="Editar" PostBackUrl="~/Exercícios/Formulario.aspx" CommandArgument='<%#Eval("id")%>' OnClick="btnEditar_Click"><span class="glyphicon glyphicon-pencil"></span></asp:LinkButton>
                    <asp:LinkButton ID="btnExcluir" runat="server" class="btn-sm btn-danger" ToolTip="Excluir" CommandArgument='<%#Eval("id") %>' OnClick="btnExcluir_Click"><span class="glyphicon glyphicon-remove"></span></asp:LinkButton>
                </td>
            </tr>
        </ItemTemplate>
    </asp:Repeater>

Follow . Cs of the page

    SqlConnection sqlconn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConexaoCASA"].ConnectionString);

protected void Page_Load(object sender, EventArgs e)
{       
    SqlDataReader dr = null;

    try
    {
        SqlCommand sqlSelect = new SqlCommand("SELECT id, nome, usuario, setor, cargo, turno, ramal FROM spc_funcionario_aprendiz", sqlconn);

        sqlconn.Open();

        SqlDataAdapter sqlDA = new SqlDataAdapter(sqlSelect);

        DataTable dt = new DataTable();

        sqlDA.Fill(dt);

        Repeater1.DataSource = dt;
        Repeater1.DataBind();
    }
    finally
    {
        if (dr != null)
        {
            dr.Close();
        }

        if (sqlconn != null)
        {
            sqlconn.Close();
        }
    }
}

protected void btnEditar_Click(object sender, EventArgs e)
{
    Session["nome"] = "";
    Session["usuario"] = "";
    Session["setor"] = "";
    Session["cargo"] = "";
    Session["turno"] = "";
    Session["ramal"] = "";
}

In the event btnEdit I want the Sessions to receive the respective result I want, already explained earlier. And that is, I hope you have understood and can help me, I thank you in advance! :)

1 answer

0


This is the solution as you are doing:

protected void btnEditar_Click(object sender, EventArgs e)
{
    //Busca o item do seu repeater
    var rptItem = (RepeaterItem)((Button)sender).Parent;

    //Busca os dados do seu item
    var dataRowView = ((DataRowView)rptItem.DataItem);

    Session["nome"] = dataRowView["nome"];
    Session["usuario"] = dataRowView["usuario"];
    Session["setor"] = dataRowView["setor"];
    Session["cargo"] = dataRowView["cargo"];
    Session["turno"] = dataRowView["turno"];
    Session["ramal"] = dataRowView["ramal"];
}

There is yet another way:

Implement the event OnItemCommand of your repeater and assign a CommandName in his LinkButton.

<asp:LinkButton ID="btnEditar" runat="server" CommandName="editar"/>

Then in your code, you can search the data as follows:

protected void repeater_ItemCommand(object source, RepeaterCommandEventArgs e)
{
    if (e.CommandName == "editar")
    {
        //Busca os dados do seu item
        var dataRowView = ((DataRowView)e.Item.DataItem);

        Session["nome"] = dataRowView["nome"];
        Session["usuario"] = dataRowView["usuario"];
        Session["setor"] = dataRowView["setor"];
        Session["cargo"] = dataRowView["cargo"];
        Session["turno"] = dataRowView["turno"];
        Session["ramal"] = dataRowView["ramal"];
    }
}
  • Oops, thank you very much, I ended up testing and it worked! I did it in another way also using the form page. In the event of btnEditar put: protected void btnEditar_Click(object sender, EventArgs e) {&#xA; Session["id"] = ((LinkButton)sender).CommandArgument;&#xA; Response.Redirect("Formulario.aspx");&#xA; } After that in the form . Cs I put the data reading and passed to the editing Textboxes, so to do the update.. I did more things but briefly that’s it. Thanks for the help :)

Browser other questions tagged

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