RowDataBound中的字符串格式

本文关键字:格式 字符串 RowDataBound | 更新日期: 2023-09-27 18:21:48

我有一个数据读取器,可以从数据库中读取此格式的字符串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"));
    }

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

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

RowDataBound中的字符串格式

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

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