how can i access string variable value of .aspx.cs file in .aspx file

◇◆丶佛笑我妖孽 提交于 2020-02-05 06:38:07

问题


i m getting a string value regression as regression = (Session["Regression"]).ToString(); in .aspx.cs file then i want to use this value in .aspx file in SelectCommand of SqlDataSource property as below

SelectCommand="SELECT [issue_oid], [issue_num], [regression], 
            [status], [tested_by], [tested_on], [patch_name], [arrived_on], [previous_info], [comment], [is_duplicate] FROM [itt_monthly_patch_issue_list] where status='Not Tested' and `regression='<%#regression%>'"`

.aspx.cs file page_load method is as below

 protected void Page_Load(object sender, EventArgs e)
    {
            if ((Session["UserName"].ToString()) == string.Empty)
            {
                Server.Transfer("regressionType.aspx");
            }
            regression = (Session["Regression"]).ToString();
            usrname = (Session["UserName"]).ToString();
            DataBind();
     }

please suggest me how can i do this? thanks in advance...


回答1:


You can you Session Variable's value in your SQLDatasource. Like this Example.

<asp:SqlDataSource ID="SqlDataSource1" runat="server"
    ConnectionString="<%$ ConnectionStrings:testDatabaseConnectionString %>"
    SelectCommand="SELECT * FROM [UserTable] WHERE ([userID] = @UserID)">
    <SelectParameters>
        <asp:SessionParameter Name="UserID" SessionField="UserID" Type="Int32" />
    </SelectParameters>
</asp:SqlDataSource>

let us know any other concerns on this..




回答2:


add Session paramter to your sql Datasource like this

<asp:SqlDataSource SelectCommand="SELECT [issue_oid], [issue_num], [regression], 
        [status], [tested_by], [tested_on], [patch_name], [arrived_on], [previous_info], [comment], [is_duplicate] FROM [itt_monthly_patch_issue_list] where status='Not Tested' and `regression='<%#regression%>'"`>

<SelectParameters>
        <asp:SessionParameter SessionField="Regression" Name="ParameterName" Type="String" />
</SelectParameters>
</asp:SqlDataSource>



回答3:


Try this:

  SelectCommand="SELECT [issue_oid], [issue_num], [regression], 
                [status], [tested_by], [tested_on], [patch_name], [arrived_on], [previous_info], [comment], [is_duplicate] FROM [itt_monthly_patch_issue_list] where status='Not Tested' and `regression='<%=ReturnRegression()%>'"`

In codebehind:

    string regression=null;//Before Page_Load

    protected string ReturnRegression()
    {
    //Write logic here to prevent null being returned
        return regression;
    }



回答4:


try this:

public StringBuilder regression = new StringBuilder();
regression.Append((Session["Regression"]).ToString());

Then in the .aspx page (in the selct command), you can use "regression" as:

 <% =regression %>


来源:https://stackoverflow.com/questions/11445620/how-can-i-access-string-variable-value-of-aspx-cs-file-in-aspx-file

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!