0

我有 2 页(Trial.aspx 和 TestCallBack.aspx)。Trail.aspx 将首先处于可编辑模式。现在从 TestCallBack.aspx 我将重定向回 Trail.aspx 但所有控件都应该是只读格式。我在下面给出了源代码详细信息。只有trial.aspx的文本框是只读的,根本看不到gridview。

试用版.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Trial.aspx.cs" Inherits="PTest.Trial" EnableEventValidation ="true" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>  
<asp:TextBox ID="txtVisitor" runat="server"></asp:TextBox>
       <asp:GridView ID="Gridview1" runat="server" ShowFooter="true" AutoGenerateColumns="false" ShowHeaderWhenEmpty ="true" Visible ="true">
            <Columns>
                <asp:BoundField DataField="RowNumber" HeaderText="Row Number" />
                <asp:TemplateField HeaderText="Header 1">
                    <ItemTemplate>
                        <asp:TextBox ID="txtDate" runat="server"></asp:TextBox>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Header 2">
                    <ItemTemplate>
                        <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Header 3">
                    <ItemTemplate>
                        <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
                    </ItemTemplate>
                    <FooterStyle HorizontalAlign="Right" />
                    <FooterTemplate>
                        <asp:Button ID="ButtonAdd" runat="server" Text="Add New Row" OnClick="ButtonAdd_Click" />
                    </FooterTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
    </div>
    </form>
</body>
</html>

试用版.aspx.cs

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


namespace PTest
{
    public partial class Trial : System.Web.UI.Page
    {
        private void SetInitialRow()
        {
            DataTable dt = new DataTable();
            DataRow dr = null;
            dt.Columns.Add(new DataColumn("RowNumber", typeof(string)));
            dt.Columns.Add(new DataColumn("Column1", typeof(string)));
            dt.Columns.Add(new DataColumn("Column2", typeof(string)));
            dt.Columns.Add(new DataColumn("Column3", typeof(string)));
            dr = dt.NewRow();
            dr["RowNumber"] = 1;
            dr["Column1"] = string.Empty;
            dr["Column2"] = string.Empty;
            dr["Column3"] = string.Empty;
            dt.Rows.Add(dr);

            //Store the DataTable in ViewState
            ViewState["CurrentTable"] = dt;

            Gridview1.DataSource = dt;
            Gridview1.DataBind();
        }
        private void AddNewRowToGrid()
        {
            int rowIndex = 0;

            if (ViewState["CurrentTable"] != null)
            {
                DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"];
                DataRow drCurrentRow = null;
                if (dtCurrentTable.Rows.Count > 0)
                {
                    for (int i = 1; i <= dtCurrentTable.Rows.Count; i++)
                    {
                        //extract the TextBox values
                        TextBox box1 = (TextBox)Gridview1.Rows[rowIndex].Cells[1].FindControl("txtDate");
                        TextBox box2 = (TextBox)Gridview1.Rows[rowIndex].Cells[2].FindControl("TextBox2");
                        TextBox box3 = (TextBox)Gridview1.Rows[rowIndex].Cells[3].FindControl("TextBox3");

                        drCurrentRow = dtCurrentTable.NewRow();
                        drCurrentRow["RowNumber"] = i + 1;

                        dtCurrentTable.Rows[i - 1]["Column1"] = box1.Text;
                        dtCurrentTable.Rows[i - 1]["Column2"] = box2.Text;
                        dtCurrentTable.Rows[i - 1]["Column3"] = box3.Text;

                        rowIndex++;
                    }
                    dtCurrentTable.Rows.Add(drCurrentRow);
                    ViewState["CurrentTable"] = dtCurrentTable;

                    Gridview1.DataSource = dtCurrentTable;
                    Gridview1.DataBind();
                }
            }
            else
            {
                Response.Write("ViewState is null");
            }

            //Set Previous Data on Postbacks
            SetPreviousData();
        }
        private void SetPreviousData()
        {
            int rowIndex = 0;
            if (ViewState["CurrentTable"] != null)
            {
                DataTable dt = (DataTable)ViewState["CurrentTable"];
                if (dt.Rows.Count > 0)
                {
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        TextBox box1 = (TextBox)Gridview1.Rows[rowIndex].Cells[1].FindControl("txtDate");
                        TextBox box2 = (TextBox)Gridview1.Rows[rowIndex].Cells[2].FindControl("TextBox2");
                        TextBox box3 = (TextBox)Gridview1.Rows[rowIndex].Cells[3].FindControl("TextBox3");

                        box1.Text = dt.Rows[i]["Column1"].ToString();
                        box2.Text = dt.Rows[i]["Column2"].ToString();
                        box3.Text = dt.Rows[i]["Column3"].ToString();

                        rowIndex++;
                    }
                }
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                string name = Request.QueryString["value"];
                if (name == "true")
                {
                hidecontrols();
                }
                else 
                {

                SetInitialRow();
                }
            }
        }
        protected void ButtonAdd_Click(object sender, EventArgs e)
        {
            AddNewRowToGrid();
        }
        protected void hidecontrols()
        {
            foreach (Control c in Page.Controls)
            {
                if (c is GridView)
                    ((GridView)(c)).Enabled = false;
                foreach (Control ctrl in c.Controls)
                {
                    if (ctrl is TextBox)
                    {
                        ((TextBox)(ctrl)).Enabled = false;
                    }
                    else if (ctrl in GridView)
                    {
                      ((GridView)(ctrl).Enabled =false;
                     }
                }
            }
        }
    }       
}

测试回调.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="TestCallBack.aspx.cs" Inherits="PTest.TestCallBack" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
    </div>
    </form>
</body>
</html>

TestCallBack.aspx.cs

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

namespace PTest
{
    public partial class TestCallBack : System.Web.UI.Page
    {


        protected void Button1_Click(object sender, EventArgs e)
        {
            Response.Redirect("Trial.aspx?value=true");

        }
    }
}
4

1 回答 1

0

阅读您的评论后,您的问题似乎在这里:

if (!Page.IsPostBack)
        {
            string name = Request.QueryString["value"];
            if (name == "true")
            {
            hidecontrols();
            }
            else 
            {

            SetInitialRow();
            }
        }

您实际上是在说如果此页面不是postback然后执行此IF语句 - 然后在IF语句中您说如果存在 qstring 值隐藏控件。由于您正在重定向回此页面,因此它不会是回发,并且由于您将拥有一个 qstring value,它将隐藏控件。

也许尝试这样的事情:

 SetInitialRow();
 if (!Page.IsPostBack)
        {

            string name = Request.QueryString["value"];
            if (name == "true")
            {
              GridView1.ShowFooter = false;
            }
            else 
            {
             GridView1.ShowFooter = true;
            }
        }

不确定这是否是您真正想要的,尽管问题在于您的页面加载中的 if-else。也许如果你用你所追求的实际流程更新你的 Q;这将有助于我们为您提供更多帮助。

于 2013-09-22T12:42:49.190 回答