2012-07-13 73 views
1

我需要一些帮助。如何在gridview中查找标签ID(ItemTemplate)和文本框ID(EditItemTemplate)?

在我的GridView控件(绑定到一个SqlDataSource)没有在项目模板和文本框(tbEditDI)在编辑项模板

<asp:GridView ID="gvDiscussItem" runat="server" AutoGenerateColumns="False" BackColor="White" BorderColor="Black" BorderStyle="Double" BorderWidth="1px" 
CellPadding="2" DataKeyNames="discussionID" DataSourceID="SQLDiscussItems" 
ForeColor="Black" ShowHeaderWhenEmpty="True" 
style="font-size: small" Width="600px" > 
<Columns> 
<ItemTemplate> 
<asp:Label ID="lblDI" runat="server" Text='<%# Bind("discussionItem") %>'></asp:Label> 
</ItemTemplate> 
<EditItemTemplate> 
<asp:TextBox ID="tbEditDI" runat="server" Text='<%# Bind("discussionItem") %>' 
TextMode="MultiLine" onkeyup="SettingEditHeightDI(this);"></asp:TextBox> 
</EditItemTemplate> 
</asp:GridView> 

我怎样才能使标签的标签(lblDI)(lblDI )成为多行显示数据,文本框(tbEditDI)适合页面加载的所有文本?

我有这些代码可供我参考,帮助我制作标签多行,以及适合页面大小的文本框,但现在不适用,至于标签,我无法获得要在aspx.cs页面中使用的标签控件,以及通过javascript自动调整负载大小的文本框控件。

// to auto resize the textbox on pageload automatically using javascript 
    document.getElementById("<%=tbAgenda.ClientID%>").style.height = document.getElementById("<%=tbAgenda.ClientID%>").scrollHeight + "px"; 

// to display multiline label in the aspx.cs page 
lblAgenda.Text = recentMinute.Agenda.Replace("\n", "<br/>"); // displays multilines textbox texts in a multiline label. For retrieval from database 

请帮助我,我有这个一个学术项目带来很大麻烦。 :'(

回答

0

你可以在你的GridView的RowDataBound事件添加此服务器端代码是这样的:

public void gvDiscussItem_RowDataBound(Object sender, GridViewRowEventArgs e) 
{ 

    if(e.Row.RowType == DataControlRowType.DataRow) 
    { 
     // find your control... 
     var lblDI = e.Row.FindControl("lblDI") as Label; 
     if (lblDI != null) 
     { 
     //fill the label and add the break-lines. 
     lblDI.Text = DataBinder.Eval(Container.DataItem, "discussionItem").ToString().Replace("\n","<br />"); 
     } 

    } 
} 
0
Try this 

For TextBox Control 

Textbox EditTextBox = (TextBox)gvDiscussItem.FindControl("tbEditDI"); 

For Label Control 

Label EditTextBox = (Label)gvDiscussItem.FindControl("lblDI");