C# c语言中的标签输出#
在查询字符串值中,我始终使用字符串年-月(2014-07),并使用字符串格式方法在GridView中的输出Juli 2014中使用RowDataBound事件:C# c语言中的标签输出#,c#,asp.net,C#,Asp.net,在查询字符串值中,我始终使用字符串年-月(2014-07),并使用字符串格式方法在GridView中的输出Juli 2014中使用RowDataBound事件: protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { if (!string.IsNullOrEmp
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”调用受保护的无效发送月,则以下标签月和键字符串的输出为空:
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();
}
编辑#1
<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>
我已经尝试过这个解决方案
在这种情况下,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"));
}
}
}
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"));
}
}
}
您是否尝试过在回发时将数据重新绑定到网格会发生什么情况?这可能是最简单的解决方案,但远不是最好的。我认为问题在于,在
RowDataBound
事件处理程序中,您没有设置Month
标签的值;您正在设置网格单元格的文本,而不是月份
标签的文本:
e.Row.Cells[1].Text = string.Format(new CultureInfo("de-DE"),
"{0:MMMM yyyy}",
DataBinder.Eval(e.Row.DataItem, "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"));
}
}
}
设置时,从行获取标签并设置文本
我希望这能帮助您如果您能显示绑定到GridView的数据类型以及希望得到的结果,那就更好了get@AntonioMailtraq,请问
e.Row.DataItem
的Month
属性的类型是什么?你能发布一段关于这个类是如何定义的吗?@AntonioMailtraq,还有,Request.QueryString[“Month”]
与这些有什么关系?您必须显示查询字符串中的日期而不是数据项中的日期吗?Month
的值是MySQL中查询的结果。此查询填充了GridView。还有什么要知道的?@AntonioMailtraq,是的。Month
属性的类型是什么?它是日期时间吗?另外,尝试从标记中删除部分Text='
;这已经不相关了。