在Repeater中传递行的ID以在ASP NET中提交按钮

时间:2019-01-04 11:58:15

标签: c# asp.net webforms

由于我使用了许多视图状态,因此我试图停止依赖它们,因为页面需要永久加载。我有一个带有表的中继器,当我单击一个按钮时,它将启用具有另一个中继器的表单。我需要传递第一个转发器行的ID,以便我可以在“提交”按钮上更新与该项目相关的数据。

注意。我需要的ID在中继器的第一列

<td>
    <asp:Label ID="lblIdRegisto" runat="server" Text='<%# Eval("ID_Registo") %>' Style="display: none;" />
    <asp:Label ID="lblReferenciaCliente" runat="server" Text='<%# Eval("Ref_Cliente") %>' />
</td>

这是我的代码

这是我的提交按钮

protected void btnValidarFormulario_Click(object sender, EventArgs e)
{
    if (Page.IsValid)
    {
        string cnnStr = ConfigurationManager.ConnectionStrings["ValidacaoFormas_ConnectionString"].ConnectionString;

        ADefinicao def = new ADefinicao();

        def.ReferenciaInterna = txtRefInterna.Text;
        def.Indice = txtIndice.Text;
        def.Observacoes = txtObservacoes.Text;
        def.isValid = "Sim";
        def.DataValidacao = DateTime.Now.ToString("dd-MM-yyyy").ToString();

        string nome = Session["Nome"].ToString();
        int id = Convert.ToInt32(ViewState["id"]);

        id = ADefinicao.UpdateADefinicao(nome, id, def);

        ScriptManager.RegisterStartupScript(this, this.GetType(), "key_name", "ShowToast('success','Documento validado com sucesso.'); setTimeout(function(){window.location.href ='/Conta/Início.aspx'}, 3000);", true);
    }
}

这是主要的中继器

<asp:Repeater ID="rptValidacao" runat="server" DataSourceID="SqlDataSource1" OnItemCommand="rptValidacao_ItemCommand">
    <HeaderTemplate>
        <table id="tblValidarRegistos" class="table table-bordered table-striped table-hover dataTable text-center">
        <thead class="thead-dark">
            <tr>
                <th class="text-center">Ref. Cliente</th>
                <th class="text-center">Ref. Interna</th>
                <th class="text-center">Índice</th>
                <th class="text-center">Qtd. Formas</th>
                <th class="text-center"></th>
            </tr>
        </thead>
    </HeaderTemplate>
    <ItemTemplate>
        <tr style="font-size:15px;">
            <td>
                <asp:Label ID="lblIdRegisto" runat="server" Text='<%# Eval("ID_Registo") %>' Style="display: none;" />
                <asp:Label ID="lblReferenciaCliente" runat="server" Text='<%# Eval("Ref_Cliente") %>' />
            </td>
            <td>
                <asp:Label ID="lblReferenciaInterna" runat="server" Text='<%# Eval("Ref_Interna") %>' />
            </td>
            <td>
                <asp:Label ID="lblIndicie" runat="server" Text='<%# Eval("Indice") %>' />
            </td>
            <td>
                <asp:Label ID="lblQTDFormas" runat="server" Text='<%# Eval("QTD_Formas") %>' />
            </td>
            <td>
                <asp:ImageButton ID="ibtnValidar" runat="server" ImageUrl="/Images/Icones/success.png" CommandName="Validar" CommandArgument='<%# Eval("ID_Registo") %>' Width="25" />                                    
            </td>
        </tr>
    </ItemTemplate>
    <FooterTemplate>
        </table>
    </FooterTemplate>
</asp:Repeater>

我想知道是否有办法将中继器从该中继器传递到此按钮

<asp:Button ID="btnValidarFormulario" CssClass="submit-button font-weight-bold" runat="server" Text="Validar Documentação" ValidationGroup="vgValidarDocumentacao"  OnClick="btnValidarFormulario_Click" />

编辑。

