对于我的看法,在页面:
1) I have a gridview with the select hyperlink in it. The gridview data is from the SQLDataSource.
2) And, I also have a few textboxes (abt 5) - not in the gridview.
我想这样做的是使用select hyperlink
选择,我想编辑的row
。当我点击select
,在data
的row
应该出来各自textboxes
。我该如何去这样做呢?
网格视图具有内嵌的记录编辑支持,您可以通过AutoGenerateEditButton
属性设置为true启用此功能。您必须指定在UpdateCommand
物业存储过程或SQL查询的名称,这是用来更新基础数据的基础数据。
从MSDN:
<asp:gridview id="CustomersGridView"
datasourceid="CustomersSqlDataSource"
autogeneratecolumns="true"
autogeneratedeletebutton="true"
autogenerateeditbutton="true"
datakeynames="CustomerID"
runat="server">
</asp:gridview>
<asp:sqldatasource id="CustomersSqlDataSource"
selectcommand="Select [CustomerID], [CompanyName], [Address], [City], [PostalCode], [Country] From [Customers]"
updatecommand="Update Customers SET CompanyName=@CompanyName, Address=@Address, City=@City, PostalCode=@PostalCode, Country=@Country WHERE (CustomerID = @CustomerID)"
deletecommand="Delete from Customers where CustomerID = @CustomerID"
connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>"
runat="server">
</asp:sqldatasource>
见here为完整的示例代码。
Wireup OnSelectedIndexChanged
事件:
ASPX:
<asp:GridView id="gvTest" OnSelectedIndexChanged="gvTest_SelectedIndexChanged"
..........></asp:GridView>
<asp:TextBox id="text1" runat="server"/>
码:
protected void gvTest_SelectedIndexChanged(object sender, EventArgs e)
{
//get currently selected row
var r =gvTest.Rows[gvTest.SelectedIndex];
//THIS WAY YOU CAN GET TEXT FROM ALL COLUMNS
text1.Text = r.Cells[r.Cells.Count - 1].Text;
}
protected void gvofertas_RowCommand(object sender, GridViewCommandEventArgs e)
{
try
{
gvofertas.SelectedIndex = Convert.ToInt32(e.CommandArgument);
switch (e.CommandName)
{
case "ELIMINAR":
{
//lblSolEliminar.Text = "Usuario: " + Convert.ToString(gvCorreos.DataKeys[gvCorreos.SelectedIndex].Values["etspcpusrn"]);
mpeEliminar.Show();
break;
}
case "EDITAR":
{
Limpiar();
Session["NROOFERTAACTUALIZA"] = Convert.ToString(gvofertas.DataKeys[gvofertas.SelectedIndex].Values["efophcodi"]).Trim();
txtDescripcion.Text = Convert.ToString(gvofertas.DataKeys[gvofertas.SelectedIndex].Values["efophdesc"]).Trim();
StartDate.Text= Convert.ToDateTime(gvofertas.DataKeys[gvofertas.SelectedIndex].Values["efophfini"]).ToShortDateString();
EndDate.Text = Convert.ToDateTime(gvofertas.DataKeys[gvofertas.SelectedIndex].Values["efophffin"]).ToShortDateString();
txtRango1Localidades1Agregar.Text = Convert.ToString(gvofertas.DataKeys[gvofertas.SelectedIndex].Values["efophloci"]).Trim();
txtRango2Localidades1Agregar.Text = Convert.ToString(gvofertas.DataKeys[gvofertas.SelectedIndex].Values["efophlocf"]).Trim();
this.mpeAgregar.Show();
BtnGuardar2.Text = "Actualizar";
txtDescripcion.Focus();
break;
}
}
catch (Exception ex)
{
ucMsje.RegistrarMensajeCliente("dvMsjeError", F.m_strImagenError, ex.Message);
}