[英]Accessing code behind method Variable in aspx page
我在自定义详细视图中工作。 我有一个程序页面,可以查看所有程序。 当用户单击“更新”按钮时,将重定向用户以管理程序页面。 “管理程序”页面包含一个从中提取所有行的方法。
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;
}
}
如何从.aspx页面访问变量? 让我们说我想访问ProgTitle
。
我使用过这种方法,但似乎没有用
<%=ProjTitle%>
我想在那里尊重的文本框中显示每列的值
<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>
通常我会将html代码添加到方法但这不可行所以我想获取变量值并将其显示在文本框中。
htmlStr+="<table><tr><td>"+ProgTitle+"</td></tr></table>";
必须在后面的代码ProgTitle
声明为protected
或public
,以使其可从.aspx访问。 更改您的代码如下
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;
}
}
然后你可以在你的aspx代码中访问ProgTitle
<input type="text" name="Title" class="large" value="<%=ProgTitle %>">
如果不使用数据绑定,那么如何使用session并分配变量然后使用它。 代码Behing: SESSION.START(); SESSION["PjTitle"]=PrgTtile;
SESSION.START(); SESSION["PjTitle"]=PrgTtile;
然后在aspx中使用以下命令访问它: <%= SESSION["PjTitle"] %>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.