Display Updated values in another View of ASP.Net MultiView control using C# and VB.Net

Last Reply 26 days ago By dharmendr

Posted 29 days ago

I've the web form that when the user selects name from the dropdown list, the values of that name populate in the textboxes and at the same time its values show in the Review section. This part currently is working. If the user makes any edits in those texbox(es), the new edit/value(s) should be appearing in the Review section automacally . Is it possible to do that? If so, would you help how to do that? Thank you very much.

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="e_preview_Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">

        <asp:ScriptManager runat="server" ID='sm1' EnablePartialRendering="false"></asp:ScriptManager>

        <asp:UpdatePanel runat="server" ID='up1'>
            <ContentTemplate>
                <asp:MultiView ID="mvPeview" runat="server" ActiveViewIndex="0" EnableTheming="True">
                    <asp:View ID="vwSubmit" runat="server">
                        Select Customer Name
                        <asp:DropDownList ID="lstFullName" runat="server" AutoPostBack="true" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
                        </asp:DropDownList>
                        <br />

                        Name: 
                        <asp:TextBox ID="txtName" runat="server"></asp:TextBox>
                        <br />
                        Department:
                        <asp:TextBox ID="txtDepartment" runat="server"></asp:TextBox>
                        <br />                     
                        Phone:
                        <asp:TextBox ID="txtPhone" runat="server"></asp:TextBox>
                        <br />
                        Email:
                        <asp:TextBox ID="txtEmail" runat="server"></asp:TextBox>
                        <br />
                        Order:
                        <asp:TextBox ID="txtOrder" runat="server"></asp:TextBox>
                        Count: <asp:TextBox ID="txtCount" runat="server"></asp:TextBox>
                        <br />
                        <hr />

                        Name: <asp:Label ID="lblName" runat="server"></asp:Label>
                        <br />                       
                        <asp:Label ID="lblDepartment" runat="server"></asp:Label>
                        <br />
                        <asp:Label ID="lblEmail" runat="server"></asp:Label></a>
                        <br />
                        <asp:Label ID="lblPhone" runat="server"></asp:Label></span>
                        <br />
                         <asp:Label ID="lblOrder" runat="server"></asp:Label> 
                        and <asp:Label ID="lblCount" runat="server"></asp:Label>
                        <br />
                    </asp:View>

                    <asp:View ID="vwResult" runat="server">
                    </asp:View>
                </asp:MultiView>

            </ContentTemplate>
        </asp:UpdatePanel>
    </form>
</body>
</html>

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.Configuration;
using System.Data.SqlClient;
using System.Web.UI.WebControls;
using System.Data;
using System.Configuration;
using ConfigurationSettings = System.Configuration.ConfigurationManager;

public partial class e_preview_Default :  System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            lstFullName.Items.Add(new ListItem("Select CustDisplayname", ""));
            lstFullName.AppendDataBoundItems = true;

            String strConnString = ConfigurationManager.ConnectionStrings["ConnString"].ConnectionString;


            String strQuery = "SELECT *  FROM CusContactlist";
            SqlConnection con = new SqlConnection(strConnString);
            SqlCommand cmd = new SqlCommand();
            cmd.CommandType = CommandType.Text;

            cmd.CommandText = strQuery;
            cmd.Connection = con;
            try
            {
                con.Open();
                lstFullName.DataSource = cmd.ExecuteReader();
                lstFullName.DataTextField = "CustDisplayname";
                lstFullName.DataValueField = "CustDisplayname";
                lstFullName.DataBind();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                con.Close();
                con.Dispose();
            }
        }
    }

    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        String strConnString = ConfigurationManager.ConnectionStrings["ConnString"].ConnectionString;
        String strQuery = "SELECT * FROM CusContactlist WHERE CustDisplayname = @CustDisplayname";

        SqlConnection con = new SqlConnection(strConnString);
        SqlCommand cmd = new SqlCommand();

        cmd.Parameters.AddWithValue("@CustDisplayname", lstFullName.SelectedItem.Value);
        cmd.CommandType = CommandType.Text;
        cmd.CommandText = strQuery;
        cmd.Connection = con;
        try
        {
            con.Open();
            SqlDataReader sdr = cmd.ExecuteReader();
            while (sdr.Read())
            {
                txtName.Text = sdr["CustDisplayname"].ToString();
                txtDepartment.Text = sdr["dept_list"].ToString();
                txtEmail.Text = sdr["email"].ToString();
                txtPhone.Text = sdr["telNumber"].ToString();
                txtOrder.Text = sdr["numberOrder"].ToString();
                txtCount.Text = sdr["Count"].ToString();

                lblName.Text = sdr["CustDisplayname"].ToString();
                lblDepartment.Text = sdr["dept_list"].ToString();
                lblEmail.Text = sdr["email"].ToString();
                lblPhone.Text = sdr["telNumber"].ToString();
                txtOrder.Text = sdr["numberOrder"].ToString();
                lblCount.Text = sdr["Count"].ToString();
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
        finally
        {
            con.Close();
            con.Dispose();
        }
    }


}

 

