2012-07-13 72 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");