2013-05-17 48 views
5

我有一個是建立像下面的一箇中繼器循環:通過中繼器項目

ListProducts = db.GetDataTable("select * from Products where Id in (" + selectedValues + ")"); 

     rptItems.DataSource = ListProducts; 
     rptItems.DataBind(); 

然後多餘的東西用做:

protected void rptItems_ItemDataBound(object sender, 
            System.Web.UI.WebControls.RepeaterItemEventArgs e) 
    { 
     DataRowView nRow = null; 

     switch (e.Item.ItemType) 
     { 
      case ListItemType.Item: 
      case ListItemType.AlternatingItem: 
       nRow = (DataRowView)e.Item.DataItem; 
       ((Literal)e.Item.FindControl("ltrDescription")).Text = "" + nRow["Description"]; 
       ((Literal)e.Item.FindControl("ltrName")).Text = "" + nRow["Name"]; 

       if ("" + nRow["HasAmount"] == "False") 
       { 
        ((Panel)e.Item.FindControl("pnlAmount")).Visible = false; 
       } 

       break; 
     } 
    } 

<asp:Repeater runat="server" ID="rptItems" OnItemDataBound="rptItems_ItemDataBound"> 
      <ItemTemplate> 
      <div class="span12 grey-box"> 
         <div class="hero-block3"> 
          <div class="row show-grid"> 
           <div class="span9"> 
            <div class="hero-content-3"> 
             <h2><asp:Literal ID="ltrName" runat="server"></asp:Literal></h2> 
             <p><asp:Literal ID="ltrDescription" runat="server"></asp:Literal></p> 
            </div> 
           </div> 
           <div class="span3"> 
           <asp:Panel ID="pnlAmount" runat="server"> 
            <div class="tour-btn" id="divAmount" runat="server"> 
             <small>How Many?<br /></small> 
             <asp:TextBox runat="server" ID="tbox" Width="40"></asp:TextBox> 
            </div> 
            </asp:Panel> 
           </div> 
          </div> 
         </div> 
        </div> 
        <div class="clear-both"></div> 
        <br /> 

      </ItemTemplate> 
     </asp:Repeater> 

它使用綁定

但是,現在在頁面的onclick事件中,我試圖保存所存儲的信息 - 這是我迄今爲止所做的,但它alw AYS都似乎是空的,我不能.text的等來的(TextBox)item.FindControl("tbSelected");

我的繼承人環路我試圖在點擊末尾添加:

protected void doStageThree(object sender, EventArgs e) 
     { 
      foreach (RepeaterItem item in rptItems.Items) 
      { 
       if (item.ItemType == ListItemType.Item || item.ItemType == ListItemType.AlternatingItem) 
       { 
        var tbSelected = (TextBox)item.FindControl("tbSelected"); 
        var lblDescription = (Literal)item.FindControl("ltrDescription"); 
        var lblName = (Literal)item.FindControl("ltrName"); 

       } 
      } 
     } 
+0

更換

foreach (RepeaterItem item in rptItems.Items) 

嘗試 「的foreach(在rptItemss.Items控制C)」,而不是RepeaterItems,然後((文本框)c.FindControl(「tbSelected」))。文本 –

+0

看起來像它可以工作,我將如何保護if(TextBox)c.FindControl(「tbSelected」)爲null(現在是總是顯示) – TMB87

+0

var text =(c.FindControl(「tbSelected」)== null? 「Empty」:((TextBox)c.FindControl(「tbSelected」))。Text; 玩弄調試器,看看有什麼作用 –

回答

2

它總是空,因爲有沒有TextBox ID爲tbSelected

<asp:TextBox runat="server" ID="tbox" Width="40"></asp:TextBox> 

將其更改爲:

var tbSelected = (TextBox)item.FindControl("tbox"); 

爲了保護您的代碼空使用關鍵字as

var tbSelected = item.FindControl("tbox") as TextBox; 

if (tbSelected != null) 
{ 
    //textbox with id tbox exists 
    tbSelected.Text = "your text"; 
} 
+0

當然,傻我!謝謝! – TMB87

+0

但是,這樣說 - tbselected always =「」 – TMB87

+0

如果你在'PostBack'事件之後沒有在'Page_Load'內重新綁定你的中繼器,因爲它會清除所有的值,經常發生:) – gzaxx

2

嘗試用

foreach (Control c in rptItems.Items) 
    { 
     if(c.FindControl("tbSelected") != null) 
     { 
      var selectedText = ((TextBox)c.FindControl("tbSelected")).Text; 
     } 
    } 
+0

是的,正如gzaxx指出的,沒有下一個名爲tbSelected的盒子,它在你的示例代碼中被稱爲tBox,它應該有一個更像tbHowManyItems的描述性名稱 –