2012-07-12 69 views
1

即時獲取字符串值迴歸爲regression = (Session["Regression"]).ToString();在.aspx.cs文件中,然後我想在SelectCommand of SqlDataSource屬性中的.aspx文件中使用此值如下如何訪問.aspx文件中的.aspx.cs文件的字符串變量值

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文件的Page_Load方法如下

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(); 
    } 

請建議我怎樣才能做到這一點? 在此先感謝...

+0

可以通過使用System.CodeDom,expressionBuilders,德做過類似的工作,我做到了一次HTTP:/ /satindersinght.blogspot.in/2012/01/how-to-set-dynamic-connectionstring-of.html – 2012-07-12 05:47:43

+0

這讓我想起爲什麼我討厭WebForms。 – 2012-07-12 05:51:05

回答

2

您可以您會話變量在你的SqlDataSource值。像這個例子一樣。

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

讓我們知道有這方面的其他問題..

+0

非常感謝mayank .......它正在工作 – picnic4u 2012-07-13 08:26:20

0

添加會話參數設置爲您的SQL數據源這樣

<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> 
+0

沒有任何運氣。任何其他方式? – picnic4u 2012-07-12 12:38:00

+0

你現在得到什麼錯誤?這是唯一的方法,你必須聲明式地(在aspx代碼中)或編程式地(在代碼後面)將會話feild添加到sqldatasource。 – 2012-07-12 13:01:32

+0

SelectCommand =「SELECT [issue_oid],[issue_num],[regression], [status],[tested_by],[tested_on],[patch_name],[arrive_on],[previous_info],[comment],[is_duplicate] FROM [itt_monthly_patch_issue_list]其中status ='未測試'和[迴歸] ='<%= regression%>'「 實際上,上述命令不會從db中獲取數據,因爲查詢的」迴歸「列 的值不正確確認即時在wep頁面上打印一個標籤中的值,但在那裏它正在打印,因爲它就像<%#regression%> – picnic4u 2012-07-12 13:34:20

0

試試這個:

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()%>'"` 

在代碼隱藏:

string regression=null;//Before Page_Load 

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

不working.is有任何我必須爲'

'就像get或set方法那樣? – picnic4u 2012-07-12 12:12:29

+0

can any1可以幫我嗎? – picnic4u 2012-07-13 07:08:54

0

試試這個:

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

然後在.aspx頁面(在SELCT命令),你可以用「迴歸」爲:

<% =regression %> 
+0

仍然沒有從DB獲取數據bcoz迴歸值不是從aspx.cs頁面到aspx頁面 – picnic4u 2012-07-12 12:05:50

+0

可以any1幫我出來嗎? – picnic4u 2012-07-13 07:08:14

+0

檢查第三篇文章鏈接:[鏈接](http://forums.asp.net/t/1115044.aspx/1)..希望它可以幫助您 – user1509 2012-07-13 07:17:12

相關問題