动态更改SQLDataSource的用户控件 [英] User control dynamically that dynamically changes the SQLDataSource

查看:89
本文介绍了动态更改SQLDataSource的用户控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了一个用户控件,并且希望在网站中以两种不同的形式使用它,但是某些参数"可能有所不同,包括:

*基础SQL数据源
*行为可能有所不同(胆量感觉)

I have created a user control, and, I would like to use it in two different forms within my website, however, some "parameters", might be different including:

* The underlying SQL DataSource
* Behavior might be different (gut feeling)

<br />
<pre lang="xml"><%@ Control Language="C#" AutoEventWireup="true" CodeFile="Envelope.ascx.cs" Inherits="Envelope" %><br />
<asp:FormView ID="MessageForm" runat="server" DataSourceID="SqlDataSource1"<br />
            AllowPaging="True"<br />
            OnItemDeleted = "ArticleEnvelope_Deleted"<br />
            OnItemInserted = "ArticleEnvelope_Inserted"<br />
            OnItemUpdated = "ArticleEnvelope_Updated"><br />
        <HeaderTemplate><br />
        </HeaderTemplate><br />
        <EditItemTemplate><br />
            Timestamp:<br />
            <asp:TextBox ID="TimestampTextBox" runat="server"<br />
                Text=''<%# Bind("Timestamp") %>'' /><br />
            <br /><br />
            Subject:<br />
            <asp:TextBox ID="SubjectTextBox" runat="server" Text=''<%# Bind("Subject") %>'' /><br />
            <br /><br />
            EntryText:<br />
            <asp:TextBox ID="EntryTextTextBox" runat="server"<br />
                Text=''<%# Bind("EntryText") %>'' /><br />
            <br /><br />
            <asp:LinkButton ID="UpdateButton" runat="server" CausesValidation="True"<br />
                CommandName="Update" Text="Update" /><br />
            &nbsp;<asp:LinkButton ID="UpdateCancelButton" runat="server"<br />
                CausesValidation="False" CommandName="Cancel" Text="Cancel" /><br />
        </EditItemTemplate><br />
         <InsertItemTemplate><br />
             Timestamp:<br />
             <asp:TextBox ID="TimestampTextBox" runat="server"<br />
                 Text=''<%# Bind("Timestamp") %>'' /><br />
             <br /><br />
             Subject:<br />
             <asp:TextBox ID="SubjectTextBox" runat="server" Text=''<%# Bind("Subject") %>'' /><br />
             <br /><br />
             EntryText:<br />
             <asp:TextBox ID="EntryTextTextBox" runat="server"<br />
                 Text=''<%# Bind("EntryText") %>'' /><br />
             <br /><br />
             <asp:LinkButton ID="InsertButton" runat="server" CausesValidation="True"<br />
                 CommandName="Insert" Text="Insert" /><br />
             &nbsp;<asp:LinkButton ID="InsertCancelButton" runat="server"<br />
                 CausesValidation="False" CommandName="Cancel" Text="Cancel" /><br />
        </InsertItemTemplate><br />
        <ItemTemplate><br />
            Timestamp:<br />
            <asp:Label ID="TimestampLabel" runat="server" Text=''<%# Bind("Timestamp") %>'' /><br />
            <br /><br />
            Subject:<br />
            <asp:Label ID="SubjectLabel" runat="server" Text=''<%# Bind("Subject") %>'' /><br />
            <br /><br />
            EntryText:<br />
            <asp:Label ID="EntryTextLabel" runat="server" Text=''<%# Bind("EntryText") %>'' /><br />
            <br /><br />
        </ItemTemplate><br />
 </asp:FormView><br />
<asp:SqlDataSource ID="SqlDataSource1" runat="server"<br />
    ConnectionString="<%$ ConnectionStrings:couch_dbConnectionString %>"<br />
    SelectCommand="SELECT [Timestamp], [Subject], [EntryText] FROM [Article]"></asp:SqlDataSourc</pre><br />
e><br />



因此,如何以不同的形式(aspx)动态分配用户控件的SQL数据源?

:confused:



So, how is it possible to dynamically assign the SQL DataSource of a User Control, from different forms (aspx)?

:confused:

推荐答案

ConnectionStrings:couch_dbConnectionString%>< br/> SelectCommand ="SELECT [Timestamp],[Subject],[EntryText] FROM [Article]"></asp:SqlDataSourc </pre>< br/> e>< br/>
ConnectionStrings:couch_dbConnectionString %>"<br /> SelectCommand="SELECT [Timestamp], [Subject], [EntryText] FROM [Article]"></asp:SqlDataSourc</pre><br /> e><br />



因此,如何以不同的形式(aspx)动态分配用户控件的SQL数据源?

:confused:



So, how is it possible to dynamically assign the SQL DataSource of a User Control, from different forms (aspx)?

:confused:


这是您应该能够执行的操作:

1.从用户控件中删除SqlDataSource ,并仅保留FormView.例如:

Here is how you should be able to do it:

1. Remove the SqlDataSource from the User control, and, keep only the FormView. For example:

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="UcDetails.ascx.cs" Inherits="UcDetails" %>
<asp:FormView ID="FormView1" runat="server">
</asp:FormView>



2.在用户控件后面的代码内创建一个SqlDataSource 参数,并在Page_Load()中分配FormView的DataSourceID .例如:



2. Create a SqlDataSource parameter inside the User control''s code behind and assign FormView''s DataSourceID in the Page_Load(). For example:

public SqlDataSource DataSource
{
    get;
    set;
}
protected void Page_Load(object sender, EventArgs e)
{
    FormView1.DataSourceID = DataSource.ID;
}




3.将SqlDataSource 放在Aspx页面中.例如:




3. Put the SqlDataSource in the Aspx pages. For example:

<body>
    <form id="form1" runat="server">
    <uc1:UcDetails ID="UcDetails1" runat="server" />
    <asp:SqlDataSource ID="SqlDataSource1" runat="server"></asp:SqlDataSource>
    </form>
</body


>

4.将FormView DataSource 设置为aspx页面的Page_Load().例如:


>

4. Set the DataSource of the FormView into the Page_Load() of aspx pages. For example:

protected void Page_Load(object sender, EventArgs e)
{
        UcDetails1.DataSource = SqlDataSource1;
}



我认为您应该了解基本概念并可以实现您的功能.



I think you should get the basic idea and can implement your functionality.


这篇关于动态更改SQLDataSource的用户控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