Need help on this aspx page RRS feed

  • Question

  • User-1797368610 posted


    i am trying create a asp page where users will be able to insert certain items. Please Click here to see  the requirement for better understanding . on the page, there will be few fields to enter the values but tricky part is users should have ability to add multiple contacts for same agent at the same time. Based on requirement, what should be the way to achieve it? 

    Thanks in Advance!!

    Thursday, February 25, 2021 12:01 AM


  • User-1330468790 posted

    Hi Rakib1, 


    Regarding the functionality of adding multiple contacts, I think you'd better use a grid view control and separate the inputs in another place so that it would be easier for value validation. The key is to use ViewState to store the temp contacts info before submitting to the database. Everytime you add a new contact, it will be added in this temp storage.

    If you want to submit the whole records to database, you could simply fetch the contacts info from the variable ViewState["Contacts"].

    You could try below demo which focus on the tricky part - adding multiple contacts.



                <h2>Add Multiple Contacts for User</h2>
                <asp:Label ID="addContactsLabel" runat="server"></asp:Label>
                <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false"
                    EmptyDataText="No records has been added.">
                        <asp:BoundField DataField="Name" HeaderText="Name" ItemStyle-Width="120" />
                        <asp:BoundField DataField="Email" HeaderText="Country" ItemStyle-Width="120" />
                        <asp:BoundField DataField="Address" HeaderText="Address" ItemStyle-Width="120" />
                        <asp:BoundField DataField="Phone" HeaderText="Phone Number" ItemStyle-Width="120" />
                        <asp:BoundField DataField="Role" HeaderText="Role" ItemStyle-Width="120" />
                <table border="0" cellpadding="0" cellspacing="0" style="border-collapse: collapse">
                        <td style="padding-bottom: 10px">Name:<br />
                            <asp:TextBox ID="txtName" runat="server" />
                        <td style="padding-bottom: 10px">Email:<br />
                            <asp:TextBox ID="txtEmail" runat="server" />
                        <td style="padding-bottom: 10px">Address:<br />
                            <asp:TextBox ID="txtAddress" runat="server" />
                        <td style="padding-bottom: 10px">Phone Number:<br />
                            <asp:TextBox ID="txtPhone" runat="server" />
                        <td style="padding-bottom: 10px">Role:<br />
                            <asp:DropDownList ID="ddlRole" runat="server" />
                        <td style="width: 100px">
                            <asp:Button ID="btnAdd" runat="server" Text="Add" OnClick="btnAdd_Click" />

    Code behind:

    protected void Page_Load(object sender, EventArgs e)
                if (!IsPostBack)
                    DataTable dt = new DataTable();
                    dt.Columns.AddRange(new DataColumn[5] { 
                        new DataColumn("Name"), 
                        new DataColumn("Email"),
                        new DataColumn("Address"),
                        new DataColumn("Phone"),
                        new DataColumn("Role")
                    ViewState["Contacts"] = dt;
            protected void BindGrid()
                GridView1.DataSource = (DataTable)ViewState["Contacts"];
            protected void BindDDL()
                // Here you can fetch the values from role database
                DataTable roleDT = new DataTable();
                roleDT.Columns.AddRange(new DataColumn[2] {
                        new DataColumn("Text"),
                        new DataColumn("Value")
                roleDT.Rows.Add("Account Manager", "Account Manager");
                roleDT.Rows.Add("Executive", "Executive");
                roleDT.Rows.Add("Sales", "Sales");
                roleDT.Rows.Add("Support", "Support");
                ddlRole.DataSource = roleDT;
                ddlRole.DataTextField = "Text";
                ddlRole.DataValueField = "Value";
            protected void btnAdd_Click(object sender, EventArgs e)
                // Add the contact to the ViewState and update the grid view
                DataTable dt = (DataTable)ViewState["Contacts"];
                ViewState["Contacts"] = dt;
                // Restore the inputs to the original state
                txtName.Text = string.Empty;
                txtEmail.Text = string.Empty;
                txtAddress.Text = string.Empty;
                txtPhone.Text = string.Empty;
                // Restore the state of the drop down list




    Hope helps.

    Best regards,


    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, February 25, 2021 2:31 AM