简体   繁体   中英

asp.net - How can I successfully update a gridview (or listview) that has been dynamically generated?

In the code behind I am collecting information from the database, and then with a loop, displaying that info in different tables. I would like to make it so the user of the site is able to update the "Users" and "Notes" fields for each row.

Portion of Environments.aspx:

<asp:ListView ID="ListView1" runat="server" ItemPlaceholderID="Panel1" 
                    OnItemDataBound="ListView_ItemDataBound" CellSpacing="2" 
                    CellPadding="2" ShowHeaderWhenEmpty="True">
                    <LayoutTemplate>
                        <asp:Panel runat="server" ID="Panel1"></asp:Panel>
                    </LayoutTemplate>
                    <ItemTemplate>
                        <br />
                        <%#Eval("Name") %> (Database: <%#Eval("Database") %>): 
                        <asp:UpdatePanel ID="UpdatePanel1" runat="server">
                        <ContentTemplate>
                        <asp:Timer ID="Timer1" runat="server" Interval="180000" ontick="Timer1_Tick">
                        </asp:Timer>
                        <asp:GridView ID="GridView1" runat="server" Width="800" CellPadding="2" CellSpacing="2" AutoGenerateColumns="False" >
                            <Columns>
                                <asp:HyperLinkField DataNavigateUrlFields="LinkAddress" DataTextField="ServerName" HeaderText="Server:" Target="_blank" ItemStyle-Width="200" />
                                <%--<asp:BoundField DataField="ServerName" HeaderText="Server:" />--%>
                                <asp:BoundField DataField="Environment" HeaderText="Environment" ItemStyle-Width="50" />
                                <asp:BoundField DataField="Users" HeaderText="Users" ItemStyle-Width="50" />
                                <asp:BoundField DataField="Build" HeaderText="Build" ItemStyle-Width="200" />
                                <asp:BoundField DataField="Notes" HeaderText="Notes" />
                                <asp:TemplateField HeaderText="Update" ItemStyle-Width="50">
                        <EditItemTemplate>
                        <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="True" CommandName="Update" 
                            Text="Update"></asp:LinkButton>
                        <asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" CommandName="Cancel"
                            Text="Cancel"></asp:LinkButton>
                        </EditItemTemplate>
                        <ItemTemplate>
                            <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" CommandName="EditRow" 
                                Text="Edit" Visible='<%# PrivilegedUser() %>'></asp:LinkButton>
                        </ItemTemplate>
                    </asp:TemplateField>
                            </Columns>
                        </asp:GridView>
                        </ContentTemplate>
                </asp:UpdatePanel>
                    </ItemTemplate>
                </asp:ListView>

Portion of Environments.aspx.cs:

protected void Page_Load(object sender, EventArgs e)
{
    ListView ListView1;
    ListView1 = (ListView)LoginView2.FindControl("ListView1");
    if (!IsPostBack)
    {
        ListView1.DataSource = Envs;
        ListView1.DataBind();
    }
}

protected void Timer1_Tick(object sender, EventArgs e)
{
    ListView ListView1;
    ListView1 = (ListView)LoginView2.FindControl("ListView1");
    if (!IsPostBack)
    {
        ListView1.DataSource = Envs;
        ListView1.DataBind();
    }
}

protected Boolean PrivilegedUser()
{
    SQLConnections SQL = new SQLConnections();
    bool IsPrivileged = SQL.IsUserPrivileged();
    return IsPrivileged;
}

public class Environment
{
    public int EnvId { get; set; }
    public string LineBreak { get; set; }
    public string Name { get; set; }
    public string Database { get; set; }
    public List<Svrs> Svrs { get; set; }
}

public class Svrs
{
    public int ServerId { get; set; }
    public string ServerName { get; set; }
    public string Environment { get; set; }
    public string Users { get; set; }
    public string Build { get; set; }
    public string Notes { get; set; }
    public string LinkAddress { get; set; }
}

public List<Environment> Envs
{
    get
    {
        string[] EnvList = GetEnvs();
        List<string> ServerList = GetServers();
        string[,] DBNamesAndServers = GetDBNamesAndServers();
        string[] DBServers = GetDataBaseServer(DBNamesAndServers);
        string[] DBNames = GetDataBaseName(DBNamesAndServers);
        int num = EnvList.Count();
        List<Environment> CompleteEnvList = new List<Environment>();
        List<Environment> CompleteServerList = new List<Environment>();
        List<Environment> CompleteTempList = new List<Environment>();
        int i = 0;
        int z = 1;

        while(i<num)
        {
            string envname = EnvList[i];
            string dbserver = "Unknown";
            int index = 0;
            if (DBNames.Contains(EnvList[i]))
            {
                index = Array.IndexOf(DBNames, EnvList[i]);
                dbserver = (DBServers[index]);
            }
            string[,] CurServerInfo = GetSvrs(ServerList, envname);
            List<Svrs> NewSvr = new List<Svrs>();
            NewSvr.AddRange(NewSvrInfo(CurServerInfo, z));
            Environment CurrentSvr = new Environment();
            Environment CurrentEnv = new Environment();

                CurrentSvr = new Environment
                    {
                        EnvId = i+1,
                        LineBreak = "",
                        Name = envname,
                        Database = dbserver,
                        Svrs = NewSvr,
                    };
                CompleteServerList.Add(CurrentSvr);

            CompleteEnvList.AddRange(CompleteServerList);
            i++;
            z = z + 10;
        }

        return CompleteServerList; //CompleteEnvList;
    }
}

At the moment, when I click on the "Edit" link, the page just refreshes. I need to be able to know the values of "Environment", "Users" and "Notes" for the row the user is updating, so I can actually update the database and have the table refreshed. Any help would be very appreciated.

You can use this , and this link.

You have to simply alter this all as per your requirement, that's it and you will gonna to achieve your goal....

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM