关于Asp.net GridView的问题 [英] Question about Asp.net GridView

查看:60
本文介绍了关于Asp.net GridView的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题是我在文本框中输入了值。当我按下保存按钮,然后在文本框显示空值... id 和名称是绑定字段,显示值...但文本框始终显示 null ...

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

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

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;
        }                  
    }
}



当我输入值时 textboxes 并按下保存按钮然后在代码中这些

文本框仅显示空值 id 和name显示值。







更正了格式和/或语法问题。

已添加 pre 标签。

[/编辑]


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.



Corrected formatting and/or grammatical issues.
Added pre tags.
[/Edit]

推荐答案

你是在 Course_Name_TextChanged 事件中动态添加列。



所以问题是,当你点击按钮,这些信息丢失了。你需要做的是,只需在 PostBack 中再次添加列。



动态控件或列需要因为控制工作的方式,在每个页面加载时重新创建。动态控件不会被保留,因此您必须在每个页面上重新加载它们 PostBack ;但是, ViewState 将保留用于这些控件。



为此,制作一个方法并转移所有 GridView 从该事件到该方法的相关代码。从Event和 Page Load 调用该方法。
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.


这篇关于关于Asp.net GridView的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