问题
I have a GridView control in my .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>
I have an associated SqlDataSource which allow me to use a stored procedure to display my Surnames :
<asp:SqlDataSource
ID="sqlDataSource_1"
runat="server"
ConnectionString="<%$ ConnectionStrings:myConString %>"
SelectCommand="procedure_Select_surnames"
SelectCommandType="StoredProcedure">
</asp:SqlDataSource>
The GridView control perfectly display what I want to show.
QUESTION : Now how can I update my rows thanks to a stored procedure without leaving the current page ?
回答1:
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 GridView.
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 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>
Step 2
Add the event handler OnRowUpdating which occurs when you hit the button "update" but before the user valid the update :
<asp:GridView
ID="gridView_1"
<!-- others parameters -->
OnRowUpdating="gridView_1_RowUpdating" />
<!-- ... -->
</asp:GridView>
Step 3
You will now update the function we just added, 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
Here is the content of the 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 +"'";
}
Explaination :
When your click on "update", all the field instead of the key are becoming textBox. These textBox contains the new (or not) value of the field you edit.
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.
来源:https://stackoverflow.com/questions/28717301/get-datafield-value-while-updating-row-asp-gridview-asp-net-c-sharp