2016-03-31 1 views
0

私のオンラインショッピングカートプロジェクトの問題に直面しています.... 問題は、テキストボックスの値が表示されている理由リピータ内のTextBoxにアクセスするとヌルが表示される

protected void repeater1_ItemCommand(object source, RepeaterCommandEventArgs e) 
    { 
     if (e.CommandName == "addtoCartName") 
     { 

      foreach (RepeaterItem item in repeater1.Items) 
      { 
       TextBox txtName = (TextBox)item.FindControl("txtQuantity"); 
       if (txtName!= null) 
       { 

        strText = strText + ", " + txtName.Text; 
        Response.Write("Text =" + strText); 


       } 
      } 
     } 

がASPX:

リピータ制御... は、私は後ろの

コードをコードの下に、これを使用しますが、それはnull値を示したよりも私はそのテキストボックスにいくつかの値を入力したときに....

<asp:Button runat="server" ID="addtoCart" Text="Add to Cart" CommandName="addtoCartName" UseSubmitBehavior="false" /> 

TextBox position...

And here the c# code...

0あなたはそれが正しい行のテキストボックスコントロールにアクセスしています確認することができないような問題を引き起こしている おかげで私は、Repeaterコントロールをループ思う

+0

txtQuantityは別のコンテナに配置されていますか?あなたの質問にaspx情報を拡張します。 –

+0

私は { 文字列のpid =場合、Request.QueryString [「PID」] plzはそれをチェック – Heartlion

+0

@Heartlion場合はどのようにあなたが ' – Webruster

答えて

1

を返信しなければならないしてください。あなたは、イベントが発生された行からテキストボックスを取得する必要があります:

protected void repeater1_ItemCommand(object source, RepeaterCommandEventArgs e) 
{ 
    if (e.CommandName == "addtoCartName") 
    { 
     TextBox txtName = (TextBox)e.Item.FindControl("txtQuantity") 

     if (txtName!= null) 
     { 
      strText = strText + ", " + txtName.Text; 
      Response.Write("Text =" + strText); 
     } 
    } 
} 
+0

私はこの方法をチェックしましたが、同じ問題がありました.... – Heartlion

0

あなたがリピーターアイテムのアイテムタイプをチェックする必要があるので、あなたはヘッダー/フッターなどでのListItemのItemTemplateに内部のTextBoxを持っていません。 試してみる

foreach (RepeaterItem item in repeater1.Items) 
    {    
    if (item.ItemType == ListItemType.Item || item.ItemType == ListItemType.AlternatingItem) 
    { 
      TextBox txtName = (TextBox)item.FindControl("txtQuantity"); 
      if (txtName!= null) 
      { 
      strText = strText + ", " + txtName.Text; 
      Response.Write("Text =" + strText); 
      } 
     } 
    } 
+0

返信のためのthnxはnull値を返します 例:Text = – Heartlion

+0

最初のif条件には入りましたか? –

+0

はい最初のif条件に入るだけでなく、別のif条件とその本文に入力するか、テキストボックスに何らかの値を入力するかどうかを指定します。 何か別の方法がありますか? – Heartlion

関連する問題