简体   繁体   中英

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? Lets say I would like to access 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.

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. 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

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

If not using databinding, how about using session and allocate the variable then use it. On code Behing: SESSION.START(); SESSION["PjTitle"]=PrgTtile; SESSION.START(); SESSION["PjTitle"]=PrgTtile; and then in aspx acces it with: <%= SESSION["PjTitle"] %>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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