You are viewing reply posted by: dharmendr 26 days ago.
Posted 26 days ago

Hi Thuyai,

Check this example. Now please take its reference and correct your code.

Here i have used dynamic datatable to populate the record. You need to replace the datatable binding code from database.

HTML

<asp:ScriptManager runat="server" ID='sm1' EnablePartialRendering="false">
</asp:ScriptManager>
<asp:UpdatePanel runat="server" ID='up1'>
    <ContentTemplate>
        <asp:MultiView ID="mvPeview" runat="server" ActiveViewIndex="0" EnableTheming="True">
            <asp:View ID="vwSubmit" runat="server">
                Select Customer Name
                <asp:DropDownList ID="lstFullName" runat="server" AutoPostBack="true" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
                </asp:DropDownList>
                <br />
                Name:
                <asp:TextBox ID="txtName" runat="server" ReadOnly="true"></asp:TextBox>
                <br />
                Department:
                <asp:TextBox ID="txtDepartment" runat="server" OnTextChanged="UpdateCustomerList"
                    AutoPostBack="true"></asp:TextBox>
                <br />
                Phone:
                <asp:TextBox ID="txtPhone" runat="server" OnTextChanged="UpdateCustomerList" AutoPostBack="true"></asp:TextBox>
                <br />
                Email:
                <asp:TextBox ID="txtEmail" runat="server" OnTextChanged="UpdateCustomerList" AutoPostBack="true"></asp:TextBox>
                <br />
                Order:
                <asp:TextBox ID="txtOrder" runat="server" OnTextChanged="UpdateCustomerList" AutoPostBack="true"></asp:TextBox>
                Count:
                <asp:TextBox ID="txtCount" runat="server" OnTextChanged="UpdateCustomerList" AutoPostBack="true"></asp:TextBox>
                <br />
                <hr />
                Name:
                <asp:Label ID="lblName" runat="server"></asp:Label>
                <br />
                <asp:Label ID="lblDepartment" runat="server"></asp:Label>
                <br />
                <asp:Label ID="lblEmail" runat="server"></asp:Label>
                <br />
                <asp:Label ID="lblPhone" runat="server"></asp:Label></span>
                <br />
                <asp:Label ID="lblOrder" runat="server"></asp:Label>
                and
                <asp:Label ID="lblCount" runat="server"></asp:Label>
                <br />
            </asp:View>
            <asp:View ID="vwResult" runat="server">
            </asp:View>
        </asp:MultiView>
    </ContentTemplate>
</asp:UpdatePanel>

Namespaces

C#

using System.Data;

VB.Bet

Imports System.Data

Code

