I am trying to set the ConnectionString property to the return value of a function in the ASPX page.
我试图将ConnectionString属性设置为ASPX页面中函数的返回值。
Example:
<asp:SqlDataSource runat="server" id="blah"
ConnectionString="<%= ServerSensing.GetConnectionStringByServer("someKey"); %>"
>
...
</asp:SqlDataSource>
The above is obviously not going to work.. so.. what will?
以上显然不会起作用..那么......会是什么?
Preemptive remarks: * No, I can not use the Web.config binding
抢先备注:*不,我不能使用Web.config绑定
3 个解决方案
#1
you should be able to set it in your Page_Load, something like:
你应该可以在你的Page_Load中设置它,例如:
blah.ConnectionString = ServerSensing.GetConnectionStringByServer("someKey");
or if you dont have access to the code behind put some inline code on the page, like just before the markup for the SqlDataSource
或者,如果您无法访问后面的代码,请在页面上放置一些内联代码,就像在SqlDataSource的标记之前一样
<%
blah.ConnectionString = ServerSensing.GetConnectionStringByServer("someKey");
%>
#2
The best way i've found about this question is using Expression Builder in your solution.
我发现这个问题的最好方法是在解决方案中使用Expression Builder。
With this feature you can create a custom inline expression and use it in the SqlDataSource tag.
使用此功能,您可以创建自定义内联表达式并在SqlDataSource标记中使用它。
Yuo'll find some examples right here:
你会在这里找到一些例子:
http://weblogs.asp.net/infinitiesloop/archive/2006/08/09/The-CodeExpressionBuilder.aspx
That's how i implemented in my apps:
这就是我在我的应用程序中实现的方式:
[ExpressionPrefix("RepConnectionString")]
public class RepConnectionStringExpressionBuilder : ExpressionBuilder { public override CodeExpression GetCodeExpression(BoundPropertyEntry entry, object parsedData, ExpressionBuilderContext context) { CodeTypeReferenceExpression thisType = new CodeTypeReferenceExpression(base.GetType());
public class RepConnectionStringExpressionBuilder:ExpressionBuilder {public override CodeExpression GetCodeExpression(BoundPropertyEntry entry,object parsedData,ExpressionBuilderContext context){CodeTypeReferenceExpression thisType = new CodeTypeReferenceExpression(base.GetType());
CodePrimitiveExpression expression = new CodePrimitiveExpression(entry.Expression.Trim().ToString());
string evaluationMethod = "GetConnectionString";
return new CodeMethodInvokeExpression(thisType, evaluationMethod, new CodeExpression[] { expression });
}
public static string GetConnectionString(string expression)
{
XmlDocument xmlDoc = new XmlDocument();
string wPath = HttpContext.Current.Server.MapPath("~/XmlFile.xml");
xmlDoc.Load(wPath);
XmlNode wNode = xmlDoc.SelectSingleNode("Autenticacoes/Database[@id='" + expression + "']");
string wConnString = "";
if (wNode != null)
{
wConnString = "Data Source=" + wNode.Attributes["servidor"].Value + ";Initial Catalog=" + wNode.Attributes["db"].Value + ";Persist Security Info=True;User ID=" + wNode.Attributes["login"].Value + ";Password=" + wNode.Attributes["senha"].Value;
}
return wConnString;
}
}
in the web.config:
在web.config中:
<compilation>
<expressionBuilders>
<add expressionPrefix="RepConnectionString" type="RepConnectionStringExpressionBuilder"/>
</expressionBuilders>
#3
Can you set the connection string in the code behind?
你可以在后面的代码中设置连接字符串吗?
#1
you should be able to set it in your Page_Load, something like:
你应该可以在你的Page_Load中设置它,例如:
blah.ConnectionString = ServerSensing.GetConnectionStringByServer("someKey");
or if you dont have access to the code behind put some inline code on the page, like just before the markup for the SqlDataSource
或者,如果您无法访问后面的代码,请在页面上放置一些内联代码,就像在SqlDataSource的标记之前一样
<%
blah.ConnectionString = ServerSensing.GetConnectionStringByServer("someKey");
%>
#2
The best way i've found about this question is using Expression Builder in your solution.
我发现这个问题的最好方法是在解决方案中使用Expression Builder。
With this feature you can create a custom inline expression and use it in the SqlDataSource tag.
使用此功能,您可以创建自定义内联表达式并在SqlDataSource标记中使用它。
Yuo'll find some examples right here:
你会在这里找到一些例子:
http://weblogs.asp.net/infinitiesloop/archive/2006/08/09/The-CodeExpressionBuilder.aspx
That's how i implemented in my apps:
这就是我在我的应用程序中实现的方式:
[ExpressionPrefix("RepConnectionString")]
public class RepConnectionStringExpressionBuilder : ExpressionBuilder { public override CodeExpression GetCodeExpression(BoundPropertyEntry entry, object parsedData, ExpressionBuilderContext context) { CodeTypeReferenceExpression thisType = new CodeTypeReferenceExpression(base.GetType());
public class RepConnectionStringExpressionBuilder:ExpressionBuilder {public override CodeExpression GetCodeExpression(BoundPropertyEntry entry,object parsedData,ExpressionBuilderContext context){CodeTypeReferenceExpression thisType = new CodeTypeReferenceExpression(base.GetType());
CodePrimitiveExpression expression = new CodePrimitiveExpression(entry.Expression.Trim().ToString());
string evaluationMethod = "GetConnectionString";
return new CodeMethodInvokeExpression(thisType, evaluationMethod, new CodeExpression[] { expression });
}
public static string GetConnectionString(string expression)
{
XmlDocument xmlDoc = new XmlDocument();
string wPath = HttpContext.Current.Server.MapPath("~/XmlFile.xml");
xmlDoc.Load(wPath);
XmlNode wNode = xmlDoc.SelectSingleNode("Autenticacoes/Database[@id='" + expression + "']");
string wConnString = "";
if (wNode != null)
{
wConnString = "Data Source=" + wNode.Attributes["servidor"].Value + ";Initial Catalog=" + wNode.Attributes["db"].Value + ";Persist Security Info=True;User ID=" + wNode.Attributes["login"].Value + ";Password=" + wNode.Attributes["senha"].Value;
}
return wConnString;
}
}
in the web.config:
在web.config中:
<compilation>
<expressionBuilders>
<add expressionPrefix="RepConnectionString" type="RepConnectionStringExpressionBuilder"/>
</expressionBuilders>
#3
Can you set the connection string in the code behind?
你可以在后面的代码中设置连接字符串吗?