Accessing the code behind the Variable method on the aspx page
I am working in a custom detail view. I have a program page that displays the entire program. When the user clicks the refresh button, the user is redirected to the program management page. The Manage Program Pages page contains a method from which all strings are retrieved.
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 the variable from the .aspx page? Let's say I would like to access ProgTitle
.
I have used this method but it doesn't seem to work
<%=ProjTitle%>
I would like to show the values โโof each column in the corresponding textbox
<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 html code to the method, but that is not possible, so I want to get the value of the variable and display it in a textbox.
htmlStr+="<table><tr><td>"+ProgTitle+"</td></tr></table>";
source to share
ProgTitle
must be declared as protected
or public
in the code behind to make it accessible from .aspx. Modify your code 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 %>">
source to share