C#

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        lstFullName.Items.Add(new ListItem("Select CustDisplayname", ""));
        lstFullName.AppendDataBoundItems = true;
        DataTable dt = GetCustomerContactList();
        lstFullName.DataSource = dt;
        lstFullName.DataTextField = "CustDisplayname";
        lstFullName.DataValueField = "CustDisplayname";
        lstFullName.DataBind();
    }
}

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
    DataTable dt = GetCustomerContactList().Select("CustDisplayname='" + lstFullName.SelectedItem.Value + "'").CopyToDataTable();

    txtName.Text = dt.Rows[0]["CustDisplayname"].ToString();
    txtDepartment.Text = dt.Rows[0]["dept_list"].ToString();
    txtEmail.Text = dt.Rows[0]["email"].ToString();
    txtPhone.Text = dt.Rows[0]["telNumber"].ToString();
    txtOrder.Text = dt.Rows[0]["numberOrder"].ToString();
    txtCount.Text = dt.Rows[0]["Count"].ToString();

    lblName.Text = dt.Rows[0]["CustDisplayname"].ToString();
    lblDepartment.Text = dt.Rows[0]["dept_list"].ToString();
    lblEmail.Text = dt.Rows[0]["email"].ToString();
    lblPhone.Text = dt.Rows[0]["telNumber"].ToString();
    lblOrder.Text = dt.Rows[0]["numberOrder"].ToString();
    lblCount.Text = dt.Rows[0]["Count"].ToString();
}

protected void UpdateCustomerList(object sender, EventArgs e)
{
    string department = txtDepartment.Text.Trim();
    string email = txtEmail.Text.Trim();
    string phone = txtPhone.Text.Trim();
    string order = txtOrder.Text.Trim();
    string count = txtCount.Text.Trim();

    DataTable dtCustomer = Session["CustomerList"] as DataTable;

    foreach (DataRow dr in dtCustomer.Rows)
    {
        if (dr["CustDisplayname"].ToString() == lstFullName.SelectedItem.Value)
        {
            dr["dept_list"] = department;
            dr["email"] = email;
            dr["telNumber"] = phone;
            dr["numberOrder"] = order;
            dr["Count"] = count;
        }
    }
    Session["CustomerList"] = dtCustomer;

    DataTable dt = (Session["CustomerList"] as DataTable).Select("CustDisplayname='" + lstFullName.SelectedItem.Value + "'").CopyToDataTable();
    lblName.Text = dt.Rows[0]["CustDisplayname"].ToString();
    lblDepartment.Text = dt.Rows[0]["dept_list"].ToString();
    lblEmail.Text = dt.Rows[0]["email"].ToString();
    lblPhone.Text = dt.Rows[0]["telNumber"].ToString();
    lblOrder.Text = dt.Rows[0]["numberOrder"].ToString();
    lblCount.Text = dt.Rows[0]["Count"].ToString();
}

private DataTable GetCustomerContactList()
{
    DataTable dt;
    if (Session["CustomerList"] == null)
    {
        dt = new DataTable();
        dt.Columns.AddRange(new DataColumn[] { 
            new DataColumn("CustDisplayname", typeof(string)),
            new DataColumn("dept_list", typeof(string)),
            new DataColumn("email", typeof(string)),
            new DataColumn("telNumber", typeof(Int64)),
            new DataColumn("numberOrder", typeof(int)),
            new DataColumn("Count", typeof(int)) });
        dt.Rows.Add("John Hammond", "Computer", "test@test.com", 1111, 1001, 8);
        dt.Rows.Add("Mudassar Khan", "IT", "test@test.com", 2222, 1002, 5);
        dt.Rows.Add("Suzanne Mathews", "IT", "test@test.com", 3333, 1003, 10);
        dt.Rows.Add("Robert Schidner", "Computer", "test@test.com", 4444, 1004, 2);
        Session["CustomerList"] = dt;
    }
    else
    {
        dt = Session["CustomerList"] as DataTable;
    }

    return dt;
}

VB.Net

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    If Not IsPostBack Then
        lstFullName.Items.Add(New ListItem("Select CustDisplayname", ""))
        lstFullName.AppendDataBoundItems = True
        Dim dt As DataTable = GetCustomerContactList()
        lstFullName.DataSource = dt
        lstFullName.DataTextField = "CustDisplayname"
        lstFullName.DataValueField = "CustDisplayname"
        lstFullName.DataBind()
    End If
