1
我有一个数据阅读器以这种格式从数据库读取字符串2014-07。RowDataBound中的字符串格式
在查询字符串值,我也经常串2014-07我使用字符串格式方法在输出2014年7月在的RowDataBound:
//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-07或2014年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-07到2014年7月我有正确的输出:
2014-07
如果字符串中转换的RowDataBound是2014年7月我有不正确的输出:
0001-01
这开始让我相信我的结构作为一个整体是不正确的。
我缺少什么?
代码有什么问题?
我将不胜感激任何帮助,您可以给我在工作的这个问题。
Edit #1
我曾尝试打印GridView控件的值时,输出为2014年7月和输出中是空的,为什么呢?
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>
小心分享前端的来源呢? – jomsk1e 2014-09-12 11:50:44
前端来源? gridView代码? – 2014-09-12 11:54:34