我的DetailsView始终为空白。我在QueryBuilder中测试了两个数据源并返回值。我怀疑PoweredEquipDetails中的参数值赋值有问题,它们被设置为GridView1中的SelectedValue,但我不知道为什么它不起作用。
<body>
<form id="form1" runat="server">
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataSourceID="PoweredEquipHeader" DataKeyNames="Model,Serial">
<Columns>
<asp:CommandField ShowSelectButton="True" />
<asp:BoundField DataField="Model" HeaderText="Model" ReadOnly="True"
SortExpression="Model" />
<asp:BoundField DataField="Serial" HeaderText="Serial" ReadOnly="True"
SortExpression="Serial" />
</Columns>
<SelectedRowStyle BackColor="#FF6600" />
</asp:GridView>
<asp:SqlDataSource ID="PoweredEquipHeader" runat="server"
ConnectionString="<%$ ConnectionStrings:CPECConnectionString %>"
SelectCommand="Select t1.Model, t1.Serial From Powered_Equip t1 Where TechID = @TechID And Store = @Store">
<SelectParameters>
<asp:QueryStringParameter DefaultValue="TECH01" Name="TechID"
QueryStringField="Tech" />
<asp:QueryStringParameter DefaultValue="0000" Name="Store"
QueryStringField="Store" />
</SelectParameters>
</asp:SqlDataSource>
<asp:SqlDataSource ID="PoweredEquipDetail" runat="server"
ConnectionString="<%$ ConnectionStrings:CPECConnectionString %>"
SelectCommand="SELECT t1.Serial, t1.Model, t1.Status, t1.[Trailer VIN], t2.Tag, t2.State, t2.ExpMonth, t2.ExpYear FROM Powered_Equip AS t1 INNER JOIN Trailers AS t2 ON t1.[Trailer VIN] = t2.VIN WHERE (t1.Serial = @Serial) AND (t1.Model = @Model)">
<SelectParameters>
<asp:ControlParameter ControlID="GridView1" Name="Serial"
PropertyName="SelectedValue" />
<asp:ControlParameter ControlID="GridView1" Name="Model"
PropertyName="SelectedValue" />
</SelectParameters>
</asp:SqlDataSource>
<asp:DetailsView ID="DetailsView1" runat="server"
DataSourceID="PoweredEquipDetail" DataKeyNames="Serial,Model">
<Fields>
<asp:BoundField DataField="Serial" HeaderText="Serial" ReadOnly="True"
SortExpression="Serial" />
<asp:BoundField DataField="Model" HeaderText="Model"
ReadOnly="True" SortExpression="Model" />
<asp:BoundField DataField="Status" HeaderText="Status" ReadOnly="True"
SortExpression="Status" />
<asp:BoundField DataField="Trailer VIN" HeaderText="Trailer VIN" ReadOnly="True"
SortExpression="Trailer VIN" />
<asp:BoundField DataField="Tag" HeaderText="Tag" ReadOnly="True"
SortExpression="Tag" />
<asp:BoundField DataField="State" HeaderText="State" ReadOnly="True"
SortExpression="State" />
<asp:BoundField DataField="ExpMonth" HeaderText="ExpMonth" ReadOnly="True"
SortExpression="ExpMonth" />
<asp:BoundField DataField="ExpYear" HeaderText="ExpYear" ReadOnly="True"
SortExpression="ExpYear" />
</Fields>
</asp:DetailsView>
</form>
编辑:所以我相信发生的事情是GridView的DetailsV中的SelectedValue参数赋值始终是Model,而不是我的DataValueKeys中指定的Model和Serial。如果设备项具有相同值的模型和序列(即Model 118 Serial 118),则DetailsView将填充。但在任何情况下,模型和序列都不同(几乎所有其他情况),DetailsView是空白的。那么如何获得两个不同的SelectedValues来为两个不同的ControlParameters赋值?
答案 0 :(得分:1)
已更改
<SelectParameters>
<asp:ControlParameter ControlID="GridView1" Name="Serial"
PropertyName="SelectedValue" />
<asp:ControlParameter ControlID="GridView1" Name="Model"
PropertyName="SelectedValue" />
</SelectParameters>
到
<SelectParameters>
<asp:ControlParameter ControlID="GridView1" Name="Serial"
PropertyName="SelectedDataKey.Values[Serial]" />
<asp:ControlParameter ControlID="GridView1" Name="Model"
PropertyName="SelectedValue" />
</SelectParameters>
答案 1 :(得分:0)
int n = int.Parse(GridView1.SelectedValue.ToString());
TextBox4.Text = n.ToString();
con = new SqlConnection(@"Data Source=LAB-III-136;Initial Catalog=student;Integrated Security=True");
con.Open();
da = new SqlDataAdapter("select * from student1 where id='" + TextBox4.Text + " '", con);
ds = new DataSet();
da.Fill(ds, "temp");
DetailsView2.DataSource = ds;
DetailsView2.DataBind();