在更新行时获取DataField值(asp GridView)asp.NET C# [英] Get DataField value while updating row (asp GridView) asp.NET C#

查看:747
本文介绍了在更新行时获取DataField值(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步

添加事件处理程序

 < asp:GridView 
ID =gridView_1
<! - 其他参数 - >
OnRowUpdating =gridView_1_RowUpdating/>
<! - ... - >
< / asp:GridView>

第3步

现在您将更新刚添加的函数, 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 。这些文本框包含您编辑的字段的新(或不)值。

所有这些值都存储在一个数组中, NewValues []。您可以使用每个文本框更新的索引(从左到右索引顺序,从0到n)访问这些数据。

完成后,我使用通过正确地填充参数和我需要的值,并通过触发存储过程的行为自动更新存储过程。



希望对需要帮助的人有所帮助执行这样一个过程。

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 ?

解决方案

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.

这篇关于在更新行时获取DataField值(asp GridView)asp.NET C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