【问题标题】:How to edit an entire column for all rows asp:GridView如何为所有行编辑整个列 asp:GridView
【发布时间】:2013-05-30 14:55:58
【问题描述】:

所以我用谷歌搜索过stackoverflow,现在我的大脑已经超负荷了。我是 asp.net 的新手,但要掌握它的窍门。

我目前的要求是有一个网格视图,在加载时,所有行的 1 列都会立即进入编辑模式。我用这个问题和代码让我继续前进:

Allowing one column to be edited but not another

<asp:gridview id="CustomersGridView"      
              datasourceid="CustomersSqlDataSource"      
              autogeneratecolumns="false"     
              autogenerateeditbutton="true"     
              allowpaging="true"      
              datakeynames="CustomerID"       
runat="server">      
    <columns>       
        <asp:boundfield datafield="CustomerID" readonly="true" headertext="Customer ID"/>
        <asp:boundfield datafield="CompanyName" readonly="true" headertext="Customer Name"/> 
        <asp:boundfield datafield="Address" headertext="Address"/>       
        <asp:boundfield datafield="City" headertext="City"/>       
        <asp:boundfield datafield="PostalCode" headertext="ZIP Code"/>     
    </columns> 
</asp:gridview>

我已经搜索并找到了一些好的解决方案,但是,我并不完全理解它们,并且未能成功实施它们。它们是:

Edit/Update a single GridView field

Put multiple rows of a gridview into edit mode

所以我的问题是,如何将列(例如邮政编码)同时置于所有行的编辑模式?

感谢所有帮助!

谢谢!

斯蒂芬

【问题讨论】:

    标签: c# asp.net


    【解决方案1】:

    您将无法使用内置的编辑功能,但您可以通过使用 TemplateField 在编辑模式下加载列来实现这一点:

    <asp:GridView ID="GridView1" runat="server" OnRowCommand="GridView1_RowCommand" ...>    
        <Columns>
            <asp:TemplateField>
                <ItemTemplate>
                    <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("SomeColumn") %>' />
                </ItemTemplate>
            </asp:TemplateField>
            <asp:BoundField DataField="SomeOtherColumn" HeaderText="Foo" />
            ...
            <asp:TemplateField>
                <ItemTemplate>
                    <asp:Button ID="Button1" runat="server" CommandName="Update" CommandArgument='<%# Container.ItemIndex %>' />
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>
    

    代码隐藏:

    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        GridViewRow row = GridView1.Rows[(int)e.CommandArgument];
        if (row != null)
        {
            TextBox txt = row.FindControl("TextBox1") as TextBox;
            if (txt != null)
            {
                //get the value from the textbox
                string value = txt.Text;
            }
        }
    }
    

    编辑:在 GridView 之外放置一个按钮,您会像这样更新:

    <asp:GridView>
        ...
    </asp:GridView>
    <asp:Button ID="Button1" runat="server" Text="Update" OnClick="Button1_Click" />
    

    代码隐藏:

    protected void Button1_Click(object sender, EventArgs e)
    {
        foreach (GridViewRow row in GridView1.Rows)
        {
            TextBox txt = row.FindControl("TextBox1") as TextBox;
            if (txt != null)
            {
                //get the value from the textbox
                string value = txt.Text;
            }
        }
    }
    

    【讨论】:

    • 这还能让我保持简单并将其余列放在绑定字段中吗?另外,这是否允许我更新所有行,还是一次更新一行?
    • 您可以对其余列使用绑定字段,是的,该列在所有行中都是可编辑的。
    • 好的,在我勾选之前的最后一个问题。您能否详细说明一下 Button1 的需求和用途?
    • 不,如果您从 GridView 外部进行更新,则不需要 RowCommand 事件。 RowCommand 事件仅被触发以获取对被单击行的引用。由于您在 GridView 之外进行更新,因此您可以像上面的示例一样循环遍历所有行。
    • 不,如果您使用 GridView 外部的按钮执行更新,则不需要这样做。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-08-19
    • 1970-01-01
    • 1970-01-01
    • 2018-08-04
    • 2015-06-29
    • 1970-01-01
    • 2012-07-09
    相关资源
    最近更新 更多