Public Function GridView_RowUpdating(sender As Object, _
e As GridViewUpdateEventArgs) As Integer
Dim ID As Integer = GridView1.DataKeys(e.RowIndex).Value
Return ID
End Function
我需要在另一页上从myGridview编辑数据的id(datakey)。我该怎么办?
答案 0 :(得分:2)
这个代码可以帮助你......
<asp:GridView ID="grdViewTracking" runat="server" AllowPaging="True" AllowSorting="true"
OnSorting="grdViewTracking_Sorting" AutoGenerateColumns="False" BorderColor="#E7E7E7"
BorderStyle="Solid" BorderWidth="1px" OnPageIndexChanging="grdViewTracking_PageIndexChanging"
CellPadding="1" ForeColor="#666666" PageSize="10" Width="100%">
<Columns>
<asp:TemplateField HeaderText="#" ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<table>
<tr style="background-color: White">
<td style="padding-left: 5px;">
<%#DataBinder.Eval(Container.DataItem, "Number")%>
</td>
</tr>
</table>
</ItemTemplate>
<ItemStyle Width="5%" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Key" SortExpression="Key" ItemStyle-HorizontalAlign="Left">
<HeaderStyle HorizontalAlign="Left" CssClass="PaddingLeft5" />
<ItemTemplate>
<table>
<tr style="background-color: White">
<td style="padding-left: 5px">
***<asp:LinkButton ID="lnkkey" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "Key")%>'></asp:LinkButton>***
<input type=hidden runat="server" id="hfid" value=' <%#DataBinder.Eval(Container.DataItem, "Appsettingid")%>' />
</td>
</tr>
</table>
</ItemTemplate>
<ItemStyle Width="15%" />
</asp:TemplateField>
以下代码将控制转移到第二页,即编辑页面.....
protected void grdViewTracking_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
BusinessLogicPOS.AppSettingArgs ob = e.Row.DataItem as BusinessLogicPOS.AppSettingArgs;
LinkButton lnkkey = e.Row.FindControl("lnkkey") as LinkButton;
lnkkey.PostBackUrl = "AppSetting.aspx?AppSettingId=" + ob.AppSettingId.ToString();
}
检查参考:
答案 1 :(得分:0)
最简单的方法就是这样。
在WebForm1标记处使用这两种方法中的任何一种
一个。使用asp:TemplateField
<asp:TemplateField>
<ItemTemplate>
<asp:HyperLink ID="EditLink" runat="server"
NavigateUrl='<%# "~/WebForm2.aspx?id=" + Eval("ID") %>'
Text="Update" />
</ItemTemplate>
</asp:TemplateField>
湾使用asp:HyperLinkField
<asp:HyperLinkField Text="Update"
DataNavigateUrlFields="ID"
DataNavigateUrlFormatString="~\WebForm2.aspx?id={0}"
HeaderText="Action" />
在WebForm2代码隐藏处执行此操作
Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
If Request.QueryString("id") IsNot Nothing Then
Dim passedID As String = Request.QueryString("id")
'populate the fields using .FirstOrDefault
Else
'no id passed. cannot process
End If
End If
End Sub