asp.net 数据网格编辑

本文关键字:网格 编辑 数据网 数据 net asp | 更新日期: 2023-09-27 17:56:40

我有以下 asp.net 数据网格:

    <asp:DataGrid runat="server" ID="AutoGenerateTable" AutoGenerateColumns = "False" OnEditCommand="AutoGenerateTable_Edit" OnCancelCommand="btn_CancelEdits" OnUpdateCommand="btn_Update">
        <HeaderStyle Font-Bold="True" />
        <PagerStyle Mode="NumericPages" HorizontalAlign="Left" Font-Bold="true" />
        <Columns>
             <asp:BoundColumn HeaderText="Country" DataField="Country" ReadOnly="True"/>
             <asp:BoundColumn HeaderText="Partner" DataField="Partner" ReadOnly="True"/>
             <asp:BoundColumn HeaderText="Product" DataField="Product" ReadOnly="True"/>
            <asp:TemplateColumn HeaderText="Flag">
                <ItemTemplate>
                    <asp:Literal id="FlagText" runat="server" Text='<%# Eval("Flag")%>'/> 
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:DropDownList runat="server" ID="FlagFropDown" >
                        <asp:ListItem Text="Auto Fulfill" Value="1" />
                        <asp:ListItem Text="Do Not Auto Fulfill" Value="0" />
                    </asp:DropDownList>
                </EditItemTemplate>
            </asp:TemplateColumn>
            <asp:EditCommandColumn HeaderText="Edit" EditText="Edit" CancelText="Cancel" UpdateText="Update" />
        </Columns>
    </asp:DataGrid> 

除了一件事之外,一切都很好。

当我编辑数据时,我使用 DropDownList 为用户提供选项。

我想在用户按"编辑"时设置下拉列表的值。

问题是我找不到该控件,因为按下编辑按钮时它不存在,因此出现"对象未设置为实例"错误

按下按钮并且下拉列表在屏幕上后,我可以在调用更新方法时找到控件,因此我知道它最终会到达那里。

但是如何在用户按"编辑"时设置它。

我尝试从页面中查找控件并延迟调用该方法,但这不起作用。

例如:

....
    System.Threading.Thread.Sleep(1000);
        setIndexDropdown();
    }
    public void setIndexDropdown()
    {
        DropDownList DropDown = (DropDownList)Page.FindControl("FlagFropDown");
        Response.Write(DropDown.ID);
        Response.End();
    }

任何想法??

编辑

编辑功能代码:

public void AutoGenerateTable_Edit(Object sender, DataGridCommandEventArgs e)
{
    AutoGenerateTable.EditItemIndex = e.Item.ItemIndex;
    List<string> flag = new List<string>();
        flag.Add("Auto Fulfill");
        flag.Add("Do Not Auto Fulfill");
    AutoGenerateTable.DataSource = populateTable();
    AutoGenerateTable.DataBind();
    Literal text = e.Item.Cells[3].Controls[1] as Literal;
DropDownList list = e.Item.Cells[3].Controls[1] as DropDownList;
}

asp.net 数据网格编辑

你的问题是你试图在其中找到你的控制权DataGridIteme ,您的DataGridCommandEventArgs变量,是编辑命令的参数。因此,DataGridItem包含所有处于编辑模式的控件。这意味着您的下拉列表将不存在。

您需要做的是在绑定获取该行的DataGridItem。然后,DataGridItem包含处于编辑模式的所有控件。您已经有了行索引,因此只需使用它获取新DataGridItem即可。

public void AutoGenerateTable_Edit(Object sender, DataGridCommandEventArgs e)
{
    // Put the data grid into edit mode
    AutoGenerateTable.EditItemIndex = e.Item.ItemIndex;
    List<string> flag = new List<string>();
        flag.Add("Auto Fulfill");
        flag.Add("Do Not Auto Fulfill");
    AutoGenerateTable.DataSource = populateTable();
    AutoGenerateTable.DataBind();
    Literal text = e.Item.Cells[3].Controls[1] as Literal;
    // Get the row again now that we are in edit mode
    DataGridItem editItem = AutoGenerateTable.Items[e.Item.ItemIndex];
    DropDownList FlagFropDown = (DropDownList)editItem.FindControl("FlagFropDown");
    // ...
}
请使用

以下代码。希望对您有所帮助

  protected void AutoGenerateTable_EditCommand(object source, DataGridCommandEventArgs e)
        {
            // / Set the EditItemIndex property to the index of the item clicked 
            // in the DataGrid control to enable editing for that item. Be sure
            // to rebind the DateGrid to the data source to refresh the control.
            AutoGenerateTable.EditItemIndex = e.Item.ItemIndex;
            BindGrid();
        }

    protected void AutoGenerateTable_ItemDataBound(object sender, DataGridItemEventArgs e)
    {
        Literal FlagText = null;
        if (e.Item.ItemType == ListItemType.Item)
        {
            FlagText = e.Item.FindControl("FlagText") as Literal;
        }
        if (e.Item.ItemType == ListItemType.EditItem)
        {
            DropDownList FlagFropDown = e.Item.FindControl("FlagFropDown") as DropDownList;
            if (FlagText != null)
            {
                string value = FlagText.Text.Trim();
                if (FlagFropDown != null)
                {
                    //now you have got the object of dropdown
                    //If you want to do any opertaion on dropdown can write code for it.
                    FlagFropDown.Items.FindByText(value).Selected = true;
                }
            }
        }
    }