问题描述
我有一个显示所有车次一个GridView - 这GridView控件里我有一个显示该国的行程(一对多)期间访问中继器。我的中继器是假设虎视眈眈的TRIPID,然后填充数据源,其中填充的中继器。不幸的是,我的中继器只是被填充指定的最后TRIPID。
< ASP:GridView控件ID =GridView1=服务器的AutoGenerateColumns =FALSEDataKeyNames =pkiTripID
的DataSourceID =SqlDataSource2每页=20AllowPaging =真
WIDTH =100%AllowSorting =真
网格线=无onrowdatabound =GridView1_RowDataBound1>
<列>
< ASP:绑定列的DataField =TripType1HEADERTEXT =类型SORTEX pression =TripType1/>
< ASP:绑定列的DataField =RegionNameHEADERTEXT =区域SORTEX pression =RegionName/>
< ASP:模板列HEADERTEXT =国家HeaderStyle,前景色=#FF7900>
<的ItemTemplate>
< ASP:直放站ID =RepeatCountry=服务器
的DataSourceID =dsCountryByTripIDonitemdatabound =RepeatCountry_ItemDataBound>
<的ItemTemplate>
<%#的DataBinder.Eval(的Container.DataItem,国家或地区名称)%>
< / ItemTemplate中>
< / ASP:直放站>
< / ItemTemplate中>
< HeaderStyle前景色=#FF7900/>
< / ASP:的TemplateField>
< ASP:绑定列的DataField =OverallRatingHEADERTEXT =跳闸等级
SORTEX pression =OverallRating/>
< ASP:绑定列的DataField =DepartureDateHEADERTEXT =日期SORTEX pression =DepartureDate
DataFormatString ={0:MMM-DD-YYYY}HtmlEn code =FALSE/>
< /列>
< / ASP:GridView控件>
< ASP:SqlDataSource的ID =dsCountryByTripID=服务器
的ConnectionString =<%$的ConnectionStrings:MYDB%>中
的SelectCommand =spSelectCountriesByTripIDSelectCommandType =StoredProcedure的>
< SelectParameters>
< ASP:参数类型=的Int32NAME =TRIPID默认值=96/>
< / SelectParameters>
< / ASP:SqlDataSource的>
和后面
我的code 保护无效GridView1_RowDataBound1(对象发件人,GridViewRowEventArgs E)
{
中继器RP =(中继器)e.Row.FindControl(RepeatCountry);
如果(e.Row.RowType == DataControlRowType.DataRow)
{
dsCountryByTripID.SelectParameters.Clear();
DataRowView的DRV =(DataRowView的)e.Row.DataItem;
。字符串TRIPID =(DRV [pkiTripId])的ToString();
dsCountryByTripID.SelectParameters.Clear();
dsCountryByTripID.SelectParameters.Add(TRIPID,DbType.Int32,TRIPID);
//// gv2.DataBind();
//e.Row.DataBind();
}
}
保护无效RepeatCountry_ItemDataBound(对象发件人,RepeaterItemEventArgs E)
{
e.Item.DataBind();
}
我决定不使用数据源,并用我自己的,我给了我更多的灵活性结合GridView控件中的中继器(无需GridView控件),移动到前下一行。
I have a gridview that displays all trips - within that gridview I have a repeater that displays the countries visited during that trip (one to many). My repeater is suppose to grap the tripID and then populate the datasource, which populates the repeater. Unfortunately, my repeater is just being populated with the last tripID specified.
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="pkiTripID"
DataSourceID="SqlDataSource2" PageSize="20" AllowPaging="True"
Width="100%" AllowSorting="True"
GridLines="None" onrowdatabound="GridView1_RowDataBound1">
<Columns>
<asp:BoundField DataField="TripType1" HeaderText="Type" SortExpression="TripType1" />
<asp:BoundField DataField="RegionName" HeaderText="Region" SortExpression="RegionName" />
<asp:TemplateField HeaderText="Country" HeaderStyle-ForeColor="#FF7900">
<ItemTemplate>
<asp:Repeater ID="RepeatCountry" runat="server"
DataSourceID="dsCountryByTripID" onitemdatabound="RepeatCountry_ItemDataBound">
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem, "CountryName") %>
</ItemTemplate>
</asp:Repeater>
</ItemTemplate>
<HeaderStyle ForeColor="#FF7900" />
</asp:TemplateField>
<asp:BoundField DataField="OverallRating" HeaderText="Trip Rating"
SortExpression="OverallRating" />
<asp:BoundField DataField="DepartureDate" HeaderText="Date" SortExpression="DepartureDate"
DataFormatString="{0:MMM-dd-yyyy}" HtmlEncode="false" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="dsCountryByTripID" runat="server"
ConnectionString="<%$ ConnectionStrings:myDB %>"
SelectCommand="spSelectCountriesByTripID" SelectCommandType="StoredProcedure">
<SelectParameters>
<asp:Parameter Type="Int32" Name="tripID" DefaultValue="96" />
</SelectParameters>
</asp:SqlDataSource>
And my code behind
protected void GridView1_RowDataBound1(object sender, GridViewRowEventArgs e)
{
Repeater rp = (Repeater)e.Row.FindControl("RepeatCountry");
if (e.Row.RowType == DataControlRowType.DataRow)
{
dsCountryByTripID.SelectParameters.Clear();
DataRowView drv = (DataRowView)e.Row.DataItem;
string tripID = (drv["pkiTripId"]).ToString();
dsCountryByTripID.SelectParameters.Clear();
dsCountryByTripID.SelectParameters.Add("tripID", DbType.Int32, tripID);
////gv2.DataBind();
//e.Row.DataBind();
}
}
protected void RepeatCountry_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
e.Item.DataBind();
}
I decided not to use DataSource and use my own, I gave me more flexibility to bind the repeater(no need for gridview) within the gridview, before moving to the next row.
这篇关于一个GridView内中继器 - 中继器从最后gridview的ID仅拉动(导致每个中继器是相同的)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!