RowDataBound中的字符串格式

时间:2014-09-12 11:23:53

标签: c#

我有一个数据阅读器,可以用 2014-07 格式从数据库中读取字符串。

在querystring值中,我总是使用字符串 2014-07 ,并且我使用字符串格式方法在 RowDataBound 中输出 2014年7月

    //In RowDataBound
    if (!string.IsNullOrEmpty(Request.QueryString["Month"]))
    {
        e.Row.Cells[1].Text = string.Format(CultureInfo.CurrentCulture, "{0:MMMM yyyy}", 
                                             DataBinder.Eval(e.Row.DataItem, "Month"));
    }

我使用下面的代码根据字符串 2014-07 2014年7月 c#应用程序中生成新密钥:

Label Month = (Label)row.FindControl("Month");

//generate a new key
DateTime dt;
DateTime.TryParseExact(Month.Text.ToString(), "MMMM yyyy",
               CultureInfo.CurrentCulture, DateTimeStyles.None, out dt);

//Check "-" in string
if (Month.Text.ToString().IndexOf("-") == -1)
{
    string key = dt.ToString("yyyy-MM");
}
else
{
    string key = Month.Text.ToString();
}

如果该字符串 2014-07 且我没有将 2014-07 的值转换为 2014年7月,我有正确的输出:

2014-07

如果在 RowDataBound 中转换的字符串是2014年7月,我输出错误:

0001-01

这开始让我相信我的整体结构是不正确的。

我错过了什么?

代码有什么问题?

我非常感谢您在解决这个问题时能给我的任何帮助。

Edit #1

当输出是2014年7月并且输出为空时,我尝试打印gridview的值,为什么?

Label Month = (Label)row.FindControl("Month");

Response.Write(Month.Text.ToString());
Response.End();

Edit #2

代码完整如下:

protected void sendValueOfMonth(object sender, EventArgs e)
{
    DateTime d = DateTime.Now;
    string key;
    ImageButton ImgSend = (ImageButton)sender;
    GridViewRow row = (GridViewRow)Lotto_A.NamingContainer;
    Label Month = (Label)row.FindControl("Month");

    DateTime dt;
    DateTime.TryParseExact(Month.Text.ToString(), "MMMM yyyy",
                   CultureInfo.CurrentCulture, DateTimeStyles.None, out dt);

    //Check "-" in string
    if (Month.Text.ToString().IndexOf("-") == -1)
    {
        key = dt.ToString("yyyy_MM");
    }
    else
    {
        key = Month.Text.ToString();
    }

    Response.Write(Month.Text.ToString() + "<br />");
    Response.Write(key.ToString());
 }


protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
      ImageButton ImgSend = (ImageButton)e.Row.FindControl("ImgSend");
      Label Month = (Label)row.FindControl("Month");


        if (!string.IsNullOrEmpty(Request.QueryString["Month"]))
        {
            InitializeCulture();
            e.Row.Cells[1].Text = string.Format(CultureInfo.CurrentCulture, "{0:MMMM yyyy}", DataBinder.Eval(e.Row.DataItem, "Month"));
        }

    }
}


                    <asp:TemplateField HeaderText="Send">
                        <ItemTemplate>
                            <center>
                                <asp:ImageButton ID="ImgSend" runat="server" OnClick="sendValueOfMonth" />
                            </center>
                        </ItemTemplate>
                    </asp:TemplateField>

                    <asp:TemplateField HeaderText="Month">
                        <ItemTemplate>
                            <center>
                                <asp:Label ID="Month" runat="server" Text='<%# Eval("Month").ToString() %>'></asp:Label>
                            </center>
                        </ItemTemplate>
                    </asp:TemplateField>

1 个答案:

答案 0 :(得分:0)

您不需要在代码背后做任何其他事情。试试这个:

<asp:Label ID="Month" runat="server" Text='<%# Bind("Month", "{0: yyyy-MM}").ToString() %>'></asp:Label>