Click here to Skip to main content
15,905,616 members
Home / Discussions / ASP.NET
   

ASP.NET

 
GeneralRe: asp:label databinding Pin
ybasha17-Oct-06 16:42
ybasha17-Oct-06 16:42 
GeneralRe: asp:label databinding Pin
Amit Agarrwal17-Oct-06 18:37
Amit Agarrwal17-Oct-06 18:37 
AnswerRe: asp:label databinding Pin
kirthikirthi17-Oct-06 18:39
kirthikirthi17-Oct-06 18:39 
GeneralRe: asp:label databinding Pin
ybasha17-Oct-06 19:50
ybasha17-Oct-06 19:50 
QuestionChange dynamically ConnectionString of SQLDatasource Pin
Sianspheric17-Oct-06 13:46
Sianspheric17-Oct-06 13:46 
AnswerRe: Change dynamically ConnectionString of SQLDatasource Pin
minhpc_bk17-Oct-06 15:57
minhpc_bk17-Oct-06 15:57 
GeneralRe: Change dynamically ConnectionString of SQLDatasource Pin
Sianspheric18-Oct-06 2:47
Sianspheric18-Oct-06 2:47 
GeneralRe: Change dynamically ConnectionString of SQLDatasource Pin
minhpc_bk18-Oct-06 20:08
minhpc_bk18-Oct-06 20:08 
My point is that instead of searching all the datasources on the web page to update their ConnectionString property, we only need to update the connection string in a central place and all the datasource controls using this place will benefit this change. Below is the sample code to demonstrate what I mean:

public class ExSqlDataSource : SqlDataSource
{
    public override string ConnectionString
    {
        get
        {
            return base.ConnectionString;
        }
        set
        {
            string conString = HttpContext.Current.Items["MyConnecionString"] as string;
            if (string.IsNullOrEmpty(conString))
                conString = value;
                   
            base.ConnectionString = conString;
        }
    }    
}


In the example above, I use the HttpContext.Current.Items to store the connection string (you can use whatever you want), this custom sqldatasource control can be used on the web page in lieu of the built-in sqldatasource control as below:

<cc1:ExSqlDataSource ID="SqlDataSource1" runat="server" 
ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
SelectCommand="..."></cc1:ExSqlDataSource>


At runtime, the datasource control by default uses the connection string stored in the ConnectionStrings section of the web.config file under the key NorthwindConnectionString. If I want to dynamically replace this connection string, I simply set it in the HttpContext.Current.Items["MyConnecionString"] item in the web page:

public partial class Default_ : System.Web.UI.Page
{
    protected void Page_Init(object sender, EventArgs e)
    {
        HttpContext.Current.Items["MyConnecionString"] = "new connect string goes here";
    }
}


So all custom datasource controls declared in the web page will benefit this new connection string without requiring you to set their ConnectionString property.


In addition, there is also another extention point that you can use to change the default connection string. That is you can build your own expression builder and you can use your own expression to set the ConnectionString property of the datasource control. With this option, you can intervene the way the ASP.NET sets the ConnectionString property of the datasource control. For more details, you can check out the article[^].
GeneralRe: Change dynamically ConnectionString of SQLDatasource Pin
Sianspheric19-Oct-06 7:26
Sianspheric19-Oct-06 7:26 
QuestionWizard control does not work - help Pin
Rookie17-Oct-06 12:03
Rookie17-Oct-06 12:03 
AnswerRe: Wizard control does not work - help Pin
minhpc_bk17-Oct-06 15:57
minhpc_bk17-Oct-06 15:57 
QuestionChange Direction of SiteMapPath Pin
morteza5717-Oct-06 10:37
morteza5717-Oct-06 10:37 
AnswerRe: Change Direction of SiteMapPath Pin
minhpc_bk17-Oct-06 19:48
minhpc_bk17-Oct-06 19:48 
GeneralRe: Change Direction of SiteMapPath Pin
morteza5717-Oct-06 20:13
morteza5717-Oct-06 20:13 
QuestionLoad XML file Pin
shapper17-Oct-06 9:22
shapper17-Oct-06 9:22 
AnswerRe: Load XML file Pin
minhpc_bk17-Oct-06 19:49
minhpc_bk17-Oct-06 19:49 
Questionregarding "application/octet-stream" Pin
DGtech17-Oct-06 5:11
DGtech17-Oct-06 5:11 
QuestionASP.NET and Shell command Pin
Ritesh123417-Oct-06 4:15
Ritesh123417-Oct-06 4:15 
AnswerRe: ASP.NET and Shell command Pin
minhpc_bk17-Oct-06 19:54
minhpc_bk17-Oct-06 19:54 
GeneralRe: ASP.NET and Shell command Pin
Ritesh123418-Oct-06 4:38
Ritesh123418-Oct-06 4:38 
GeneralRe: ASP.NET and Shell command Pin
minhpc_bk18-Oct-06 20:13
minhpc_bk18-Oct-06 20:13 
GeneralRe: ASP.NET and Shell command Pin
Ritesh123418-Oct-06 21:18
Ritesh123418-Oct-06 21:18 
Questionddls lose selections Pin
tadhg8817-Oct-06 3:31
tadhg8817-Oct-06 3:31 
AnswerRe: ddls lose selections Pin
albCode17-Oct-06 4:52
albCode17-Oct-06 4:52 
GeneralRe: ddls lose selections Pin
tadhg8817-Oct-06 5:05
tadhg8817-Oct-06 5:05 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.