这是img按钮单击带有某些数据的转发器时需要显示的表格,需要更新,它有点长,所以我只张贴了,因为有人问我。提交按钮位于最后。我只需要表单上所有数据都来自的ID,并将其传递给“提交”按钮

 <div id="divValidar" class="card card-danger shadow mx-auto custom-card-layout" visible="false" runat="server" style="min-width: 600px; max-width: 950px;">
    <div class="card-header text-center custom-card-header">
        <h3 class="card-title font-weight-bold">A - Definição</h3>
    </div>
    <!-- /.card-header -->

    <!-- form start -->
    <div class="card-body">

        <div class="container">

            <asp:ValidationSummary runat="server"
                ID="ValidationSummary2"
                DisplayMode="BulletList"
                HeaderText="<b>Preencha todos os campos devidamente para proseguir.</b>"
                ValidationGroup="vgADefinicao"
                CssClass="alert validation-summary"
                ShowSummary="true" />


            <fieldset>
                <legend>Informação Referêncial</legend>


                <div class="row justify-content-center">

                    <div class="col-sm-4">
                        <div class="form-group col-10 mx-auto text-center">
                            <asp:Label ID="lblRefCliente" Text="1. Referência Cliente" AssociatedControlID="txtRefCliente" runat="server"></asp:Label>
                            <i class="fas fa-asterisk text-danger mb-2" style="font-size: 8px;"></i>
                            <asp:TextBox ID="txtRefCliente" CssClass="form-control form-control-sm border border-dark" runat="server" ReadOnly="true"></asp:TextBox>


                        </div>
                    </div>

                    <div class="col-sm-4">
                        <div class="form-group col-10 mx-auto">
                            <asp:Label ID="lblRefInterna" Text="2. Referência Interna" AssociatedControlID="txtRefInterna" runat="server"></asp:Label>
                            <asp:TextBox ID="txtRefInterna" CssClass="form-control form-control-sm border border-dark" runat="server" placeholder="Referência Interna"></asp:TextBox>
                        </div>
                    </div>


                    <div class="col-sm-4">
                        <div class="form-group col-10 mx-auto">
                            <asp:Label ID="lblIndice" Text="3. Índice" AssociatedControlID="txtIndice" runat="server"></asp:Label>
                            <asp:TextBox ID="txtIndice" CssClass="form-control form-control-sm border border-dark" runat="server" placeholder="Indíce"></asp:TextBox>
                        </div>
                    </div>
                </div>

            </fieldset>

            <fieldset>
                <legend>Casquilhos e Formas - Quantidade</legend>


                <div class="row justify-content-center pb-4">

                    <div class="col-md-6">
                    </div>

                    <div class="col-md-6 text-center">
                        <strong>5. Quantidade de Casquilhos</strong>
                    </div>
                </div>

                <div class="row justify-content-center row-eq-height">
                    <div class="col-md-6">


                        <div class="form-group col-7 mx-auto text-center">
                            <asp:Label ID="lblQtdFormas" Text="4. Quantidade de Formas" AssociatedControlID="txtQtdFormas" runat="server"></asp:Label>
                            <i class="fas fa-asterisk text-danger mb-2" style="font-size: 8px;"></i>
                            <asp:TextBox ID="txtQtdFormas" CssClass="form-control form-control-sm border border-dark" runat="server" ReadOnly="true"></asp:TextBox>

                        </div>

                    </div>

                    <div class="col-md-6">


                        <div class="form-group col-7 mx-auto text-center">
                            <asp:Label ID="lblCasquilhosFixos" Text="5.1 Fixos" AssociatedControlID="txtCasquilhosFixos" runat="server"></asp:Label>
                            <i class="fas fa-asterisk text-danger mb-2" style="font-size: 8px;"></i>
                            <asp:TextBox ID="txtCasquilhosFixos" CssClass="form-control form-control-sm border border-dark" runat="server" ReadOnly="true"></asp:TextBox>

                        </div>

                        <div class="form-group col-7 mx-auto text-center">
                            <asp:Label ID="lblCasquilhosLivres" Text="5.2 Livres" AssociatedControlID="txtCasquilhosLivres" runat="server"></asp:Label>
                            <i class="fas fa-asterisk text-danger mb-2" style="font-size: 8px;"></i>
                            <asp:TextBox ID="txtCasquilhosLivres" CssClass="form-control form-control-sm border border-dark" runat="server" ReadOnly="true"></asp:TextBox>

                        </div>

                    </div>
                </div>
            </fieldset>


            <fieldset>
                <legend>Casquilhos e Formas - Diâmetros</legend>


                <div class="row justify-content-center pb-4">

                    <div class="col-md-6 text-center">
                        <strong>6. Diâmetro dos casquilhos</strong>
                    </div>

                    <div class="col-md-6 text-center">
                        <strong>7. Diâmetro das formas</strong>
                    </div>

                </div>


                <div class="row justify-content-center">
                    <div class="col-md-6">
                        <div class="form-group col-6 mx-auto text-center">
                            <asp:Label ID="lblDiametroCasqLadoFixo" Text="6.1 Lado Fixo" AssociatedControlID="txtDiametroCasqLadoFixo" runat="server"></asp:Label>
                            <i class="fas fa-asterisk text-danger mb-2" style="font-size: 8px;"></i>

                            <div class="input-group input-group-sm">
                                <asp:TextBox ID="txtDiametroCasqLadoFixo" CssClass="form-control form-control-sm border border-dark" runat="server" ReadOnly="true"></asp:TextBox>
                                <div class="input-group-append border border-dark">
                                    <span class="input-group-text"><strong>mm</strong></span>
                                </div>
                            </div>

                        </div>
                        <div class="form-group col-6 mx-auto text-center">
                            <asp:Label ID="lblDiametroCasqLadoMovel" Text="7.2 Lado Móvel" AssociatedControlID="lblDiametroCasqLadoMovel" runat="server"></asp:Label>
                            <i class="fas fa-asterisk text-danger mb-2" style="font-size: 8px;"></i>
                            <div class="input-group input-group-sm">

                                <asp:TextBox ID="txtDiametroCasqLadoMovel" CssClass="form-control form-control-sm border border-dark" runat="server" ReadOnly="true"></asp:TextBox>
                                <div class="input-group-append border border-dark">
                                    <span class="input-group-text"><strong>mm</strong></span>
                                </div>
                            </div>

                        </div>

                    </div>

                    <div class="col-md-6">

                        <div class="form-group col-6 mx-auto text-center">
                            <asp:Label ID="lblDiametroFormasFixo" Text="7.1 Lado fixo" AssociatedControlID="txtDiametroFormasFixo" runat="server"></asp:Label>
                            <i class="fas fa-asterisk text-danger mb-2" style="font-size: 8px;"></i>
                            <div class="input-group input-group-sm">

                                <asp:TextBox ID="txtDiametroFormasFixo" CssClass="form-control form-control-sm border border-dark" runat="server" ReadOnly="true"></asp:TextBox>
                                <div class="input-group-append border border-dark">
                                    <span class="input-group-text"><strong>mm</strong></span>
                                </div>
                            </div>


                        </div>

                        <div class="form-group col-6 mx-auto text-center">
                            <asp:Label ID="lblDiametroFormasMovel" Text="7.2 Lado Móvel" AssociatedControlID="txtDiametroFormasMovel" runat="server"></asp:Label>
                            <i class="fas fa-asterisk text-danger mb-2" style="font-size: 8px;"></i>
                            <div class="input-group input-group-sm">
                                <asp:TextBox ID="txtDiametroFormasMovel" CssClass="form-control form-control-sm border border-dark" runat="server" ReadOnly="true"></asp:TextBox>
                                <div class="input-group-append border border-dark">
                                    <span class="input-group-text"><strong>mm</strong></span>
                                </div>
                            </div>

                        </div>
                        <div class="form-group col-6 mx-auto text-center">
                            <asp:Label ID="lblDiametroFormasCorpo" Text="7.2 Corpo" AssociatedControlID="txtDiametroFormasCorpo" runat="server"></asp:Label>
                            <i class="fas fa-asterisk text-danger mb-2" style="font-size: 8px;"></i>
                            <div class="input-group input-group-sm">

                                <asp:TextBox ID="txtDiametroFormasCorpo" CssClass="form-control form-control-sm border border-dark" runat="server" ReadOnly="true"></asp:TextBox>
                                <div class="input-group-append border border-dark">
                                    <span class="input-group-text"><strong>mm</strong></span>
                                </div>
                            </div>

                        </div>
                    </div>
                </div>

            </fieldset>


            <fieldset>
                <legend>Cru
                    <small>(A preencher pela Extrusão)</small>
                </legend>

                <div class="row justify-content-center pb-4">

                    <div class="col-md-6 text-center">
                        <strong>8. Informação do Cru</strong>
                    </div>

                    <div class="col-md-6 text-center">
                        <strong>8.3 Parâmetros do cru:</strong>
                    </div>

                </div>

                <div class="row justify-content-center">

                    <div class="col-md-6">

                        <div class="form-group col-6 mx-auto text-center">
                            <asp:Label ID="lblCruBase" Text="8.1 Base" AssociatedControlID="txtCruBase" runat="server"></asp:Label>
                            <asp:TextBox ID="txtCruBase" CssClass="form-control form-control-sm border border-dark" runat="server" ReadOnly="true"></asp:TextBox>
                        </div>

                        <div class="form-group col-6 mx-auto text-center">
                            <asp:Label ID="lblCruLinha" Text="8.2 Linha" AssociatedControlID="txtCruLinha" runat="server"></asp:Label>
                            <asp:TextBox ID="txtCruLinha" CssClass="form-control form-control-smborder border-dark" runat="server" ReadOnly="true"></asp:TextBox>
                        </div>

                        <div class="form-group col-10 mx-auto text-center">
                            <asp:Label ID="lblMarcacaoContinua" Text="8.4 Marcação contínua" AssociatedControlID="txtMarcacaoContinua" runat="server" />
                            <small class="text-muted">(texto + cor)</small>
                            <asp:TextBox ID="txtMarcacaoContinua" TextMode="MultiLine" CssClass="form-control form-control-sm border border-dark" runat="server" ReadOnly="true" />
                        </div>

                    </div>

                    <div class="col-md-6">

                        <div class="form-group col-6 mx-auto text-center">
                            <asp:Label ID="lblCruDiametro" Text="Diâmetro" AssociatedControlID="txtCruDiametro" runat="server"></asp:Label>
                            <div class="input-group input-group-sm">
                                <asp:TextBox ID="txtCruDiametro" CssClass="form-control form-control-sm border border-dark" runat="server" ReadOnly="true"></asp:TextBox>
                                <div class="input-group-append border border-dark">
                                    <span class="input-group-text"><strong>mm</strong></span>
                                </div>
                            </div>
                        </div>

                        <div class="form-group col-6 mx-auto text-center">
                            <asp:Label ID="lblCruEspessura" Text="Espessura" AssociatedControlID="txtCruEspessura" runat="server"></asp:Label>

                            <div class="input-group input-group-sm">
                                <asp:TextBox ID="txtCruEspessura" CssClass="form-control form-control-sm border border-dark" runat="server" ReadOnly="true"></asp:TextBox>
                                <div class="input-group-append border border-dark">
                                    <span class="input-group-text"><strong>mm</strong></span>
                                </div>

                            </div>
                        </div>

                        <div class="form-group col-6 mx-auto text-center">
                            <asp:Label ID="lblComprimento" Text="Comprimento" AssociatedControlID="txtComprimento" runat="server"></asp:Label>
                            <div class="input-group input-group-sm">
                                <asp:TextBox ID="txtComprimento" CssClass="form-control form-control-sm border border-dark" runat="server" ReadOnly="true"></asp:TextBox>
                                <div class="input-group-append border border-dark">
                                    <span class="input-group-text"><strong>mm</strong></span>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>

            </fieldset>


            <fieldset>
                <legend>9. Datas Previstas</legend>

                <div class="row justify-content-center">

                    <div class="col-md-6">

                        <div class="form-group col-6 mx-auto text-center">
                            <asp:Label ID="lblDataFormasPreSerie" runat="server" AssociatedControlID="txtReadOnlyDataFormasPreSerie" Text="9.1 Formas Pré-Série" />
                            <i class="fas fa-asterisk text-danger mb-2" style="font-size: 8px;"></i>
                            <div class="input-group input-group-sm">
                                <div class="input-group-prepend border border-dark">
                                    <span class="input-group-text"><i class="far fa-calendar-alt"></i></span>
                                </div>
                                <asp:TextBox ID="txtReadOnlyDataFormasPreSerie" runat="server" class="form-control form-control-sm border border-dark" ReadOnly="true" />
                            </div>

                        </div>

                        <div class="form-group col-6 mx-auto text-center">
                            <asp:Label ID="lblDataFormasSerie" runat="server" AssociatedControlID="txtReadOnlyDataFormasSerie" Text="9.3 Formas Série" />
                            <i class="fas fa-asterisk text-danger mb-2" style="font-size: 8px;"></i>
                            <div class="input-group input-group-sm">
                                <div class="input-group-prepend border border-dark">
                                    <span class="input-group-text"><i class="far fa-calendar-alt"></i></span>
                                </div>
                                <asp:TextBox ID="txtReadOnlyDataFormasSerie" runat="server" class="form-control form-control-sm border border-dark" ReadOnly="true" />
                            </div>
                        </div>

                    </div>

                    <div class="col-md-6">


                        <div class="form-group col-6 mx-auto text-center">
                            <asp:Label ID="lblDataAmostrasIniciais" runat="server" AssociatedControlID="txtReadOnlyDataAmostrasIniciais" Text="9.2 Amostras iniciais" />
                            <i class="fas fa-asterisk text-danger mb-2" style="font-size: 8px;"></i>
                            <div class="input-group input-group-sm">
                                <div class="input-group-prepend border border-dark">
                                    <span class="input-group-text"><i class="far fa-calendar-alt"></i></span>
                                </div>
                                <asp:TextBox ID="txtReadOnlyDataAmostrasIniciais" runat="server" class="form-control form-control-sm border border-dark" ReadOnly="true" />
                            </div>

                        </div>

                        <div class="form-group col-6 mx-auto text-center">
                            <asp:Label ID="lblDataArranqueProducao" runat="server" AssociatedControlID="txtReadOnlyDataArranqueProducao" Text="9.4 Arranque Produção" />
                            <i class="fas fa-asterisk text-danger mb-2" style="font-size: 8px;"></i>
                            <div class="input-group input-group-sm">
                                <div class="input-group-prepend border border-dark">
                                    <span class="input-group-text"><i class="far fa-calendar-alt"></i></span>
                                </div>
                                <asp:TextBox ID="txtReadOnlyDataArranqueProducao" runat="server" class="form-control form-control-sm border border-dark" ReadOnly="true" />
                            </div>

                        </div>
                    </div>
                </div>
            </fieldset>

            <fieldset>
                <legend>Documentos</legend>

                <div class="row justify-content-center">

                    <div class="col-md-12">



                        <asp:Repeater ID="rptAnexos" runat="Server" OnItemCommand="rptAnexos_ItemCommand">
                            <HeaderTemplate>
                                        <table id="tblAnexos" class="table-sm table table-striped table-bordered text-center rounded" style="border-radius: 10px;">
                                            <caption>Lista de Anexos</caption>
                                            <thead class="thead-dark">
                                                <tr>
                                                    <th>ID</th>
                                                    <th>Tipo Desenho</th>
                                                    <th>Nome Ficheiro</th>
                                                    <th>Inserido Por</th>
                                                    <th></th>
                                                </tr>
                                            </thead>
                            </HeaderTemplate>
                            <ItemTemplate>

                                <tr style="font-size:small">
                                    <td>
                                        <asp:Label ID="lblIdRegisto" runat="server" Text=' <%# Eval("ID_Registo") %>' />
                                    </td>
                                    <td>
                                        <asp:Label ID="lblTipoDesenho" runat="server" Text=' <%# Eval("Tipo") %>' />

                                    </td>
                                    <td>
                                        <asp:Label ID="lblNomeAnexo" runat="server" Text='<%# Eval("NomeAnexo") %>' />
                                    </td>
                                    <td>
                                        <asp:Label ID="lblPiloto" runat="server" Text='<%# Eval("Piloto") %>' />
                                    </td>
                                    <td class="text-center">
                                        <asp:ImageButton runat="server" ImageUrl="/Images/Icones/download.png" Width="20" CommandName="Download" CommandArgument='<%# Eval("ID") %>' />
                                    </td>
                                </tr>

                            </ItemTemplate>
                            <FooterTemplate>
                                </table>
                            </FooterTemplate>
                        </asp:Repeater>

                    </div>
                </div>

            </fieldset>

            <fieldset>
                <legend>Observações</legend>

                <div class="row justify-content-center row-eq-height">
                    <div class="col-md-12">


                        <div class="form-group col-7 mx-auto text-center">
                            <asp:Label ID="lblObservacoes" Text="Observações" AssociatedControlID="txtObservacoes" runat="server"></asp:Label>
                            <i class="fas fa-asterisk text-danger mb-2" style="font-size: 8px;"></i>
                            <asp:TextBox ID="txtObservacoes" CssClass="form-control form-control-sm border border-dark" runat="server" TextMode="MultiLine" Rows="3"></asp:TextBox>
                            <div class="valid-feedback">Success! You've done it.</div>
                            <i class="glyphicon glyphicon-ok form-control-feedback"></i>
                            <div class="valid-feedback feedback-icon">
                                <i class="fa fa-check"></i>
                            </div>
                            <div class="invalid-feedback feedback-icon">
                                <i class="fa fa-times"></i>
                            </div>

                            <asp:CustomValidator ID="cvObservacoes" runat="server" EnableClientScript="True" ValidationGroup="vgValidarDocumentacao" Display="Dynamic" ValidateEmptyText="True" ClientValidationFunction="CustomValidator" CssClass="server-validator" ControlToValidate="txtObservacoes"></asp:CustomValidator>
                            <asp:RequiredFieldValidator ID="rfvObservacoes" runat="server" CssClass="server-validator" ValidationGroup="vgValidarDocumentacao" SetFocusOnError="True" ControlToValidate="txtObservacoes" Text="Campo necessário." ErrorMessage="<b>Observações:</b> Campo necessário." Display="Dynamic"></asp:RequiredFieldValidator>


                        </div>

                    </div>
                </div>

            </fieldset>
        </div>
    </div>

    <!-- /.card-body -->
    <div class="card-footer custom-card-footer">
        <asp:Button ID="btnValidarFormulario" CssClass="submit-button font-weight-bold" runat="server" Text="Validar Documentação" ValidationGroup="vgValidarDocumentacao"  OnClick="btnValidarFormulario_Click" />
    </div>
    <!-- /.card-footer -->
</div>

2 个答案:

答案 0 :(得分:2)

您可以将Label放置在ItemTemplate中,并将Visibility设置为false。

<asp:Repeater ID="rptValidacao" runat="server">
    <ItemTemplate>
        <asp:Label ID="LabelID" runat="server" Text='<%# Eval("id") %>' Visible="false"></asp:Label>
    </ItemTemplate>
</asp:Repeater>

但是,如果在Button Click事件中使用FindCOntrol,则Label仍会以正确的值存在

protected void btnValidarFormulario_Click(object sender, EventArgs e)
{
    Label lbl = rptValidacao.Items[i].FindControl("LabelID") as Label;
}

您还应该研究DataKeys。但是中继器不能使用它们。

答案 1 :(得分:1)

一种解决方案是创建一个HiddenField,并在中继器yourHiddenField.Value的数据绑定上填充以下内容:

3,4,7,12

,然后在按钮上单击以拆分隐藏字段中的值。如果在执行提交之前通过Javascript填充了隐藏字段,则即使在客户端也可以执行此操作。