gpt4 book ai didi

asp.net - 使用多个数据键时,DataKeyNames 属性无法正常工作

转载 作者:行者123 更新时间:2023-12-05 07:45:08 26 4
gpt4 key购买 nike

我在我的 aspx 页面中有一个 gridview 和一个 detailview 并使用对象数据源来显示数据,表上的主键由 2 列组成,所以我在 Gridview1 中设置 DataKeyNames = "ID,StepCount",看我的网站页面代码如下:

<asp:GridView ID="GridView1" DataKeyNames="ID,StepCount" runat="server" AutoGenerateColumns="False" DataSourceID="ObjectDataSource1">
<Columns>
<asp:CommandField ShowSelectButton="True" />
<asp:BoundField DataField="ID" HeaderText="ID" SortExpression="ID" />
<asp:BoundField DataField="StepCount" HeaderText="StepCount" SortExpression="StepCount" />
<asp:BoundField DataField="Direction" HeaderText="Direction" SortExpression="Direction" />
<asp:BoundField DataField="StatusChangeTo" HeaderText="StatusChangeTo" SortExpression="StatusChangeTo" />
</Columns>
</asp:GridView>
<br />
<asp:DetailsView ID="DetailsView1" DataKeyNames="ID,StepCount" runat="server" Height="50px" Width="125px" AutoGenerateRows="False"
DataSourceID="ObjectDataSource2">
<Fields>
<asp:BoundField DataField="ID" HeaderText="ID" SortExpression="ID" />
<asp:BoundField DataField="StepCount" HeaderText="StepCount" SortExpression="StepCount" />
<asp:BoundField DataField="Direction" HeaderText="Direction" SortExpression="Direction" />
<asp:BoundField DataField="StatusChangeTo" HeaderText="StatusChangeTo" SortExpression="StatusChangeTo" />
<asp:BoundField DataField="TicketID" HeaderText="TicketID" SortExpression="TicketID" />
<asp:BoundField DataField="StandaloneIncident" HeaderText="StandaloneIncident" SortExpression="StandaloneIncident" />
<asp:BoundField DataField="CETransactionXML" HeaderText="CETransactionXML" SortExpression="CETransactionXML" />
</Fields>
</asp:DetailsView>
<br />
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server" SelectMethod="getAllMastterChildren"
TypeName="HarnessMaintain.Pages.DataAccessLayer"></asp:ObjectDataSource>
<br />
<asp:ObjectDataSource ID="ObjectDataSource2" runat="server" SelectMethod="getMasterchildByPK"
TypeName="HarnessMaintain.Pages.DataAccessLayer">
<SelectParameters>
<asp:ControlParameter ControlID="GridView1" Name="ID" PropertyName="SelectedValue"
Type="Int32" />
<asp:ControlParameter ControlID="GridView1" Name="StepCount" PropertyName="SelectedValue"
Type="Int32" />
</SelectParameters>
</asp:ObjectDataSource>

以下是我的 C# 代码,用于根据从 Gridview 中选择的 ID 和步数检索数据:

public static List<MasterChildDetail> getMasterchildByPK(int ID, int StepCount)
{
string sqlStr = "select * from mswscemasterchild where id = :id and step_count = :step_count";
List<MasterChildDetail> mcds = new List<MasterChildDetail>();
MasterChildDetail mcd = new MasterChildDetail();
using (OracleConnection ocon = new OracleConnection(conStr))
{
OracleCommand ocmd = new OracleCommand(sqlStr, ocon);
ocmd.Parameters.Add(":id", ID);
ocmd.Parameters.Add(":step_count", StepCount);

ocmd.Connection.Open();
OracleDataReader reader = ocmd.ExecuteReader();
while (reader.Read())
{
mcd.ID = Convert.ToInt32(reader["ID"]);
mcd.StepCount = Convert.ToInt32(reader["STEP_COUNT"]);
if (System.DBNull.Value.Equals(reader["DIRECTION"]))
{
mcd.Direction = null;
}
else
{
mcd.Direction = reader["DIRECTION"].ToString();
}

if (System.DBNull.Value.Equals(reader["STATUS_CHANGE_TO"]))
{
mcd.StatusChangeTo = null;
}
else
{
mcd.StatusChangeTo = reader["STATUS_CHANGE_TO"].ToString();
}

if (System.DBNull.Value.Equals(reader["TICKET_ID"]))
{
mcd.TicketID = null;
}
else
{
mcd.TicketID = reader["TICKET_ID"].ToString();
}

if (System.DBNull.Value.Equals(reader["STANDALONE_INCIDENT"]))
{
mcd.StandaloneIncident = null;
}
else
{
mcd.StandaloneIncident = reader["STANDALONE_INCIDENT"].ToString();
}

if (System.DBNull.Value.Equals(reader["CE_TRANSACTION_XML"]))
{
mcd.CETransactionXML = null;
}
else
{
mcd.CETransactionXML = reader["CE_TRANSACTION_XML"].ToString();
}

mcds.Add(mcd);
break;
}
}
return mcds;
}

通过调试,我发现有线问题,即使我点击第二个“选择”和第三个“选择”,传递的ID和StepCount的值总是完全相同,如下所示:

enter image description here

你能告诉我为什么或如何解决这个问题吗?

最佳答案

数据库的主键列是您的 DataKey 名称。一张表中不能有 2 个主键。所以 GridView 的 DataKey 名称中应该只有 1 列。

关于asp.net - 使用多个数据键时,DataKeyNames 属性无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42174676/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com