在gridview中,我想将数据绑定到文本框。我怎么做? [英] In the gridview, I want to bind data to a textbox. How do I do that?

查看:71
本文介绍了在gridview中,我想将数据绑定到文本框。我怎么做?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的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新手。 Net



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

推荐答案

根据我们的评论讨论,这里有一个快速演示供您参考:



ASPX:

Based on our comments discussion, here's a quick demo for your reference:

ASPX:
<asp:Repeater ID="Repeater1" runat="server">
        <HeaderTemplate>
            <table>
        </HeaderTemplate>
        <ItemTemplate>
                 <tr>
                    <td><%# DataBinder.Eval(Container.DataItem, "Id") %></td>
                    <td><%# DataBinder.Eval(Container.DataItem, "Field1") %></td>
                    <td><%# DataBinder.Eval(Container.DataItem, "Field2") %></td>
                    <td><%# DataBinder.Eval(Container.DataItem, "Field3") %></td>
                 </tr>
                 <tr>
                   <td colspan="4"><%# DataBinder.Eval(Container.DataItem, "Field4") %></td>
                 </tr>
        </ItemTemplate>
        <FooterTemplate>
            </table>
        </FooterTemplate>
</asp:Repeater>



背后的代码:


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) {
                BindRepeater();
            }

        }

        private void BindRepeater() {
            var data = GetSampleData();

            Repeater1.DataSource = data;
            Repeater1.DataBind();
        }

        private List<Student> GetSampleData() {
            List<Student> students = new List<Student>();
            students.Add(new Student() { Id = 1, Field1 = "SomeText 1", Field2 = "SomeText 1", Field3 = "SomeText 3", Field4="SomeText with more contents here and there." });
            students.Add(new Student() { Id = 2, Field1 = "SomeText 2", Field2 = "SomeText 2", Field3 = "SomeText 2", Field4 = "SomeText with more contents here and there." });
            students.Add(new Student() { Id = 3, Field1 = "SomeText 3", Field2 = "SomeText 3", Field3 = "SomeText 3", Field4 = "SomeText with more contents here and there." });

            return students;
        }
    }

    public class Student
    {
        public int Id { get; set; }
        public string Field1 { get; set; }
        public string Field2 { get; set; }
        public string Field3 { get; set; }
        public string Field4 { get; set; }
    }
}





Field4 以上示例应显示在条目的每一行下方。



希望有所帮助!



Field4 in the example above should display below each row of the entry.

Hope that helps!


这篇关于在gridview中,我想将数据绑定到文本框。我怎么做?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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