在更新行时获取DataField值(asp GridView)asp.NET C# [英] Get DataField value while updating row (asp GridView) asp.NET C#
问题描述
在我的 .aspx
文件中有一个GridView控件,该文件由我的表( Id ,姓氏 ):
< asp:GridView
ID =gridView_1
runat =server
AutoGenerateColumns =False<! - 用于自定义我的列 - >
DataKeyNames =Id
DataSourceID =sqlDataSource_1
OnRowUpdating =gridView_rolesTiers_RowUpdating>
<列>
< asp:BoundField
DataField =dbColumn_db
HeaderText =Id
InsertVisible =False
ReadOnly =True/>
< asp:BoundField
DataField =dbColumn_Surname
HeaderText =姓氏
SortExpression =RLT_Intitule>
< asp:CommandField
ShowEditButton =True/>
< /列>
< / asp:GridView>
我有一个关联的SqlDataSource,它允许我使用存储过程来显示我的姓氏:
< asp:SqlDataSource
ID =sqlDataSource_1
runat =server
ConnectionString =<%$ ConnectionStrings:myConString%>
SelectCommand =procedure_Select_surnames
SelectCommandType =StoredProcedure>
< / asp:SqlDataSource>
GridView
控件完美显示我想要的内容以显示。
问题:现在,如何通过存储过程更新我的行而无需离开当前页面?
我发现这个问题的答案不是我所期望的,也不是那么复杂,改变我的 GridView
的结构。
以下是一个快速解决方案,供那些搜索简单方法使用存储过程更新行的人:
第1步
添加 CommandField 在您的 GridView
控件中,以允许用户更新一行。这个 CommandField
必须放入< Columns> <! - 此处 - > < / Columns>
标记:
< asp:GridView
ID = gridView_1
runat =server
AutoGenerateColumns =False<! - 用于自定义列 - >
DataKeyNames =Id
DataSourceID =sqlDataSource_1
OnRowUpdating =gridView_rolesTiers_RowUpdating>
<列>
<! - 您的先行行 - >
< asp:CommandField ShowEditButton =True/>
< /列>
<! - nexts attributes - >
< / asp:GridView>
第2步
添加事件处理程序 第3步 现在您将更新刚添加的函数, 这里是 解释: 当您点击更新时,所有字段而不是密钥都变为 所有这些值都存储在一个数组中, NewValues []。您可以使用每个文本框更新的索引(从左到右索引顺序,从0到n)访问这些数据。 希望对需要帮助的人有所帮助执行这样一个过程。 I have a GridView control in my I have an associated SqlDataSource which allow me to use a stored procedure to display my Surnames : The QUESTION : Now how can I update my rows thanks to a stored procedure without leaving the current page ? I found out that the answer to this question wasn't the one I expected, or was so complicated that it would need to change the structure of my Here is a quick solution for those who search an easy way to update a row with a stored procedure : Step 1 Add a CommandField in your Step 2 Add the event handler OnRowUpdating which occurs when you hit the button "update" but before the user valid the update : Step 3 You will now update the function we just added, Here is the content of the Explaination : When your click on "update", all the field instead of the key are becoming All these value are stored in an array, NewValues[]. You can access these data by using the index of each textBox updating (from left to right for the index order, from 0 to n). To finish, I use the stored procedure by correctly filling the parameter with the value I need, and the line is automatically updated by triggering the behaviour of the stored procedure. Hope it helps to those who need to perform such a process. 这篇关于在更新行时获取DataField值(asp GridView)asp.NET C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
< asp:GridView
ID =gridView_1
<! - 其他参数 - >
OnRowUpdating =gridView_1_RowUpdating/>
<! - ... - >
< / asp:GridView>
gridView_1_RowUpdating
以获取我们需要更新的变量。以下是更新存储过程的样子:
CREATE PROCEDURE [dbo]。[storedProcedure_Update_Surnames ]
@id int
,@surname varchar(50)
AS
UPDATE [dbo]。[table_Surnames]
SET dbColumn_Surname = @surname
WHERE dbColumn_Id = @id
RETURN 0
gridView_1_RowUpdating
方法:
protected void gridView_1_RowUpdating(object sender,GridViewUpdateEventArgs e)
{
string surname = e.NewValues [0] .ToString(); / * NewValues是关键* /
string id = gridView_1.DataKeys [e.RowIndex] .Values [0] .ToString(); / *获取id * /
sqlDataSource_1.UpdateCommand =[storedProcedure_Update_Surnames]+ id +,'+ surname +';
}
textBox
。这些文本框包含您编辑的字段的新(或不)值。
.aspx
file which is filled by my table (Id, Surname) :<asp:GridView
ID="gridView_1"
runat="server"
AutoGenerateColumns="False" <!-- used to customize my columns -->
DataKeyNames="Id"
DataSourceID="sqlDataSource_1"
OnRowUpdating="gridView_rolesTiers_RowUpdating">
<Columns>
<asp:BoundField
DataField="dbColumn_db"
HeaderText="Id"
InsertVisible="False"
ReadOnly="True" />
<asp:BoundField
DataField="dbColumn_Surname"
HeaderText="Surname"
SortExpression="RLT_Intitule">
<asp:CommandField
ShowEditButton="True" />
</Columns>
</asp:GridView>
<asp:SqlDataSource
ID="sqlDataSource_1"
runat="server"
ConnectionString="<%$ ConnectionStrings:myConString %>"
SelectCommand="procedure_Select_surnames"
SelectCommandType="StoredProcedure">
</asp:SqlDataSource>
GridView
control perfectly display what I want to show. GridView
. GridView
control to allow user to update a row. This CommandField
has to be placed into <Columns> <!-- here --> </Columns>
tags :<asp:GridView
ID="gridView_1"
runat="server"
AutoGenerateColumns="False" <!-- used to customize my columns -->
DataKeyNames="Id"
DataSourceID="sqlDataSource_1"
OnRowUpdating="gridView_rolesTiers_RowUpdating">
<Columns>
<!-- your precedent rows -->
<asp:CommandField ShowEditButton="True" />
</Columns>
<!-- nexts attributes -->
</asp:GridView>
<asp:GridView
ID="gridView_1"
<!-- others parameters -->
OnRowUpdating="gridView_1_RowUpdating" />
<!-- ... -->
</asp:GridView>
gridView_1_RowUpdating
to get the variables we need to update. Here is what the updating stored procedure could look like : CREATE PROCEDURE [dbo].[storedProcedure_Update_Surnames]
@id int
, @surname varchar(50)
AS
UPDATE [dbo].[table_Surnames]
SET dbColumn_Surname = @surname
WHERE dbColumn_Id = @id
RETURN 0
gridView_1_RowUpdating
method :protected void gridView_1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
string surname = e.NewValues[0].ToString(); /* NewValues is the key */
string id = gridView_1.DataKeys[e.RowIndex].Values[0].ToString(); /* get the id */
sqlDataSource_1.UpdateCommand = "[storedProcedure_Update_Surnames] " + id + ", '" + surname +"'";
}
textBox
. These textBox contains the new (or not) value of the field you edit.