End Sub

Protected Sub DropDownList1_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
    Dim dt As DataTable = GetCustomerContactList().Select("CustDisplayname='" & lstFullName.SelectedItem.Value & "'").CopyToDataTable()
    txtName.Text = dt.Rows(0)("CustDisplayname").ToString()
    txtDepartment.Text = dt.Rows(0)("dept_list").ToString()
    txtEmail.Text = dt.Rows(0)("email").ToString()
    txtPhone.Text = dt.Rows(0)("telNumber").ToString()
    txtOrder.Text = dt.Rows(0)("numberOrder").ToString()
    txtCount.Text = dt.Rows(0)("Count").ToString()
    lblName.Text = dt.Rows(0)("CustDisplayname").ToString()
    lblDepartment.Text = dt.Rows(0)("dept_list").ToString()
    lblEmail.Text = dt.Rows(0)("email").ToString()
    lblPhone.Text = dt.Rows(0)("telNumber").ToString()
    lblOrder.Text = dt.Rows(0)("numberOrder").ToString()
    lblCount.Text = dt.Rows(0)("Count").ToString()
End Sub

Protected Sub UpdateCustomerList(ByVal sender As Object, ByVal e As EventArgs)
    Dim department As String = txtDepartment.Text.Trim()
    Dim email As String = txtEmail.Text.Trim()
    Dim phone As String = txtPhone.Text.Trim()
    Dim order As String = txtOrder.Text.Trim()
    Dim count As String = txtCount.Text.Trim()
    Dim dtCustomer As DataTable = TryCast(Session("CustomerList"), DataTable)

    For Each dr As DataRow In dtCustomer.Rows

        If dr("CustDisplayname").ToString() = lstFullName.SelectedItem.Value Then
            dr("dept_list") = department
            dr("email") = email
            dr("telNumber") = phone
            dr("numberOrder") = order
            dr("Count") = count
        End If
    Next

    Session("CustomerList") = dtCustomer
    Dim dt As DataTable = (TryCast(Session("CustomerList"), DataTable)) _
                        .Select("CustDisplayname='" & lstFullName.SelectedItem.Value & "'").CopyToDataTable()
    lblName.Text = dt.Rows(0)("CustDisplayname").ToString()
    lblDepartment.Text = dt.Rows(0)("dept_list").ToString()
    lblEmail.Text = dt.Rows(0)("email").ToString()
    lblPhone.Text = dt.Rows(0)("telNumber").ToString()
    lblOrder.Text = dt.Rows(0)("numberOrder").ToString()
    lblCount.Text = dt.Rows(0)("Count").ToString()
End Sub

Private Function GetCustomerContactList() As DataTable
    Dim dt As DataTable

    If Session("CustomerList") Is Nothing Then
        dt = New DataTable()
        dt.Columns.AddRange(New DataColumn() {New DataColumn("CustDisplayname", GetType(String)),
                                              New DataColumn("dept_list", GetType(String)),
                                              New DataColumn("email", GetType(String)),
                                              New DataColumn("telNumber", GetType(Int64)),
                                              New DataColumn("numberOrder", GetType(Integer)),
                                              New DataColumn("Count", GetType(Integer))})
        dt.Rows.Add("John Hammond", "Computer", "test@test.com", 1111, 1001, 8)
        dt.Rows.Add("Mudassar Khan", "IT", "test@test.com", 2222, 1002, 5)
        dt.Rows.Add("Suzanne Mathews", "IT", "test@test.com", 3333, 1003, 10)
        dt.Rows.Add("Robert Schidner", "Computer", "test@test.com", 4444, 1004, 2)
        Session("CustomerList") = dt
    Else
        dt = TryCast(Session("CustomerList"), DataTable)
    End If

    Return dt
End Function

Screenshot