更改标题名称时只有1个数据的Gridview错误可见

时间:2012-08-10 04:02:29

标签: c# asp.net visual-studio-2008

从上面的标题,我的代码第一次page_load,没有问题,它显示正确,但在我点击按钮更改语言后,它消失(只显示标题)。我花了大约一个星期才发现但仍然无法找到发生的事情。 如果可见数据大于1,则其工作正常。

编辑: 忘了把我的页面加载方法

    protected void Page_Load(object sender, EventArgs e)
    {
        NoResult.Visible = false;
        Get_Data();
    }

    protected void Get_Data()
    {

        DBCAD.Service1 myCADDB = new DBCAD.Service1();
        myCADDB.UseDefaultCredentials = true;
        string result = "";
        //set web service proxy
        if (!GlobalVariable_CCCNS.filterOrNot)
        {
            //invoke web service method
            result = myCADDB.CallCardStatus_Filter_CCCNSEMBILAN_ALL();
        }
        else
        {
            GlobalVariable_CCCNS.FilterDC = DropDownList1.SelectedValue;
            //invoke web service method
            if (GlobalVariable_CCCNS.FilterDC == "CCC NS ALL")
            {
                result = myCADDB.CallCardStatus_Filter_CCCNSEMBILAN_ALL();
             }
            else
            {                  
                result = myCADDB.CallCardStatus_Filter(GlobalVariable_CCCNS.FilterDC);
            }
        }

        //read the response data and put in xml document
        XmlDocument xmlDoc = new XmlDocument();
        xmlDoc.LoadXml(result);
        string mypath = Server.MapPath(@"Data.xml");

        //XmlTextReader reader = new XmlTextReader ("DBCAD.xml");
        xmlDoc.Save(mypath);

        //set the data set
        DataSet ds = new DataSet();

        ds.ReadXml(mypath);

        //Open hidden column
        CallCardStatus.Columns[0].Visible = true;

        if (ds.Tables.Count > 0)
        {
            //list out the result to Data Grid
            CallCardStatus.DataSource = ds;
            CallCardStatus.DataBind();
        }
        else
        {
            NoResult.Visible = true;
        }

        //Clear Unwanted Column
        CallCardStatus.Columns[0].Visible = false;
  }

这是我的RowDataBound

    string lastRow = "";
    protected void CallCardStatus_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        //Many item with same id but different status, I just want to visible and get the first row for each id.
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            var thisRow = e.Row;

            if (thisRow.Cells[0].Text == lastRow)
            {
                e.Row.Visible = false;
            }
            lastRow = thisRow.Cells[0].Text;
        }
    }

这是我的语言马来语和英语的RadioButton

    protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        GlobalVariable_CCCNS.cultureName = RadioButtonList1.SelectedValue.ToString();

        Page.Culture = GlobalVariable_CCCNS.cultureName;
        Page.UICulture = GlobalVariable_CCCNS.cultureName;
        if (GlobalVariable_CCCNS.cultureName == "ms-MY")
        {
            Label2.Visible = false;
            Label2.Text = "Kawalan Status Kad Panggilan";
            Label2.Visible = true;
        }
        else
        {
            Label2.Visible = false;
            Label2.Text = "CallCard Status Monitoring";
            Label2.Visible = true;
        }

        Page_Render();
    }


    protected void Page_Render()
    {
        Page.Culture = GlobalVariable_CCCNS.cultureName;
        Page.UICulture = GlobalVariable_CCCNS.cultureName;
        ALL.Text = GetLocalResourceObject("ALLResource1.Text").ToString();
        Label1.Text = GetLocalResourceObject("Label1Resource1.Text").ToString();
        NoResult.Text = GetLocalResourceObject("NoResultResource1.Text").ToString();

        DBCAD.Service1 myCADDB = new DBCAD.Service1();
        myCADDB.UseDefaultCredentials = true;
        string result = "";
        //set web service proxy
        if (!GlobalVariable_CCCNS.filterOrNot)
        {
            //invoke web service method
            result = myCADDB.CallCardStatus_Filter_CCCNSEMBILAN_ALL();
        }
        else
        {
            GlobalVariable_CCCNS.FilterDC = DropDownList1.SelectedValue;
            //invoke web service method

            if (GlobalVariable_CCCNS.FilterDC == "CCC NS ALL"){
                    result = myCADDB.CallCardStatus_Filter_CCCNSEMBILAN_ALL();
            }else{
                    //invoke web service method
                    result = myCADDB.CallCardStatus_Filter(GlobalVariable_CCCNS.FilterDC);
            }
        }

        //read the response data and put in xml document
        XmlDocument xmlDoc = new XmlDocument();
        xmlDoc.LoadXml(result);
        string mypath = Server.MapPath(@"Data.xml");

        //XmlTextReader reader = new XmlTextReader ("DBCAD.xml");
        xmlDoc.Save(mypath);

        //set the data set
        DataSet ds = new DataSet();

        ds.ReadXml(mypath);
        // CallCardStatus.DataSource = ds;
        //CallCardStatus.DataBind();

        //Open hidden column
        CallCardStatus.Columns[0].Visible = true;

        if (ds.Tables.Count > 0)
        {
            //list out the result to Data Grid
            CallCardStatus.DataSource = ds;
            CallCardStatus.Columns[2].HeaderText = GetLocalResourceObject("ButtonFieldResource1.Text").ToString();
            CallCardStatus.Columns[3].HeaderText = GetLocalResourceObject("BoundFieldResource3.HeaderText").ToString();
            CallCardStatus.Columns[4].HeaderText = GetLocalResourceObject("BoundFieldResource4.HeaderText").ToString();
            CallCardStatus.Columns[5].HeaderText = GetLocalResourceObject("BoundFieldResource5.HeaderText").ToString();
            CallCardStatus.Columns[6].HeaderText = GetLocalResourceObject("BoundFieldResource6.HeaderText").ToString();
            CallCardStatus.Columns[7].HeaderText = GetLocalResourceObject("BoundFieldResource7.HeaderText").ToString();
            CallCardStatus.Columns[8].HeaderText = GetLocalResourceObject("BoundFieldResource8.HeaderText").ToString();
            CallCardStatus.Columns[9].HeaderText = GetLocalResourceObject("BoundFieldResource9.HeaderText").ToString();
            CallCardStatus.DataBind();
        }
        else
        {
            NoResult.Visible = true;
        }

        //Clear Unwanted Column
        CallCardStatus.Columns[0].Visible = false;
     }

