ASP.NET 以编程方式设置 SqlDataSource 参数不适用于回发

发布于 2024-09-04 09:55:48 字数 1926 浏览 8 评论 0 原文

我想以编程方式设置 SqlDataSource 的参数,如 http://www.asp.net/data-access/tutorials/using-parameterized-queries-with-the-sqldatasource-vb 。 GridView 也绑定到 sqlDataSource。我的标记是:

<asp:SqlDataSource ID="mySqlDataSource" runat="server" 
    ConnectionString="<%$ ConnectionStrings:ConnectionStringHTL %>" 
    SelectCommand="SELECT [subscription_con] FROM [HTL_CONSUME_CON] WHERE ([subscription_con] = @subscription_con)">
    <SelectParameters>
        <asp:Parameter Name="subscription_con" Type="Int32" />
    </SelectParameters>
</asp:SqlDataSource>
<asp:GridView ID="myGridView" runat="server" AllowPaging="True" 
    AllowSorting="True" DataSourceID="mySqlDataSource">
</asp:GridView>

在代码隐藏中,我有:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load    
    mySqlDataSource.SelectParameters("subscription_con").DefaultValue = calcResult()
End Sub

calcResult() 的返回值对于每个回发都是不同的。当用户单击表单上 UseSubmitBehavior=True 的按钮时,会发生回发。

我使用调试器单步执行后面的代码,我看到它为每个页面加载执行,并且我看到从 clacResult() 返回的预期值。

但是,绑定的 DataGrid 永远不会在回发时更新,它仅在第一页加载时更新。

如果我更改 SqlDataSource 参数以将控件作为源,那么它可以在回发上工作。换句话说,我将标记更改为使用:

<asp:ControlParameter ControlID="myTextBox" Name="subscription_con" PropertyName="Text" Type="Int32" />

并将后面的代码更改为:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load    
   myTextBox.Text = calcResult()
End Sub

使用 TextBox 控件作为 SqlDataSource 参数的源,GridView 的更新适用于原始页面加载和所有回发。但是,我确实不需要 TextBox,并且不想使用它。

关于如何以编程方式为 SqlDataSource 设置参数,我缺少什么?在没有控制源的情况下,以编程方式设置 SqlDataSource 参数时,为什么绑定的 GridView 不会在回发时更新?

I want to set a parameter for a SqlDataSource programmatically as described in Step 5 at http://www.asp.net/data-access/tutorials/using-parameterized-queries-with-the-sqldatasource-vb . Also a GridView is bound to the sqlDataSource. My markup is:

<asp:SqlDataSource ID="mySqlDataSource" runat="server" 
    ConnectionString="<%$ ConnectionStrings:ConnectionStringHTL %>" 
    SelectCommand="SELECT [subscription_con] FROM [HTL_CONSUME_CON] WHERE ([subscription_con] = @subscription_con)">
    <SelectParameters>
        <asp:Parameter Name="subscription_con" Type="Int32" />
    </SelectParameters>
</asp:SqlDataSource>
<asp:GridView ID="myGridView" runat="server" AllowPaging="True" 
    AllowSorting="True" DataSourceID="mySqlDataSource">
</asp:GridView>

In the codebehind, I have:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load    
    mySqlDataSource.SelectParameters("subscription_con").DefaultValue = calcResult()
End Sub

The return value from calcResult() is different for each postback. The postback occurs when the user clicks a Button on the form that has UseSubmitBehavior=True.

I've use the debugger to step through the code behind and I see it executed for each page load and I see the expected values returned from clacResult().

However the bound DataGrid is never updated on the postbacks, it only updates on the first page load.

If I change the SqlDataSource parameter to have a control as the source, then it works on postbacks. In otherwords, I changed the markup to use:

<asp:ControlParameter ControlID="myTextBox" Name="subscription_con" PropertyName="Text" Type="Int32" />

and I changed the code behind to be:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load    
   myTextBox.Text = calcResult()
End Sub

With the TextBox control as the source of the SqlDataSource parameter, update of the GridView works for the original page load and all postbacks. However, I really have no need for the TextBox and would prefer not to use it.

What am I missing about how to set a parameter programmatically for an SqlDataSource? Why doesn't the bound GridView get updated on postbacks when setting the SqlDataSource parameter programmatically when there is no control source?

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

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

发布评论

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

评论(2

日暮斜阳 2024-09-11 09:55:48

您可能需要一个关于回发的显式数据库:

if (Page.IsPostBack)
{
    grid.DataBind();
}

应该这样做。

You may need an explict databaind on postbacks:

if (Page.IsPostBack)
{
    grid.DataBind();
}

Should do it.

云朵有点甜 2024-09-11 09:55:48

C#

SqlDataSource1.SelectParameters["Where_Clause"] = new Parameter() { Name = "Where_Clause", DefaultValue = "WHERE m.Id = 1" };

贪婪网络。

C#

SqlDataSource1.SelectParameters["Where_Clause"] = new Parameter() { Name = "Where_Clause", DefaultValue = "WHERE m.Id = 1" };

Greedy Networks.

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