尝试在 rowDataBound 期间向我的 sqldatasource 添加参数
所以我在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在这里您可以阅读为什么它只执行最后一个“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.
首先,我们需要澄清页面的结构。在我看来,
dsCountryByTripID
是在 Gridview1 外部声明的,而不是在 Gridview1 内部声明的。这可能就是为什么您的dsCountryByTripID
仅使用最后一个 tripID 输入的原因。为了执行您想要的操作,结构应如下所示:
接下来,要将正确的值分配给内部数据源选择参数,您可以在外部 gridview (
gv1
) RowCreated 事件处理程序中执行此操作。它与您发布的 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 yourdsCountryByTripID
is only going inputted with the last tripID.In order to do what you want, the structure should be like this:
Next, to assign the correct value to the inner datasource selectparameter, you can do that in the outer gridview (
gv1
) RowCreated event handler.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 :)