0

I have this code in my code behind.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    public class fields
    {
        public string firstname { get; set; }
        public string middlename { get; set; }
        public string lastname { get; set; }
        public string phonenumber { get; set; }

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        List<fields> data = new List<fields>();

        string fn = TextBox1.Text;
        string mn = TextBox3.Text;
        string ln = TextBox2.Text;
        string pn = TextBox4.Text;

        fields s = new fields();
        s.firstname = TextBox1.Text;
        s.middlename = TextBox3.Text;
        s.lastname = TextBox2.Text;
        s.phonenumber = TextBox4.Text;

        data.Add(s);

        GridView1.DataSource = data;
        GridView1.DataBind();



    }
}

I have four textboxes in my view. I am trying to show in the grid those data when I click submit button my page. But I am not able to show the data in the grid.

can any body help me out.

1 Answer 1

1

Use this:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

[Serializable]
public class fields
{
    public string firstname { get; set; }
    public string middlename { get; set; }
    public string lastname { get; set; }
    public string phonenumber { get; set; }

}

public partial class _Default : System.Web.UI.Page
{
    List<fields> data = null;

    protected void Page_Load(object sender, EventArgs e)
    {
        data = ViewState["_data"] as List<fields>;
        if (data == null) 
            data = new List<fields>();
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        string fn = TextBox1.Text;
        string mn = TextBox3.Text;
        string ln = TextBox2.Text;
        string pn = TextBox4.Text;

        fields s = new fields();
        s.firstname = TextBox1.Text;
        s.middlename = TextBox3.Text;
        s.lastname = TextBox2.Text;
        s.phonenumber = TextBox4.Text;

        data.Add(s);
        ViewState["_data"] = data;

        GridView1.DataSource = data;
        GridView1.DataBind();
    }
}

Hope it works!

Sign up to request clarification or add additional context in comments.

6 Comments

I am getting this error. Type '_Default+fields' in Assembly 'App_Web_3zhqnips, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' is not marked as serializable. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.Runtime.Serialization.SerializationException: Type '_Default+fields' in Assembly 'App_Web_3zhqnips, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' is not marked as serializable.
You have to mark fields class as Serializable. I have updated the code above. Please check
Yes I did that Abdul, but problem is I am not seeing the data in the grid. how ever I can see row by row increasing when I click submit button my page. I am not data in the rows.
I don't see any reason for not working! Are you sure you tested my updated code?
Thank you very much for your guidance. I succeed in using your coding. But only one row is persistent all previous rows are deleted. How to do it?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.