Click here to Skip to main content
15,891,657 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
The problem is that i enter the value in the textboxes. when i press the save button then the in the textboxes show null value...id and name are boundfield which show the value...but textbox always shows the null...
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Data;
using System.Web.UI.WebControls;
using IITBAL;
using IITDAL;

public partial class Faculty_Manage_Results : System.Web.UI.Page
{   
    public string SessionId = null;
    public string SessionPassword = null;
  
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["id"] == null || Session["password"] == null)
        {
            Response.Redirect("Admin Login.aspx");
        }
        else
        {
            SessionId = Session["id"].ToString();
            SessionPassword = Session["password"].ToString();
            GridView1.Columns.Clear();
        }

        if (IsPostBack)
        {

        }

        if (!IsPostBack)
        {
            // LoadRegisteredSemesters();
            LoadAssignCoursesName();      
        }

    }
    protected void LoadAssignCoursesName()
    {
        DataTable dt = new DataTable();
        dt = IITBAL.Courses.GetAssignCoursesName(SessionId);
        Course_Name.DataSource = dt;
        Course_Name.DataTextField = "CourseName";
        Course_Name.DataBind();
    }

    protected void Course_Name_TextChanged(object sender, EventArgs e)
    {
        Semester.Items.Clear();
        Semester.Items.Insert(0, new ListItem("--Select--", ""));

        string ID = Course_Name.Text;
        DataTable DT = new DataTable();
        DT = IITBAL.Courses.GetAssignSemesterName(SessionId, ID);
        Semester.DataSource = DT;
        Semester.DataTextField = "Semester";
        Semester.DataBind();
        if (Semester.Items.Count > 1)
        {
            Semester.Enabled = true;
        }
        else
        {
            Semester.Enabled = false;
        }
        DataTable finaldt = new DataTable();
        finaldt = LoadStudentsIdAndName();
        int index = 0;
        foreach (DataColumn col in finaldt.Columns)
        {
            BoundField bfield = new BoundField();

            bfield.DataField = col.ColumnName;

            bfield.HeaderText = col.ColumnName;

            GridView1.Columns.Insert(index, bfield);
            index++;
        }


        //GridView1.Columns.Insert(index, finaldt);

        int quizes = LoadDesignCourseQuizez();

        for (int count = 1; count <= quizes; count++)
        {
            var tfield = new TemplateField();
            tfield.ItemTemplate = new TextBoxColumnQ();
            tfield.HeaderText = "Q" + count;
            GridView1.Columns.Insert(index, tfield);
            index++;
        }
        int assignments = LoadDesignCourseAssignments();
        for (int count = 1; count <= assignments; count++)
        {
            var tfield = new TemplateField();
            tfield.ItemTemplate = new TextBoxColumnA();
            tfield.HeaderText = "A" + count;
            GridView1.Columns.Insert(index, tfield);
            index++;

        }
        int sessionals = LoadDesignCourseSessionals();
        for (int count = 1; count <= sessionals; count++)
        {
            var tfield = new TemplateField();
            tfield.ItemTemplate = new TextBoxColumnS();
            tfield.HeaderText = "S" + count;
            GridView1.Columns.Insert(index, tfield);
            index++;
        }
        int terminal = 1;
        for (int count = 0; count < terminal; count++)
        {
            var tfield = new TemplateField();
            tfield.ItemTemplate = new TextBoxColumnTer();
            tfield.HeaderText = "Terminal";
            GridView1.Columns.Insert(index, tfield);
            index++;
        }
        int total = 1;
        for (int count = 0; count < total; count++)
        {
            var tfield = new TemplateField();
            tfield.ItemTemplate = new TextBoxColumnTot();
            tfield.HeaderText = "Total";
            GridView1.Columns.Insert(index, tfield);
            index++;
        }

        int edit = 1;
        for (int count = 0; count < edit; count++)
        {
            var templatefield = new TemplateField();
            templatefield.ItemTemplate = new LinkButtonColumn();
            templatefield.HeaderText = "Edit";
            GridView1.Columns.Insert(index, templatefield);
            index++;
        }

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


    protected DataTable LoadStudentsIdAndName()
    {
        string semester = Semester.Text.ToString();
        string coursename = Course_Name.Text.ToString();
        DataTable dt = new DataTable();
        dt = IITBAL.Results.GetStudentIdAndName(SessionId, semester, coursename);
        return dt;
    }
    private int LoadDesignCourseQuizez()
    {
        string semester = Semester.Text.ToString();
        string coursename = Course_Name.Text.ToString();
        DataTable dt = new DataTable();
        dt = IITBAL.FacultyCourses.GetDesignCourseQuizez(SessionId, coursename, semester);
        int quizez = Convert.ToInt32(dt.Rows[0]["Quizez"].ToString());
        return quizez;
    }
    private int LoadDesignCourseAssignments()
    {
        string semester = Semester.Text.ToString();
        string coursename = Course_Name.Text.ToString();
        DataTable dt = new DataTable();
        dt = IITBAL.FacultyCourses.GetDesignCourseAssignments(SessionId, coursename, semester);
        int assignments = Convert.ToInt32(dt.Rows[0]["Assignments"].ToString());
        return assignments;

    }
    private int LoadDesignCourseSessionals()
    {
        string semester = Semester.Text.ToString();
        string coursename = Course_Name.Text.ToString();
        DataTable dt = new DataTable();
        dt = IITBAL.FacultyCourses.GetDesignCourseSessionals(SessionId, coursename, semester);
        int sessionals = Convert.ToInt32(dt.Rows[0]["Sessionals"].ToString());
        return sessionals;
    }
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        LoadStudentsIdAndName();
        GridView1.PageIndex = e.NewPageIndex;
        GridView1.DataBind();
    }

    protected void btn_click_Click(object sender, EventArgs e)
    {
        Response.Redirect("FacultyMainPage.aspx");

    }
    class TextBoxColumnQ : ITemplate
    {
        public void InstantiateIn(System.Web.UI.Control container)
        {
            TextBox txtBox = new TextBox();
            txtBox.ID = "txtBoxQ";

            txtBox.Width = 30;
            txtBox.MaxLength = 4;

            container.Controls.Add(txtBox);
        }
    }
    class TextBoxColumnA : ITemplate
    {
        public void InstantiateIn(System.Web.UI.Control container)
        {
            TextBox txtBox = new TextBox();
            txtBox.ID = "txtBoxA";
            txtBox.Width = 30;
            txtBox.MaxLength = 4;

            container.Controls.Add(txtBox);
        }
    }

    class TextBoxColumnS : ITemplate
    {
        public void InstantiateIn(System.Web.UI.Control container)
        {
            TextBox txtBox = new TextBox();
            txtBox.ID = "txtBoxS";
            txtBox.Width = 30;
            txtBox.MaxLength = 4;

            container.Controls.Add(txtBox);
        }
    }
    class TextBoxColumnTer : ITemplate
    {
        public void InstantiateIn(System.Web.UI.Control container)
        {
            TextBox txtBox = new TextBox();
            txtBox.ID = "txtBoxTer";
            txtBox.Width = 30;
            txtBox.MaxLength = 4;

            container.Controls.Add(txtBox);
        }
    }
    class TextBoxColumnTot : ITemplate
    {
        public void InstantiateIn(System.Web.UI.Control container)
        {
            TextBox txtBox = new TextBox();
            txtBox.ID = "txtBoxTot";
            txtBox.Width = 30;
            txtBox.MaxLength = 4;

            container.Controls.Add(txtBox);
        }
    }
    class LinkButtonColumn : ITemplate
    {
        public void InstantiateIn(System.Web.UI.Control container)
        {
            LinkButton edit = new LinkButton();
            edit.Text = "Edit";
            edit.CommandName = "Edit";
            //edit.CommandArgument=edit.CommandArgument'<#Eval("CourseCode")>';
            edit.ID = "btn_edit";

            container.Controls.Add(edit);
        }
    }

