在查询字符串值中,我总是字符串 YEAR-MONTH(2014-07),我使用字符串格式方法在输出 Juli 2014 带有 RowDataBound 事件的GridView :
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (!string.IsNullOrEmpty(Request.QueryString["Month"]))
{
e.Row.Cells[1].Text = string.Format(new CultureInfo("de-DE"),
"{0:MMMM yyyy}",
DataBinder.Eval(e.Row.DataItem, "Month"));
}
}
}
如果使用 asp:ImageButton ID =" SendImg" 受保护的void Sendmonth 调用标签月下面的输出键字符串为空:
protected void Sendmonth(object sender, EventArgs e)
{
ImageButton SendImg = (ImageButton)sender;
GridViewRow row = (GridViewRow)SendImg.NamingContainer;
Label Month = (Label)row.FindControl("Month");
string key;
DateTime dt;
DateTime.TryParseExact(Month.Text.ToString(), "MMMM yyyy",
CultureInfo.CurrentCulture, DateTimeStyles.None, out dt);
key = "2 >>> " + Month.Text.ToString();
Response.Write(Month.Text.ToString() + "<br />");
Response.Write(key.ToString());
Response.End();
}
这开始让我相信我的整体结构是不正确的。
我错过了什么?
代码有什么问题?
我非常感谢您在解决这个问题时能给我的任何帮助。
.aspx
<asp:TemplateField HeaderText="Month">
<ItemTemplate>
<center>
<asp:Label ID="Month" runat="server" Text='<%# Eval("Month").ToString() %>'></asp:Label>
</center>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Sendmonth">
<ItemTemplate>
<center>
<asp:ImageButton ID="SendImg" runat="server" OnClick="Sendmonth" />
</center>
</ItemTemplate>
</asp:TemplateField>
EDIT #1
我尝试了repierre
建议的解决方案在这种情况下,我在GV中的值等于2014-07,标签月和键字符串的输出是正确的,但我需要在GV中显示价值Juli 2014。
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (!string.IsNullOrEmpty(Request.QueryString["Month"]))
{
var label = e.Row.FindControl("Month") as Label;
// Check if label is not null
label.Text = string.Format(new CultureInfo("de-DE"),
"{0:MMMM yyyy}",
DataBinder.Eval(e.Row.DataItem, "Month"));
}
}
}
答案 0 :(得分:0)
您是否尝试过在Postback上将数据重新绑定到网格时会发生什么?这可能是最简单的解决方案,但远非最好的。
答案 1 :(得分:0)
我认为问题是由于您的RowDataBound
事件处理程序中您没有设置Month
标签的值;您要设置网格单元格的文本而不是Month
标签:
e.Row.Cells[1].Text = string.Format(new CultureInfo("de-DE"),
"{0:MMMM yyyy}",
DataBinder.Eval(e.Row.DataItem, "Month"));
尝试设置Month
标签的值:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (!string.IsNullOrEmpty(Request.QueryString["Month"]))
{
var label = e.Row.FindControl("Month") as Label;
// Check if label is not null
label.Text = string.Format(new CultureInfo("de-DE"),
"{0:MMMM yyyy}",
DataBinder.Eval(e.Row.DataItem, "Month"));
}
}
}
答案 2 :(得分:0)
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (!string.IsNullOrEmpty(Request.QueryString["Month"]))
{
e.Row.Cells[1].Text =
string.Format(new CultureInfo("de-DE"),
"{0:MMMM yyyy}", DataBinder.Eval(e.Row.DataItem, "Month"));
}
}
}
更改为:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (!string.IsNullOrEmpty(Request.QueryString["Month"]))
{
Label Month = (Label)e.Row.FindControl("Month");
Month.Text = string.Format(new CultureInfo("de-DE"),
"{0:MMMM yyyy}",
DataBinder.Eval(e.Row.DataItem, "Month"));
}
}
}
设置从行获取标签并设置文本。
我希望这可以帮到你