2016-04-24 35 views
0

你好開發商我真的需要解決這個問題,因爲它的最後一年社交網站項目的問題是文本框內中繼消失時按鍵回傳

我有一個Repeater,裏面直放站我有一個文本框和一個按鈕 和我有中繼器外的標籤,其中我展示文本框的值,但 當我按一下按鈕,它回發到服務器比標籤消失在這裏我想告訴文本框的值

這是我嘗試:

ASPX頁面

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="index.aspx.cs" Inherits="SocialSite.index" MasterPageFile="~/Master.Master"%> 



<asp:Repeater runat="server" ID="repeater1"> 
    <ItemTemplate> 

    <asp:TextBox runat="server" ID="txtComment" placeholder="write a comment..."></asp:TextBox> 

    <asp:Button runat="server" ID="btnComment" Text="Post" CssClass="btn btn-primary btn-sm" OnClick="btnComment_Click"/> 

    </ItemTemplate> 
    </asp:Repeater> 

這是Repeater控件

<asp:Label runat="server" ID="lblMsg" Text="Not working" ForeColor="Red"> </asp:Label> 

代碼隱藏外面的標籤:

這是頁面加載事件在那裏我結合中繼器

private void Page_Load(object sender, System.EventArgs e) 
    { 
     if (!IsPostBack) 
     { 
      DataTable dt = Helper.ExecutePlainQuery("select * from post inner join userregistration on post.uid=userregistration.uid inner join profile on userregistration.uid=profile.uid order by postid DESC"); 
      repeater1.DataSource = dt; 
      repeater1.DataBind(); 
     } 
    } 

按鈕點擊:

protected void btnComment_Click(object sender, EventArgs e) 
    { 
     foreach (RepeaterItem item in repeater1.Items) 
     { 
      if (item.ItemType == ListItemType.Item || item.ItemType == ListItemType.AlternatingItem) 
      { 
       TextBox txtName = (TextBox)item.FindControl("txtComment"); 
       if (txtName != null) 
       { 
        lblMsg.Text = txtName.Text; 
       } 
      } 
     } 
    } 

在這裏,我調試的按鈕的代碼效果很好我的意思是在分配給標籤/ lblMsg但前面的文本框中輸入值結束標籤剛剛消失

我搜索這個問題,但沒有得到解決方案....有人說,使用頁面初始化... Page OnInit .... V iewStateMode .... ....的EnableViewState但不是爲我工作

請點擊按鈕後,再次幫助其我的最後一年的項目問題

回答

0

你可以嘗試做這樣的:

protected void btnComment_Click(object sender, EventArgs e) 
{ 
    Button btnComment = sender as Button; 
    RepeaterItem item = btnComment.NamingContainer as RepeaterItem; 
    TextBox txtComment = item.FindControl("txtComment") as TextBox; 
    lblMsg.Text = txtComment.Text; 
} 

在你的原代碼,標籤文本設置在轉發器的每個文本框的內容的循環中,直到被設置爲最終的文本框。結果,標籤總是顯示最後一個轉發器項目的文本,無論點擊哪個按鈕。如果最後一個TextBox爲空,則標籤「消失」。

+0

哦,是的,謝謝你的工作 – Heartlion

+0

不客氣!請將答案標記爲已接受(使用複選標記)。 – ConnorsFan

+0

完成........ :) – Heartlion

0

綁定的中繼器。你可以讓一個單獨的方法綁定一箇中繼器,並從Page_LoadbtnComment_Click稱之爲:

protected void btnComment_Click(object sender, EventArgs e) 
    { 
     foreach (RepeaterItem item in repeater1.Items) 
     { 
      if (item.ItemType == ListItemType.Item || item.ItemType == ListItemType.AlternatingItem) 
      { 
       TextBox txtName = (TextBox)item.FindControl("txtComment"); 
       if (txtName != null) 
       { 
        lblMsg.Text = txtName.Text; 
       } 
      } 
     } 

DataTable dt = Helper.ExecutePlainQuery("select * from post inner join userregistration on post.uid=userregistration.uid inner join profile on userregistration.uid=profile.uid order by postid DESC"); 
      repeater1.DataSource = dt; 
      repeater1.DataBind(); 
    } 
+0

不工作.....同樣的問題 – Heartlion