如何将此示例 aspx 代码移至 aspx.cs(以实现代码隐藏)?

发布于 2024-11-11 18:40:12 字数 618 浏览 3 评论 0原文

我有一个下拉列表,其中填充了来自 SQL 数据库的数据。这就是我在 aspx 文件中可能有的内容。如何将代码从 aspx 文件移动(尽可能多)到 aspx.cs 文件以实现代码隐藏技术? 我的意思是至少 SELECT 部分。 谢谢。

<asp:DropDownList ID="DropDownList1" ... runat="server"/>
...
<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
ConnectionString="<%$ ConnectionStrings:Pubs %>"
SelectCommand="SELECT [au_id], [au_lname], [au_fname], [state] FROM [authors] WHERE    [state] = @state">
<SelectParameters>
<asp:ControlParameter Name="state" ControlID="DropDownList1"  PropertyName="SelectedValue" />
</SelectParameters>
</asp:SqlDataSource>

I have a dropdownlist which is populated with data from an SQL db. This is what I might have in the aspx file. How do I move (as much as possible) the code from the aspx file to the aspx.cs file to implement the code behind technique?
I mean at least the SELECT portion.
Thanks.

<asp:DropDownList ID="DropDownList1" ... runat="server"/>
...
<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
ConnectionString="<%$ ConnectionStrings:Pubs %>"
SelectCommand="SELECT [au_id], [au_lname], [au_fname], [state] FROM [authors] WHERE    [state] = @state">
<SelectParameters>
<asp:ControlParameter Name="state" ControlID="DropDownList1"  PropertyName="SelectedValue" />
</SelectParameters>
</asp:SqlDataSource>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

故事灯 2024-11-18 18:40:12

假设您正在将网格与数据源 SqlDataSource1 绑定,那么您可以在代码隐藏中捕获 SelectedIndexChanged 事件并获取数据来绑定网格,如下所示:

ASPX 文件:

<asp:DropDownList ID="DropDownList1" runat="server" 
         OnSelectedIndexChanged="ddlChanged" />

C#(代码隐藏):

protected void ddlChanged(object sender, EventArgs e)
{
    var cs=..;//get connection string
    using(var con=new SqlConnection(cs))
    {
        using(var com=new SqlCommand(con))
        {
            com.Open();
            com.CommandType = CommandType.Text;
            com.CommandText="SELECT [au_id], [au_lname], [au_fname], [state] 
                 FROM [authors] WHERE [state] = @state";

            var state=....;//GET VALUE OF STATE FROM DROPDOWN
            var p = com.Parameters.Add("@state");//set other properties
            p.Value = state;

            using(var adptr=new SqlDataAdapter(com))
            {
                var dtb=new DataTable();
                adptr.Fill(dtb);
                grid.DataSource=dtb;
                grid.DataBind();
            }
        }
    }
}

suppose you are binding a grid with data source SqlDataSource1 then you can catch SelectedIndexChanged event in codebehind and get data to bind the grid like this:

ASPX file:

<asp:DropDownList ID="DropDownList1" runat="server" 
         OnSelectedIndexChanged="ddlChanged" />

C# (codebehind):

protected void ddlChanged(object sender, EventArgs e)
{
    var cs=..;//get connection string
    using(var con=new SqlConnection(cs))
    {
        using(var com=new SqlCommand(con))
        {
            com.Open();
            com.CommandType = CommandType.Text;
            com.CommandText="SELECT [au_id], [au_lname], [au_fname], [state] 
                 FROM [authors] WHERE [state] = @state";

            var state=....;//GET VALUE OF STATE FROM DROPDOWN
            var p = com.Parameters.Add("@state");//set other properties
            p.Value = state;

            using(var adptr=new SqlDataAdapter(com))
            {
                var dtb=new DataTable();
                adptr.Fill(dtb);
                grid.DataSource=dtb;
                grid.DataBind();
            }
        }
    }
}
掀纱窥君容 2024-11-18 18:40:12

SelectCommand 是您正在使用的数据源对象的属性。这些可以根据需要应用在后面的代码中,但您可能希望在重写的 Init 页面函数中执行此操作,因为这可能会在 ASP.NET 页面生命周期的早期使用。例如,虽然我不确定具体在哪里。

protected override OnInit(object sender, EventArgs e)
{
    dsMySource.SelectCommand = "SELECT [au_id], [au_lname], [au_fname], [state] FROM [authors] WHERE [state] = @state"

}

您还必须确保在后面的代码中正确使用 @state 参数,这也可以作为属性进行访问 (dsMySource.SelectParameters)。

The SelectCommand is a property of the datasource object you are using. These can be applied in the code behind as needed, but you may want to do it in an overridden Init page function as this might be used quite early on in the asp.net page life cycle. for eg, although I'm not sure exactly where.

protected override OnInit(object sender, EventArgs e)
{
    dsMySource.SelectCommand = "SELECT [au_id], [au_lname], [au_fname], [state] FROM [authors] WHERE [state] = @state"

}

You are also going to have to make sure the @state parameter is used correctly in the code behind as well, this can be accessed also as a property (dsMySource.SelectParameters).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文