如何将变量传递给用户控件以用作sqldatasource的select命令

时间:2014-01-02 00:16:59

标签: asp.net user-controls sqldatasource

我不明白为什么我的usercontrol中的属性ParentScheduleItemId始终为零。我想要做的就是将值传递给我的用户控件,以便它可以用作SqlDataSource中select命令的select参数。我在下面有以下代码,它似乎导致ParentScheduleItemId == 0,但它实际上打印了我在句子No data was returned旁边的ascx文件中传递的预期非零值:

namespace CCApplication.Cms
{
    public partial class schedule_sub_item : System.Web.UI.UserControl
    {
        public int ParentScheduleItemId {
            get;

            set; }

        protected void Page_Load(object sender, EventArgs e)
        {
            SqlDataSource1.SelectParameters.Add("ParentScheduleItemId", ParentScheduleItemId.ToString());
            //SqlDataSource1.SelectParameters.Add("ParentScheduleItemId", "12");
        }
    }
}

我在ascx文件中有以下代码

<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
            ConnectionString="<%$ ConnectionStrings:NewConferencesApplicationConnectionString %>" 
            SelectCommand="
SELECT [ScheduleItemId],[ParentScheduleItemId],[Name],[Type],[Location],[MaximumUsers],[CompanionAllowed],[StartTime],[EndTime]
FROM [Conferences_ScheduleItem]
WHERE ParentScheduleItemId = @ParentScheduleItemId
ORDER BY StartTime;
            "
            DeleteCommand="DELETE FROM [Conferences_ScheduleItem] WHERE [ScheduleItemId] = @ScheduleItemId"
            >
        </asp:SqlDataSource>


        <asp:ListView ID="ListView1" runat="server" DataKeyNames="ScheduleItemId" 
            DataSourceID="SqlDataSource1">
            <EmptyDataTemplate>
                <table id="Table1" runat="server" style="">
                    <tr>
                        <td>
                            No data was returned.<% Response.Write(ParentScheduleItemId.ToString()); %></td>
                    </tr>
                </table>
            </EmptyDataTemplate>
            <ItemTemplate>
                <tr style="">
                    <td>
                        <asp:Label ID="ScheduleItemIdLabel" runat="server" 
                            Text='<%# Eval("ScheduleItemId") %>' />
                    </td>

                </tr>
            </ItemTemplate>
            <LayoutTemplate>
                <table id="Table2" runat="server">
                    <tr id="Tr1" runat="server">
                        <td id="Td1" runat="server">
                            <table ID="itemPlaceholderContainer" runat="server" border="0" style="">
                                <tr ID="itemPlaceholder" runat="server">
                                </tr>
                            </table>
                        </td>
                    </tr>
                    <tr id="Tr3" runat="server">
                        <td id="Td2" runat="server" style="">
                        </td>
                    </tr>
                </table>
            </LayoutTemplate>
        </asp:ListView>

所以我想做的就是做<usercontrol:mylistview ParentScheduleItemId=24>之类的事情,然后用24作为我的SelectCommand的一部分。我如何实现这一目标?

注意 - 你会注意到我注释了SqlDataSource1.SelectParameters.Add("ParentScheduleItemId", "12");行。如果我取消注释它,它实际上表现如预期,并给我的选择参数数字12和sqldatasource给我我想要的值。我只是不明白为什么它上面的行有一个ParentScheduleItemId == 0.这是否与页面加载周期有关?无论如何,我只想将正确的parentcheduleitemid传递给我的选择命令。

1 个答案:

答案 0 :(得分:0)

好的,这令人沮丧。我把头撞到墙上3个小时后找到了解决办法。这是工作代码:

// codebehind

namespace CCApplication.Cms
{

public partial class schedule_sub_item : System.Web.UI.UserControl
{
    public string _parentScheduleItemId;
    public string ParentScheduleItemId {
        get
        {
            return _parentScheduleItemId;
        }
        set
        {
            _parentScheduleItemId = value;
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void UpdateSqlParametersEventHandler(object sender, SqlDataSourceSelectingEventArgs e)
    {
        System.Data.Common.DbParameter param = e.Command.CreateParameter();
        param.ParameterName = "@ParentScheduleItemId";
        param.Value = this._parentScheduleItemId;
        e.Command.Parameters.Add(param);
    }
}
}

//前端代码

<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
            ConnectionString="<%$ ConnectionStrings:NewConferencesApplicationConnectionString %>" 
OnSelecting="UpdateSqlParametersEventHandler"
            SelectCommand="
SELECT [ScheduleItemId],[ParentScheduleItemId],[Name],[Type],[Location],[MaximumUsers],[CompanionAllowed],[StartTime],[EndTime]
FROM [Conferences_ScheduleItem]
WHERE ParentScheduleItemId = @ParentScheduleItemId
ORDER BY StartTime;
            "
            DeleteCommand="DELETE FROM [Conferences_ScheduleItem] WHERE [ScheduleItemId] = @ScheduleItemId"
            >
    </asp:SqlDataSource>


    <asp:ListView ID="ListView1" runat="server" DataKeyNames="ScheduleItemId" 
        DataSourceID="SqlDataSource1">
        <EmptyDataTemplate>
            <table id="Table1" runat="server" style="">
                <tr>
                    <td>
                        No data was returned.<% Response.Write(ParentScheduleItemId.ToString()); %></td>
                </tr>
            </table>
        </EmptyDataTemplate>
        <ItemTemplate>
            <tr style="">
                <td>
                    <asp:Label ID="ScheduleItemIdLabel" runat="server" 
                        Text='<%# Eval("ScheduleItemId") %>' />
                </td>

            </tr>
        </ItemTemplate>
        <LayoutTemplate>
            <table id="Table2" runat="server">
                <tr id="Tr1" runat="server">
                    <td id="Td1" runat="server">
                        <table ID="itemPlaceholderContainer" runat="server" border="0" style="">
                            <tr ID="itemPlaceholder" runat="server">
                            </tr>
                        </table>
                    </td>
                </tr>
                <tr id="Tr3" runat="server">
                    <td id="Td2" runat="server" style="">
                    </td>
                </tr>
            </table>
        </LayoutTemplate>
    </asp:ListView>

所以基本上我需要添加一个OnSelecting =“UpdateSqlParametersEventHandler”,然后在后面的代码中添加该函数来设置参数。

之前遇到问题的原因是因为页面加载周期导致sqldatasource在ParentScheduleItemId被设置之前运行...