C# Asp.net gridview,在编辑模式下在后面的代码中获取边界字段值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1184563/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Asp.net gridview, get boundfield value in code behind when in edit mode
提问by
In asp.net (c#) I'm using a gridview and edit mode to make it easy for the user to change values. But I need to check the value that the user enter in OnRowUpdating, when using this code, I always get the old value not the new value I entered. How can I get the new value?
在 asp.net (c#) 中,我使用 gridview 和编辑模式来方便用户更改值。但是我需要检查用户在 OnRowUpdating 中输入的值,使用此代码时,我总是得到旧值而不是我输入的新值。我怎样才能获得新的价值?
<asp:GridView ID="MyGridView" OnRowUpdating="MyGridViewUpdate" DataKeyNames="id,value">
<Columns>
<asp:BoundField ReadOnly="false" DataField="value" DataFormatString="{0:0.##}" />
<asp:CommandField ShowEditButton="true" EditImageUrl="~/images/iconedit.png" ButtonType="Image" CancelImageUrl="~/images/iconclose.png" UpdateImageUrl="~/images/iconedit.png" />
</Columns>
</asp:GridView>
In codebehind:
在代码隐藏中:
protected void MyGridViewUpdate(object sender, GridViewUpdateEventArgs e)
{
string test = e.NewValues["value"].ToString();
//Test always give the old value, I need the new value the user added..
}
回答by Fernando
Try using the OnRowUpdated
event. The OnRowUpdating
is raised before the grid updates the row. The OnRowUpdated
is raised after the grid updates the row.
尝试使用该OnRowUpdated
事件。该OnRowUpdating
网格更新行之前上升。该OnRowUpdated
网格更新一行之后上升。
回答by Dan Diplo
I've just done a quick test with a GridView and SqlDataSource and e.NewValues["value"]
gave me the new value, as expected. What you are doing is correct, so far as the code you posted.
我刚刚对 GridView 和 SqlDataSource 进行了快速测试e.NewValues["value"]
,并按预期为我提供了新值。就您发布的代码而言,您正在做的事情是正确的。
The only thing I can think of is that you have not set AutoGenerateColumns="false"
on your GridView when you are using BoundFields. If you don't set it you will get two sets of columns - the BoundFields and the generated ones. This will cause a clash, as you will be sending two columns with the same name.
我唯一能想到的是AutoGenerateColumns="false"
你在使用 BoundFields 时没有在你的 GridView 上设置。如果您不设置它,您将获得两组列 - BoundFields 和生成的列。这将导致冲突,因为您将发送两个具有相同名称的列。
If that isn't the problem, the you will need to post more code.
如果这不是问题,您将需要发布更多代码。
回答by Dan Diplo
Strange, I'm using autogenerate=false but it isn't working, this is my full code:
奇怪,我正在使用 autogenerate=false 但它不起作用,这是我的完整代码:
<asp:SqlDataSource ID="DSValues"
runat="server"
DataSourceMode="DataSet"
ConnectionString="..."
ProviderName="MySql.Data.MySqlClient"
SelectCommand="CALL spValues();"
UpdateCommand="UPDATE table SET value=?value WHERE id=?id;">
</asp:SqlDataSource>
<asp:GridView ID="MyGridView" OnRowUpdating="MyGridViewUpdate" DataKeyNames="id,value" DataSourceID="DSValues" AutoGenerateColumns="false">
<Columns>
<asp:BoundField ReadOnly="false" DataField="value" DataFormatString="{0:0.##}" />
<asp:CommandField ShowEditButton="true" EditImageUrl="~/images/iconedit.png" ButtonType="Image" CancelImageUrl="~/images/iconclose.png" UpdateImageUrl="~/images/iconedit.png" />
</Columns>
</asp:GridView>
//SpValues() is a stored procedure that just do: "SELECT id,value FROM table"
This worked:
这有效:
(MyGridView.Rows[e.RowIndex].Cells[1].Controls[0] as TextBox).Text
I could just use that, but it would be fine to know how to use "NewValues" instead...
我可以使用它,但是知道如何使用“NewValues”代替就好了......
回答by Dan Diplo
Ahh, looking more carefully I think I see the problem:
啊,仔细看我想我看到了问题:
You have added both id andvalue to the DataKeyNames property of the GridView eg. DataKeyNames="id,value"
. This is like specifying they are both primary keys. If you want value to be editable then you should remove it from the DataKeyNames.
您已将 id和value添加到 GridView 例如的 DataKeyNames 属性。DataKeyNames="id,value"
. 这就像指定它们都是主键一样。如果您希望值可编辑,则应将其从 DataKeyNames 中删除。
回答by Raghunath
<asp:GridView id="gvDiagnostics" runat="server"
AllowPaging="True" AutoGenerateColumns="False"
BackColor="White" BorderColor="#DEDFDE"
BorderStyle="None" BorderWidth="1px"
CellPadding="4" CssClass="label" ForeColor="Black"
GridLines="Vertical" Width="100%"
DataKeyNames="SNo">
<footerstyle backcolor="#CCCC99" />
<columns>
<asp:BoundField DataField="SNo" HeaderText="SNo">
<ItemStyle HorizontalAlign="Left" VerticalAlign="Middle"></ItemStyle>
<HeaderStyle HorizontalAlign="Left" VerticalAlign="Middle"></HeaderStyle>
</asp:BoundField>
<%--<asp:BoundField DataField="ProductId" SortExpression="ProductId" HeaderText="S.No"></asp:BoundField>--%>
<asp:TemplateField SortExpression="Code" HeaderText="Code">
<EditItemTemplate>
<asp:TextBox runat="server" Text='<%# Bind("Code") %>' id="TextBox1"></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:LinkButton id="lbtnCode" runat="server" Text='<%# Eval("Code") %>' OnClick="lbtnSNo_Click"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="ShortList" SortExpression="ShortList" HeaderText="ShortList"></asp:BoundField>
<asp:BoundField DataField="Description" SortExpression="Description" HeaderText="Description"></asp:BoundField>
</columns>
<rowstyle backcolor="#F7F7DE" />
<emptydatatemplate>
<asp:LinkButton id="lbtnProductId" runat="server" Text='<%# Eval("ProductCategory") %>'></asp:LinkButton>
</emptydatatemplate>
<selectedrowstyle backcolor="#C04000" font-bold="True" forecolor="White" />
<pagerstyle backcolor="#F7F7DE" forecolor="Black" horizontalalign="Right" />
<headerstyle backcolor="#6B696B" font-bold="True" forecolor="White" />
<alternatingrowstyle backcolor="White" />
</asp:GridView>