【问题标题】:Write to Gridview Row when Linkbutton in Gridview clicked单击 Gridview 中的链接按钮时写入 Gridview 行
【发布时间】:2013-07-12 14:59:15
【问题描述】:

我有一个 Gridview,每行都有一个 Close linkbutton。当用户单击关闭Button 时,它应该将数据写入该特定行。我的数据已经写入,但是当我单击第三行 linkbutton 关闭时,它会写入第一行。我想让它写到第三行。

<asp:GridView ID="gvMain" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None"
Width="688px" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False"
OnRowCommand="gvTransInfo_RowCommand">
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<Columns>
    <asp:TemplateField HeaderText="Transaction Type" SortExpression="TransTypeDesc">
        <EditItemTemplate>
            <asp:DropDownList ID="DDLTrans" runat="server" DataSourceID="SqlDataSourceTrans"
                DataTextField="TransTypeDesc" DataValueField="TransTypeId">
            </asp:DropDownList>
            <asp:SqlDataSource ID="SqlDataSourceTrans" runat="server" ConnectionString="<%$ ConnectionStrings:iQueueConnectionString %>"
                SelectCommand="SELECT [TransTypeId], [TransTypeDesc] FROM [tblTransType]"></asp:SqlDataSource>
        </EditItemTemplate>
        <ItemTemplate>
            <asp:Label ID="lblTrans" runat="server" Text='<%# Bind("TransTypeDesc") %>'></asp:Label>
        </ItemTemplate>
        <ItemStyle HorizontalAlign="Center" />
    </asp:TemplateField>
    <asp:BoundField DataField="Received" HeaderText="Received" SortExpression="Received">
        <ItemStyle HorizontalAlign="Center" />
    </asp:BoundField>
    <asp:BoundField DataField="Complete" HeaderText="Complete" SortExpression="Complete">
        <ItemStyle HorizontalAlign="Center" />
    </asp:BoundField>
    <asp:BoundField DataField="TransTime" HeaderText="Est. Transaction Time" SortExpression="TransTime">
        <ItemStyle HorizontalAlign="Center" />
    </asp:BoundField>
    <asp:TemplateField ShowHeader="False">
        <ItemTemplate>
            <asp:LinkButton ID="lbClose" runat="server" CausesValidation="False" CommandName="CloseClicked"
                OnClick="CloseClick_Click">Close</asp:LinkButton>
            <asp:LinkButton ID="lbEdit" runat="server" CausesValidation="False" CommandName="EditRow">Edit</asp:LinkButton>
            &nbsp;<asp:LinkButton ID="lbDelete" runat="server" CausesValidation="False" CommandName="DeleteRow">Cancel</asp:LinkButton>
        </ItemTemplate>
        <EditItemTemplate>
            <asp:LinkButton ID="lbUpdate" runat="server" CausesValidation="True" CommandName="UpdateRow"
                Text="Update" CommandArgument='<%# Eval("TicketId")%>'></asp:LinkButton>
            &nbsp;<asp:LinkButton ID="lbCancel" runat="server" CausesValidation="False" CommandName="CancelUpdate"
                CommandArgument='<%# Eval("TicketId")%>' Text="Cancel"></asp:LinkButton>
        </EditItemTemplate>
        <FooterStyle HorizontalAlign="Center" />
        <ItemStyle HorizontalAlign="Center" />
    </asp:TemplateField>
</Columns>

protected void CloseClick_Click(object sender, EventArgs e)
    {
        var StartLink = (Control)sender;
        GridViewRow row = (GridViewRow)StartLink.NamingContainer;
        firstCellText = row.Cells[0].Text;
    }

【问题讨论】:

  • CloseClick_Click的代码是什么?
  • @DavideLettieri 已更新

标签: c# asp.net gridview


【解决方案1】:

使用带有select 属性的CommandField,而不是带有LinkButtonTemplateField

并在YourGridView_SelectedIndexChanged handler 中使用YourGridView.SelectedIndex 处理您单击的行。

你可以说:

firstCellText = row.Cells[YourGridView.SelectedIndex].Text;

【讨论】:

    【解决方案2】:

    解决了。我刚刚做了一个查询并根据我的 firstcellText 值显示它,从而解决了问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多