简体   繁体   English

在aspx页面中访问方法后面的代码变量

[英]Accessing code behind method Variable in aspx page

I am working in custom detailed view. 我在自定义详细视图中工作。 I have a program page that views all the program. 我有一个程序页面,可以查看所有程序。 When a user clicks on update button, the user is redirected to manage programs page. 当用户单击“更新”按钮时,将重定向用户以管理程序页面。 The Manage Programs Page contains a method that fetches all the rows from. “管理程序”页面包含一个从中提取所有行的方法。

public string ProgramsDetails()
{
    using (SqlConnection con = new SqlConnection(str))
    {
        string htmlStr = "";
        SqlCommand cmd = con.CreateCommand();
        cmd.CommandText = " SELECT * from programs where ProgId=@ProgId";
        cmd.Parameters.AddWithValue("@ProgId", "1");
        con.Open();
        SqlDataReader reader = cmd.ExecuteReader();
        if (reader.Read())
        {
            int ProgId = reader.GetInt32(0);
            string ProgTitle = reader.GetString(1);
            string ProgBudget = reader.GetString(4);
            string ProgStarDate = reader.GetString(5); 

        }

        con.Close();
        return htmlStr;
    }
}

How can I access a variable from .aspx page? 如何从.aspx页面访问变量? Lets say I would like to access ProgTitle . 让我们说我想访问ProgTitle

I have used this method but seems it's not working 我使用过这种方法,但似乎没有用

<%=ProjTitle%>

I would like to show the values of each column in there respected text box 我想在那里尊重的文本框中显示每列的值

<div class="cmrs-panel-body no-padding">
        <div class="cmrs-form-inline">
            <div class="cmrs-form-row bordered">
                <label class="cmrs-form-label">Program Code Name</label>
                <div class="cmrs-form-item">
                    <input type="text" name="Code" class="large">
                </div>
            </div>
        </div>
        <div class="cmrs-form-inline">
            <div class="cmrs-form-row bordered">
                <label class="cmrs-form-label">Program Title</label>
                <div class="cmrs-form-item">
                    <input type="text" name="Title" class="large" value="<%=ProgTitle %>"> 
                </div>
            </div>
        </div>
        <div class="cmrs-form-inline">
            <div class="cmrs-form-row bordered">
                <label class="cmrs-form-label">Program Description</label>
                <div class="cmrs-form-item">
                    <textarea class="large"></textarea>
                </div>
            </div>
        </div>
</div>

Normally i would add the html code to method but this is not feasible so i want to get the variable value and display it in textbox. 通常我会将html代码添加到方法但这不可行所以我想获取变量值并将其显示在文本框中。

htmlStr+="<table><tr><td>"+ProgTitle+"</td></tr></table>";

ProgTitle must be declared as protected or public in the code behind to make it accessible from .aspx. 必须在后面的代码ProgTitle声明为protectedpublic ,以使其可从.aspx访问。 Change your code as below 更改您的代码如下

protected string ProgTitle;

public string ProgramsDetails()
{
    using (SqlConnection con = new SqlConnection(str))
    {
        string htmlStr = "";
        SqlCommand cmd = con.CreateCommand();
        cmd.CommandText = " SELECT * from programs where ProgId=@ProgId";
        cmd.Parameters.AddWithValue("@ProgId", "1");
        con.Open();
        SqlDataReader reader = cmd.ExecuteReader();
        if (reader.Read())
        {
            int ProgId = reader.GetInt32(0);
            ProgTitle = reader.GetString(1);
            string ProgBudget = reader.GetString(4);
            string ProgStarDate = reader.GetString(5);              
        }

        con.Close();
        return htmlStr;
    }
}

then you can access ProgTitle in your aspx code 然后你可以在你的aspx代码中访问ProgTitle

<input type="text" name="Title" class="large" value="<%=ProgTitle %>">

If not using databinding, how about using session and allocate the variable then use it. 如果不使用数据绑定,那么如何使用session并分配变量然后使用它。 On code Behing: SESSION.START(); SESSION["PjTitle"]=PrgTtile; 代码Behing: SESSION.START(); SESSION["PjTitle"]=PrgTtile; SESSION.START(); SESSION["PjTitle"]=PrgTtile; and then in aspx acces it with: <%= SESSION["PjTitle"] %> 然后在aspx中使用以下命令访问它: <%= SESSION["PjTitle"] %>

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

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