繁体   English   中英

标签在C#中的输出

[英]Label output in c#

在querystring值中,我始终具有字符串YEAR-MONTH(2014-07),并且使用字符串格式方法在具有RowDataBound事件的GridView中的输出Juli 2014中具有:

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下面的Label Month字符串的输出为null:

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, Label Monthkey string的输出正确,但是我需要在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"));
        }
    }
}

您是否尝试过查看如果在Postback上将数据重新绑定到网格会发生什么情况? 那可能是最简单的解决方案,但远未达到最佳解决方案。

我认为问题是由于在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"));
        }
    }
}
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"));
        }
    }
}

设置时,从行获取标签并设置文本。

希望对您有所帮助

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM