locked
Need help on this aspx page RRS feed

  • Question

  • User-1797368610 posted

    Hi,

    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

Answers

  • 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.

      

    aspx:

    <div>
                <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.">
                    <Columns>
                        <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" />
                    </Columns>
                </asp:GridView>
                <table border="0" cellpadding="0" cellspacing="0" style="border-collapse: collapse">
                    <tr>
                        <td style="padding-bottom: 10px">Name:<br />
                            <asp:TextBox ID="txtName" runat="server" />
                        </td>
                        <td style="padding-bottom: 10px">Email:<br />
                            <asp:TextBox ID="txtEmail" runat="server" />
                        </td>
                        <td style="padding-bottom: 10px">Address:<br />
                            <asp:TextBox ID="txtAddress" runat="server" />
                        </td>
                        <td style="padding-bottom: 10px">Phone Number:<br />
                            <asp:TextBox ID="txtPhone" runat="server" />
                        </td>
                        <td style="padding-bottom: 10px">Role:<br />
                            <asp:DropDownList ID="ddlRole" runat="server" />
                        </td>
                        <td style="width: 100px">
                            <asp:Button ID="btnAdd" runat="server" Text="Add" OnClick="btnAdd_Click" />
                        </td>
                    </tr>
                </table>
            </div>

    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;
                    BindGrid();
                    BindDDL();
                }
            }
    
            protected void BindGrid()
            {
                GridView1.DataSource = (DataTable)ViewState["Contacts"];
                GridView1.DataBind();
            }
    
            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";
                ddlRole.DataBind();
    
            }
    
            protected void btnAdd_Click(object sender, EventArgs e)
            {
                // Add the contact to the ViewState and update the grid view
                DataTable dt = (DataTable)ViewState["Contacts"];
                dt.Rows.Add(
                    txtName.Text.Trim(),
                    txtEmail.Text.Trim(),
                    txtAddress.Text.Trim(),
                    txtPhone.Text.Trim(),
                    ddlRole.SelectedItem.Text.Trim()
                    );
                ViewState["Contacts"] = dt;
                BindGrid();
    
                // 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
                BindDDL();
            }

    Demo:

     

     

    Hope helps.

    Best regards,

    Sean

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