为SqlDataSource的编程不适合回传ASP.NET工作设置参数 [英] ASP.NET Setting parameter for SqlDataSource programatically does not work for postback

查看:129
本文介绍了为SqlDataSource的编程不适合回传ASP.NET工作设置参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要设置一个SqlDataSource编程参数作为<一个步骤5中所述href=\"http://www.asp.net/data-access/tutorials/using-parameterized-queries-with-the-sqldatasource-vb\" rel=\"nofollow\">http://www.asp.net/data-access/tutorials/using-parameterized-queries-with-the-sqldatasource-vb 。另外一个GridView绑定到SqlDataSource控件。我的标记是:

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>

在codebehind,我有:

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

从calcResult()的返回值是每次回发不同。当用户点击一个具有UseSubmitBehavior表单上的按钮时,会出现回发= TRUE。

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.

我已经使用调试器来逐步落后于code和我看到它的每一页加载执行,我看到预期值从clacResult()。

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().

但所绑定的DataGrid从不更新的回传,它仅在第一个页面加载更新。

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

如果我改变SqlDataSource的参数有一个控制为源,那么它的工作原理上回发。在otherwords,我改变了标记的使用方法:

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" />

和我换了code后面的是:

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

使用TextBox控件作为参数的SqlDataSource之源,在GridView的更新工程,为原来的页面加载和所有回发。不过,我实在是没有必要的文本框,并会preFER不使用它。

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.

我缺少有关如何为将SqlDataSource编程设置的参数?为什么不以编程方式设置在SqlDataSource参数时,当没有控制源绑定的GridView控件得到更新上回发?

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?

推荐答案

您可能需要在回发的露骨databaind:

You may need an explict databaind on postbacks:

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

应该这样做。

这篇关于为SqlDataSource的编程不适合回传ASP.NET工作设置参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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