在gridview中,我想将数据绑定到文本框。我怎么做?任何帮助将不胜感激。 [英] In the gridview, I want to bind data to a textbox. How do I do that? Any help would be greatly appreciated.
问题描述
我的GridView中有一个文本框。我想将该文本框与所需的数据绑定。我怎么做?以下是我的aspx& aspx.vb文件。
aspx:
I have a textbox in my GridView. I want to bind that textbox with required Data. How do I do that? Below are my aspx & aspx.vb files.
aspx:
<asp:GridView ID="grdItems" runat="server" Width="100%" AllowPaging="True" CellPadding="4" ForeColor="#333333" GridLines="Horizontal" AutoGenerateColumns="False">
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" Font-Size="X-Small" />
<RowStyle BackColor="#EFF3FB" />
<EditRowStyle BackColor="#2461BF" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<pagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" Font-Size="Small" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:BoundField DataField="actionItemId" HeaderText="Item Id" >
<ItemStyle Font-Size="Small" VerticalAlign="Top" />
<HeaderStyle Font-Bold="True" Font-Size="Small" HorizontalAlign="Left" Width="65px" />
<FooterStyle Font-Size="X-Small" />
</asp:BoundField>
<asp:TemplateField HeaderText="Description" >
<ItemStyle Font-Size="Small" VerticalAlign="Top" />
<HeaderStyle Font-Size="Small" HorizontalAlign="Left" Width="265px"/>
<ItemTemplate>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Actions Taken">
<ItemTemplate>
<tr>
<td colspan="1">
<asp:TextBox runat="server" ID="actionsTB" TextMode="MultiLine"> </asp:TextBox>
</td>
</tr>
</ItemTemplate>
<ItemStyle Font-Size="Small" VerticalAlign="Top" />
<HeaderStyle Font-Bold="True" Font-Size="Small" HorizontalAlign="Left" />
</asp:TemplateField>
</Columns>
<pagerSettings Mode="NumericFirstLast" />
</asp:GridView>
aspx.vb :( Bind该列的方法)
aspx.vb:(Bind Method for that column)
Private Sub GetActionsTaken(ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs, ByVal curActionItemId As Int32)
Dim flexdata As DataSet = Nothing
flexdata = CType(Session("flexdata"), DataSet)
Dim myRows() As DataRow
Dim sbData As New System.Text.StringBuilder
Dim dbhelper As New DbHelper
myRows = flexdata.Tables(table.PastActivities).Select("actionitemid=" & curActionItemId)
For Each myRow As DataRow In myRows
sbData.Append("" & dbhelper.ConvertDrString(myRow.Item(colActivity.occurredOn)) & " - " & "" & dbhelper.ConvertDrString(myRow.Item(colActivity.personFullName)) & "<br>")
sbData.Append(dbhelper.ConvertDrString(myRow.Item(colActivity.activity)) & "<br><br>")
Next
e.Row.Cells(gridCol.ActionsTaken).Text = sbData.ToString
dbhelper = Nothing
End Sub
以前,数据直接传递到Column的文本,如上面aspx.vb文件中所示。但是现在我在同一列中有一个文本框,我想将相同的数据绑定到该文本框。任何帮助将不胜感激。谢谢!
我尝试了什么:
ASP新手。净。试图解决它。
Previously, the data was passed directly to the Column's text as shown above in the aspx.vb file. But now I have a textbox in the same column and I want to bind the same data with that textbox. Any help would be greatly appreciated. Thanks!
What I have tried:
New to ASP.Net. Trying to solve it.
推荐答案
这是一个简单的例子。这是在C#中,因为我不熟悉VB.NET。但这应该是你可以直接跟随和转换。我已经更新了代码并添加了一个VB.NET等价物。
ASPX (为简单起见删除了样式):
Here's a quick example.This is in C# as I don't do VB.NET unfortunately. But this should be straight forward for you to follow and convert.I've updated the code and added a VB.NET equivalent.
ASPX (removed the styles for simplicity):
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false"
onrowdatabound="GridView1_RowDataBound">
<Columns>
<asp:BoundField DataField="Id" HeaderText="Id" />
<asp:BoundField DataField="Field1" HeaderText="Field 1" />
<asp:TemplateField>
<ItemTemplate>
<tr>
<td colspan="3">
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</td>
</tr>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
代码背后:
Code Behind:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebFormDemo
{
public partial class Repeater : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e) {
if (!IsPostBack) {
BindGridView();
}
}
private void BindGridView() {
var data = GetSampleData();
GridView1.DataSource = data;
GridView1.DataBind();
}
private List<Student> GetSampleData() {
List<Student> students = new List<Student>();
students.Add(new Student() { Id = 1, Field1 = "SomeText 1", Field2 = "SomeText 1" });
students.Add(new Student() { Id = 2, Field1 = "SomeText 2", Field2 = "SomeText 2" });
students.Add(new Student() { Id = 3, Field1 = "SomeText 3", Field2 = "SomeText 3" });
return students;
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) {
if (e.Row.RowType == DataControlRowType.DataRow) {
//access first column
int id = Convert.ToInt32(e.Row.Cells[0].Text);
//access the TexBox
TextBox tb = (TextBox)e.Row.FindControl("TextBox1");
tb.Text = "Assign whatever value here";
}
}
}
public class Student
{
public int Id { get; set; }
public string Field1 { get; set; }
public string Field2 { get; set; }
}
}
关键是 RowDataBound
事件。当网格与数据绑定时会触发该事件,这就是为什么它是访问 TextBox
并为其分配值的理想位置。
这是使用转换工具的VB.NET等价物:
The key there is the RowDataBound
event. That event is fired when a grid is bounded with data, that's why it's a perfect place to access your TextBox
and assign a value to it.
Here's the VB.NET equivalent using a convert tool:
Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Namespace WebFormDemo
Public Partial Class Repeater
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
If Not IsPostBack Then
BindGridView()
End If
End Sub
Private Sub BindGridView()
Dim data = GetSampleData()
GridView1.DataSource = data
GridView1.DataBind()
End Sub
Private Function GetSampleData() As List(Of Student)
Dim students As List(Of Student) = New List(Of Student)()
students.Add(New Student() With {
.Id = 1,
.Field1 = "SomeText 1",
.Field2 = "SomeText 1"
})
students.Add(New Student() With {
.Id = 2,
.Field1 = "SomeText 2",
.Field2 = "SomeText 2"
})
students.Add(New Student() With {
.Id = 3,
.Field1 = "SomeText 3",
.Field2 = "SomeText 3"
})
Return students
End Function
Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then
Dim id As Integer = Convert.ToInt32(e.Row.Cells(0).Text)
Dim tb As TextBox = CType(e.Row.FindControl("TextBox1"), TextBox)
tb.Text = "Assign whatever value here"
End If
End Sub
End Class
Public Class Student
Public Property Id As Integer
Public Property Field1 As String
Public Property Field2 As String
End Class
End Namespace
这篇关于在gridview中,我想将数据绑定到文本框。我怎么做?任何帮助将不胜感激。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!