任何人都可以提供帮助吗?谢谢.Siti ..:)

1 个答案:

答案 0 :(得分:1)

为了清理遗留代码,我建议您使用以下内容:(清理代码将有助于您更轻松地发现错误)

绑定您的GridView

绑定数据绑定控件的最佳做法是在Page_Load事件中:

protected void Page_Load(object sender, EventArgs e)
{
     if (!this.IsPostBack)
     {
         // in this method u will bind your GridView
         this.BindGrid();
     }
}

除非我遗漏了某些内容,否则Page_Render方法中的代码将用于呈现GridView,并且该代码在Get_Data方法中重复。您可以将代码专用于将GridView绑定在一个方法中。

现在,您只需在内容发生变化时重新绑定GridView,例如,如果您允许用户修改GridView条记录。否则,您不应该重新绑定它。 (只要您的网页有EnableViewState="true"

,就是这样

用于本地化GridView列的代码可以移至GridVew.DataBound事件。或者甚至更好,委托逻辑将视图本地化为您的标记,为此,您可以在GridView中创建模板化列。

示例:

    <asp:GridView runat="server" DataSourceID="lds" ID="gv"
        AutoGenerateColumns="false"
    >
        <Columns>
            <asp:TemplateField>
                <HeaderTemplate>
                    <asp:Label Text="<%$ Resources: your_resource_file_name_without_extension, your resource_key %>" runat="server" />
                </HeaderTemplate>
                <ItemTemplate>
                    <asp:Label ID="a_meaningfull_name" Text='<%# Eval("your_field_name") %>' runat="server" />
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>

设置覆盖Page.InitializeCulture方法的页面语言:

请注意,您只需在此方法中设置this.UICulture = culture;this.Culture = culture;

protected override void InitializeCulture()
{
    // you have to call Request.Form, because at this point in the page life cycle, the page viewstate has not been loaded yet
    var culture = this.Request.Form["RadioButtonList1"];

    if (!string.IsNullOrWhiteSpace(culture))
    {
        // if the values of your list are culture specific (ie. en-US, es-MX, etc) you can uncomment the following line
        // this.Culture = culture;
        this.UICulture = culture;
        base.InitializeCulture();
    }
}

使用此方法,不需要RadioButtonList1_SelectedIndexChanged事件。您在此事件中放置的代码不是必需的。

此代码:

    if (GlobalVariable_CCCNS.cultureName == "ms-MY")
    {
        Label2.Visible = false;
        Label2.Text = "Kawalan Status Kad Panggilan";
        Label2.Visible = true;
    }
    else
    {
        Label2.Visible = false;
        Label2.Text = "CallCard Status Monitoring";
        Label2.Visible = true;
    }

可以使用标记轻松消除:(请注意,您需要为要在应用程序中使用的每种语言创建一个资源文件,to learn more about ASP.Net Globalization click here

使用全球资源

<!-- Assuming global resources -->
<asp:Label runat="server" ID="Label2" Text="<%$ Resources: your_resource_file_name_without_extension, your_resource_key %>"  />

您的全局资源文件如下所示:

  <data name="your_resource_key" xml:space="preserve">
    <value>your text</value>
  </data>

使用本地资源

<!-- Assuming local implicit resources -->
<asp:Label runat="server" ID="Label2" meta:resourcekey="base_name_of_your_resource_key" Text="default value used to render the control at design time in VS" />

在这种情况下,您的本地资源文件将如下所示:

  <data name="base_name_of_your_resource_key.Text" xml:space="preserve">
    <value>your text</value>
  </data>

您无需在Page_Render();活动中致电RadioButtonList1_SelectedIndexChanged