Problem is here below
C#
protected void btn_save_Click(object sender, EventArgs e)
{
    foreach (GridViewRow row in GridView1.Rows)
    {
        if (row.RowType == DataControlRowType.DataRow)
        {
            string Id = row.Cells[0].Text;
            string name = row.Cells[1].Text;
            TextBox textBoxq = row.FindControl("txtBoxQ") as TextBox;
            TextBox textBoxa = row.FindControl("txtBoxA") as TextBox;
            TextBox textBoxs = row.FindControl("txtBoxS") as TextBox;
            TextBox textBoxt = row.FindControl("txtBoxTer") as TextBox;
            TextBox textBoxtot = row.FindControl("txtBoxTot") as TextBox;
        }                  
    }
}

when i enter the value in textboxes and press the save button then in the code these
textboxes shows the null value only id and name shows the value.


[Edit member="Tadit"]
Corrected formatting and/or grammatical issues.
Added pre tags.
[/Edit]
Posted
v4
Comments
[no name] 2-Oct-14 15:01pm    
I see an unformatted way too much code dump but no question.
ZurdoDev 2-Oct-14 15:40pm    
I hope you do not expect someone to look through all that code for you. Please debug the issue and figure out where the problem is and then narrow down your question. You'll likely answer your own question by doing so.

1 solution

You are dynamically adding columns inside the Course_Name_TextChanged Event.

So, the problem is that, when you click on the Button, these information is lost. What you have to do is, just add the columns again in PostBack.

Dynamic controls or columns need to be recreated on every page load, because of the way that controls work. Dynamic controls do not get retained so you have to reload them on every page PostBack; however, ViewState will be retained for these controls.

For that, make one method and transfer all the GridView related code from that event to the method. Call that method from Event and Page Load both.
 
Share this answer
 
Comments
mudi lala 3-Oct-14 7:55am    
Tadit Dash you give ma a amazing solution
Glad to hear that. Thanks. :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900