ASP.NET - 在.aspx页面中使用变量 [英] ASP.NET - Use variable in .aspx page

查看:147
本文介绍了ASP.NET - 在.aspx页面中使用变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从.aspx文件访问的变量或财产在我的cs文件,但没有任何运气。我看了看这里的几个线程,它们都试过了,但仍然没有运气。这里的情景;我有与文本框GridView控件,并在一排一个RequiredFieldValidator:

I'm trying to access a variable or property in my .cs file from the .aspx file but not having any luck. I've looked at several threads on here and have tried them all but still no luck. Here's the scenario; I've got a GridView control with textboxes and a RequiredFieldValidator in one row:

<asp:TemplateField HeaderText="User Name" ItemStyle-Width="10px">
    <ItemTemplate>
        <asp:Label ID="lblWebLogin" runat="server" Text='<%#Eval("WebLogin") %>'></asp:Label>
    </ItemTemplate>
    <EditItemTemplate>
        <asp:TextBox ID="txtWebLogin" runat="server" Text='<%#Eval("WebLogin") %>'></asp:TextBox>
        <asp:RequiredFieldValidator ID="rfvEditWebLogin" runat="server" 
            ControlToValidate="txtWebLogin" Display="None" ErrorMessage='<%= this.LoginRequired %>'></asp:RequiredFieldValidator>
    </EditItemTemplate>
    <FooterTemplate>
        <asp:TextBox ID="txtWebLogin" runat="server"></asp:TextBox>
    </FooterTemplate>
</asp:TemplateField>

有是这是工作,因为我可以的ErrorMessage设置为一个文本字符串,看到它的工作原理在窗体上的ValidationSummary控件。

There is a ValidationSummary control on the form which is working because I can set the ErrorMessage to a literal string and see that it works.

下面是从我的.cs的code:

Here's the code from my .cs:

public string LoginRequired
{
    get { return "Error, please try again"; }
}

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
        BindDataToGrid();
}

private void BindDataToGrid()
{
    DataTable dt;
    SqlCommand sc = new SqlCommand();
    DAL oDAL = new DAL(DBInformation.Instance.CurrentEncodedConnectionString, DBInformation.Instance.ConnectionTimeout);

    //Get User Information.
    sc.CommandText = Constants.StoredProcedureNames.GetUsers;
    dt = oDAL.SPDataTable(sc, "AllUsers");
    gvUsers.DataSource = dt;

    foreach (DataRow dr in dt.Rows)
        lUser.Add(new User());

    gvUsers.DataBind();
}

任何想法,为什么它不工作?

any ideas as to why it's not working?

推荐答案

更改此

ErrorMessage='<%= this.LoginRequired %>'

在此

ErrorMessage='<%# this.LoginRequired %>'

和呼叫的DataBind()在页面加载

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
  {
    DataBind();
    BindDataToGrid();
 }
}

这篇关于ASP.NET - 在.aspx页面中使用变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