asp.net中的gridview控件选择项问题,急
请问我在asp.net中引入了一个gridview控件,并连接到数据库某一个表中,现在我想通过按下gridview中某一行后面的按钮,来将此行对应id号的记录字段存入数据...
请问我在asp.net中引入了一个gridview控件,并连接到数据库某一个表中,现在我想通过按下gridview中某一行后面的按钮,来将此行对应id号的记录字段存入数据库中另一个表中,并在gridview控件下面的detailsview控件中进行显示,怎么能在gridview中找到对应的id号记录呢?怎么才能完成这些操作呢?我很着急,谢谢了!
展开
1个回答
展开全部
看来你是用的ASP.NET2.0了.关键是确定选择行的数据键,这样:
GridView1.DataKeys[GridView1.SelectedIndex].Value;
确定了ID值,一切都解决了吧.
.aspx里:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="EmployeeID"
DataSourceID="SqlDataSource1" OnSelectedIndexChanged="GridView1_SelectedIndexChanged">
<Columns>
<asp:BoundField DataField="EmployeeID" HeaderText="EmployeeID" InsertVisible="False"
ReadOnly="True" SortExpression="EmployeeID" />
<asp:BoundField DataField="EmployeeFirstName" HeaderText="EmployeeFirstName" SortExpression="EmployeeFirstName" />
<asp:BoundField DataField="EmployeeLastName" HeaderText="EmployeeLastName" SortExpression="EmployeeLastName" />
<asp:CommandField ShowSelectButton="True" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ZTGStestdbConnectionString %>"
SelectCommand="SELECT [EmployeeID], [EmployeeFirstName], [EmployeeLastName] FROM [Employees]">
</asp:SqlDataSource>
<br />
<asp:DetailsView ID="DetailsView1" runat="server" DataSourceID="SqlDataSource2"
Height="50px" Width="125px">
</asp:DetailsView>
<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:ZTGStestdbConnectionString %>" SelectCommand="SELECT [EmployeeFirstName], [EmployeeLastName], [EmployeeSex], [EmployeeAddress], [EmployeePhone], [EmployeeImagePath] FROM [Employees] WHERE ([EmployeeID] = @EmployeeID)">
<SelectParameters>
<asp:ControlParameter ControlID="GridView1" Name="EmployeeID" PropertyName="SelectedValue"
Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
隐藏代码里:
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
int id = nt32.Parse(GridView1.DataKeys[GridView1.SelectedIndex].Value.ToString());
//你的插入数据库新表的代码
}
GridView1.DataKeys[GridView1.SelectedIndex].Value;
确定了ID值,一切都解决了吧.
.aspx里:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="EmployeeID"
DataSourceID="SqlDataSource1" OnSelectedIndexChanged="GridView1_SelectedIndexChanged">
<Columns>
<asp:BoundField DataField="EmployeeID" HeaderText="EmployeeID" InsertVisible="False"
ReadOnly="True" SortExpression="EmployeeID" />
<asp:BoundField DataField="EmployeeFirstName" HeaderText="EmployeeFirstName" SortExpression="EmployeeFirstName" />
<asp:BoundField DataField="EmployeeLastName" HeaderText="EmployeeLastName" SortExpression="EmployeeLastName" />
<asp:CommandField ShowSelectButton="True" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ZTGStestdbConnectionString %>"
SelectCommand="SELECT [EmployeeID], [EmployeeFirstName], [EmployeeLastName] FROM [Employees]">
</asp:SqlDataSource>
<br />
<asp:DetailsView ID="DetailsView1" runat="server" DataSourceID="SqlDataSource2"
Height="50px" Width="125px">
</asp:DetailsView>
<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:ZTGStestdbConnectionString %>" SelectCommand="SELECT [EmployeeFirstName], [EmployeeLastName], [EmployeeSex], [EmployeeAddress], [EmployeePhone], [EmployeeImagePath] FROM [Employees] WHERE ([EmployeeID] = @EmployeeID)">
<SelectParameters>
<asp:ControlParameter ControlID="GridView1" Name="EmployeeID" PropertyName="SelectedValue"
Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
隐藏代码里:
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
int id = nt32.Parse(GridView1.DataKeys[GridView1.SelectedIndex].Value.ToString());
//你的插入数据库新表的代码
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询