尝试在 rowDataBound 期间向我的 sqldatasource 添加参数

发布于 2024-09-14 06:55:51 字数 1217 浏览 0 评论 0原文

所以我在 gridview 中有一个 gridview (我有一个一对多表),我的第一个 gridview 运行良好,但我的第二个 gridview 有一个 sqldatasource,

<asp:SqlDataSource ID="dsCountryByTripID" runat="server" 
        ConnectionString="<%$ ConnectionStrings:bahDatabase %>" 
        SelectCommand="spSelectCitiesByTripID" SelectCommandType="StoredProcedure">
        <SelectParameters>
        <asp:Parameter Type="Int32" Name="tripID"  DefaultValue="56" />
        </SelectParameters>
    </asp:SqlDataSource>

在我的 Gridview1 行数据绑定期间 有一个选择参数(默认值仅用于测试)我正在尝试获取与 tripID 匹配的列。但是 dsCountryByTripID 这是我的数据源,仅使用最后一个 tripID 输入。

 protected void GridView1_RowDataBound1(object sender, GridViewRowEventArgs e)
    {
        GridView gv2 = (GridView)e.Row.FindControl("GridView2");
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            dsCountryByTripID.SelectParameters.Clear();
            DataRowView drv = (DataRowView)e.Row.DataItem;
            string tripID = (drv["pkiTripId"]).ToString();
            dsCountryByTripID.SelectParameters.Add("tripID", DbType.Int32, tripID);
            //gv2.DataBind();
            //e.Row.DataBind();

        }
    }

So I have a gridview within a gridview (I have a one to many table) my first gridview is working well, but my second gridview has a sqldatasource that has a select parameter(the default value was just for testing)

<asp:SqlDataSource ID="dsCountryByTripID" runat="server" 
        ConnectionString="<%$ ConnectionStrings:bahDatabase %>" 
        SelectCommand="spSelectCitiesByTripID" SelectCommandType="StoredProcedure">
        <SelectParameters>
        <asp:Parameter Type="Int32" Name="tripID"  DefaultValue="56" />
        </SelectParameters>
    </asp:SqlDataSource>

during my Gridview1 row databound I am trying to grab the columns that match the tripID. But dsCountryByTripID which is my datasource, is only going inputted with the last tripID.

 protected void GridView1_RowDataBound1(object sender, GridViewRowEventArgs e)
    {
        GridView gv2 = (GridView)e.Row.FindControl("GridView2");
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            dsCountryByTripID.SelectParameters.Clear();
            DataRowView drv = (DataRowView)e.Row.DataItem;
            string tripID = (drv["pkiTripId"]).ToString();
            dsCountryByTripID.SelectParameters.Add("tripID", DbType.Int32, tripID);
            //gv2.DataBind();
            //e.Row.DataBind();

        }
    }

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

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

发布评论

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

评论(2

蓝颜夕 2024-09-21 06:55:51

在这里您可以阅读为什么它只执行最后一个“tripId”: http://msdn.microsoft.com/en-us/library/tw738475(VS.80).aspx
sql 数据源仅在页面末尾执行,因此每次浏览一行时,tripID 都会被覆盖。

您真的需要使用数据源吗?你不能使用 ado.net 或其他东西来进行数据访问吗?

希望这有帮助。

here you can read why it only executes the last "tripId": http://msdn.microsoft.com/en-us/library/tw738475(VS.80).aspx
The sql datasource only gets executed at the end of the page, so every time you go through a row the tripID is overriden.

Do you really need to use the datasource? Can't you use ado.net or something different for you dataaccess?

Hope this helps.

凑诗 2024-09-21 06:55:51

首先,我们需要澄清页面的结构。在我看来,dsCountryByTripID 是在 Gridview1 外部声明的,而不是在 Gridview1 内部声明的。这可能就是为什么您的 dsCountryByTripID 仅使用最后一个 tripID 输入的原因。

为了执行您想要的操作,结构应如下所示:

<asp:gridview id="gv1" runat="server" DataSourceID="ds1">
...
    <asp:gridview id="gv2nested" runat="server" DataSourceID="dsCountryByTripID">
    </asp:gridview>
    <asp:SqlDataSource ID="dsCountryByTripID" runat="server" 
    ConnectionString="<%$ ConnectionStrings:bahDatabase %>" 
    SelectCommand="spSelectCitiesByTripID" SelectCommandType="StoredProcedure">
        <SelectParameters>
            <asp:Parameter Type="Int32" Name="tripID"  DefaultValue="56" />
        </SelectParameters>
    </asp:SqlDataSource>
...
</asp:gridview>
<asp:SqlDataSource ID="ds1" runat="server" >
</asp:SqlDataSource>

接下来,要将正确的值分配给内部数据源选择参数,您可以在外部 gridview (gv1) RowCreated 事件处理程序中执行此操作。

protected void gv1_RowCreated(object sender, GridViewRowEventArgs e)
    {
        //Retrieve the inner gridview
        //Retrieve the inner sqldatasource
        //Retrieve and assign selectparameter value
    }

它与您发布的 C# 代码类似,但在将值分配给 selectparameter 之前,您还需要额外使用 FindControl 来检索现在嵌套 sqldatasource (dsCountryByTripID)。 (尝试在这里教一个人钓鱼,但如果您需要更多帮助或者以上情况不是您的情况,请告诉我:)

First, we need to clarify the structure of your page. It seems to me that dsCountryByTripID is declared outside of Gridview1, instead of inside Gridview1. That's probably why your dsCountryByTripID is only going inputted with the last tripID.

In order to do what you want, the structure should be like this:

<asp:gridview id="gv1" runat="server" DataSourceID="ds1">
...
    <asp:gridview id="gv2nested" runat="server" DataSourceID="dsCountryByTripID">
    </asp:gridview>
    <asp:SqlDataSource ID="dsCountryByTripID" runat="server" 
    ConnectionString="<%$ ConnectionStrings:bahDatabase %>" 
    SelectCommand="spSelectCitiesByTripID" SelectCommandType="StoredProcedure">
        <SelectParameters>
            <asp:Parameter Type="Int32" Name="tripID"  DefaultValue="56" />
        </SelectParameters>
    </asp:SqlDataSource>
...
</asp:gridview>
<asp:SqlDataSource ID="ds1" runat="server" >
</asp:SqlDataSource>

Next, to assign the correct value to the inner datasource selectparameter, you can do that in the outer gridview (gv1) RowCreated event handler.

protected void gv1_RowCreated(object sender, GridViewRowEventArgs e)
    {
        //Retrieve the inner gridview
        //Retrieve the inner sqldatasource
        //Retrieve and assign selectparameter value
    }

It's similar to the C# code you posted, but you need to additionally use FindControl to retrieve the now nested sqldatasource (dsCountryByTripID) before assigning the value to the selectparameter. (Trying to teach a man fishing here, but let me know if you need more help or if the above is not your case :)

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