如何将标签的id传递给c#.net中的函数

时间:2014-05-14 10:42:15

标签: asp.net c#-4.0

我有以下标签。

<asp:Label ID="lbl_Modification" runat="server" Font-Bold="False" Font-Size="Small" ForeColor="#FF3300" Visible="False" Width="300px"></asp:Label>
<asp:Label ID="lbl_Message" runat="server" Font-Bold="False" Font-Size="Small" ForeColor="#FF3300" Visible="False" Width="300px"></asp:Label>

我想将上述标签的id传递给以下方法。

public bool date_Validation(DateTime t_Start_Date,DateTime t_End_Date,DateTime t_View_From)
{
    #region  date_Validation
    try
    {
        if (t_Start_Date.Date < DateTime.Today)
        {
            lbl_Modification.Visible = true;
            lbl_Modification.Text = "Date cannot be lesser than the Current Date";
            return false;
        }
        else if (t_End_Date.Date < t_Start_Date)
        {
            lbl_Modification.Visible = true;
            lbl_Modification.Text = "Invalid End Date";
            return false;
        }
        else if (t_View_From.Date < DateTime.Today || t_View_From.Date >= t_Start_Date.Date)
        {
            lbl_Modification.Visible = true;
            lbl_Modification.Text = "view Date cannot be greater  than the Start Date or lesser than Current date";
            return false;
        }
        return true;
    }
    catch (Exception e)
    {
        throw e;
    }

    #endregion
}

我怎么能这样做。 请有人帮我这样做。

2 个答案:

答案 0 :(得分:3)

public bool date_Validation(DateTime t_Start_Date,DateTime t_End_Date,DateTime t_View_From, Label lblModification)
{
// Method Content
}
//Call this method with your label parameter
date_Validation(startdate,enddate,viewform,lbl_Modification)
//lbl_Modification -> its your label name

答案 1 :(得分:1)

Control的ID是一个字符串,如果你想将它作为一个额外的参数传递,那么改变你的方法声明以接受另一个参数

public bool date_Validation(DateTime t_Start_Date,DateTime t_End_Date,DateTime t_View_From, string LabelID)
{
    #region  date_Validation
    try
    {
        if (t_Start_Date.Date < DateTime.Today)
        {
            //Find the control here 
            var label=This.FindControl(LabelID);
            lbl_Modification.Visible = true;
            lbl_Modification.Text = "Date cannot be lesser than the Current Date";
            return false;
        }
        else if (t_End_Date.Date < t_Start_Date)
        {
            lbl_Modification.Visible = true;
            lbl_Modification.Text = "Invalid End Date";
            return false;
        }
        else if (t_View_From.Date < DateTime.Today || t_View_From.Date >= t_Start_Date.Date)
        {
            lbl_Modification.Visible = true;
            lbl_Modification.Text = "view Date cannot be greater  than the Start Date or lesser than Current date";
            return false;
        }
        return true;
    }
    catch (Exception e)
    {
        throw e;
    }

    #endregion
}

然后你的调用代码就像

var isValid=date_Validation(startdate, enddate, "lbl_Modification");