2015-04-08 4 views
1

У меня есть следующий повторитель, завернутые в панели UpdatePass значения из Repeater строки в текстовое поле на нажатие кнопки в asp.net C#

 <asp:ScriptManager ID="ScriptManager1" runat="server" /> 
     <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional"> 
      <Triggers> 
       <asp:AsyncPostBackTrigger ControlID="DropDownList1" EventName="SelectedIndexChanged" /> 
      </Triggers> 
      <ContentTemplate> 
       <asp:Repeater ID="skillTable" runat="server"> 
        <ItemTemplate> 
         <table class="table table-hover"> 
          <tr> 
           <td> 
            <asp:ImageButton runat="server" AutoPostBack="True" ID="skillButton" OnClick="skillButton_Click" CommandArgument="<%# Eval(DropDownList1.SelectedValue)%>" class="addText btn btn-success" ImageUrl="~/img/addbut.png" /></td> 
           <td><asp:Label runat="server" id="skillName" Text='<%# DataBinder.Eval(Container.DataItem, DropDownList1.SelectedValue) %>'></asp:Label></td> 
          </tr> 
         </table>     
        </ItemTemplate> 
       </asp:Repeater> 
      </ContentTemplate> 
     </asp:UpdatePanel> 

Он выгоняет информацию из моей базы данных отлично, и она имеет кнопку прямо перед текстом в каждой строке.

Проблема, которая у меня есть, заключается в том, что мне нужно каждую кнопку в каждой строке, чтобы при щелчке добавить конкретную строку кода в эту строку в текстовое поле.

  foreach (RepeaterItem item in skillTable.Items) 
    { 
     string skill = item.DataItem.ToString(); 
     string text = skillList.Text; 
     if (!string.IsNullOrEmpty(text)) 
     { 

      if (!text.Contains(skill)) 
      { 
       text += " | " + skill; 
       skillList.Text = text; 
      } 
     } 
     else 
     { 
      text = skill; 
      skillList.Text = text; 
     } 
    } 
    UpdatePanel2.Update(); 

Я также попробовал этот путь,

 protected void skillTable_ItemDataBound(object sender, RepeaterItemEventArgs e) 
{ 
    int d = 0; 
     if(DropDownList1.SelectedValue == "Business and Finance") 
     { 
      d = 1; 
     } 
     else if(DropDownList1.SelectedValue == "Computers and Technology") 
     { 
      d = 2; 
     } 
     else if (DropDownList1.SelectedValue == "Education") 
     { 
      d = 3; 
     } 
     else if (DropDownList1.SelectedValue == "Customer Service") 
     { 
      d = 4; 
     } 
     DataRowView drv = (DataRowView)e.Item.DataItem; 
     string skill = drv[d].ToString(); 
     Session["Table"] = skill; 

} 
protected void skillButton_Click(object sender, System.Web.UI.ImageClickEventArgs e) 
{ 
    string skill = (string)(Session["Table"]); 
    string text = skillList.Text; 
    if (!string.IsNullOrEmpty(text)) 
    { 

     if (!text.Contains(skill)) 
     { 
      text += " | " + skill; 
      skillList.Text = text; 
     } 
    } 
    else 
    { 
     text = skill; 
     skillList.Text = text; 
    } 
    UpdatePanel2.Update(); 
} 

, но ни один из них, кажется, правильно работать. Любой совет? Я до сих пор не использовал повторителей, поэтому, если есть какой-либо другой инструмент, который будет работать лучше, я открыт для предложений.

Заранее благодарен!

+0

возможно эта ссылка может помочь ... http://stackoverflow.com/questions/16606208/looping-through-repeater-items – MethodMan

+0

@MethodMan Спасибо, я пробовал эту ссылку, однако, что возвращает все в ретрансляторе Таблица. Я просто хочу, чтобы одна конкретная строка была вытащена, когда вы нажимаете кнопку этой конкретной строки. Спасибо за помощь, хотя! – Krytix

ответ

2

Так что я понял. То, что мне не хватало, было способом, чтобы мой сайт специально сузил то, что мне нужно. Я добавил код, ударивший по моему методу кликов, и избавился от метода ItemDataBound, и он работал как шарм.

Button button = (sender as Button); 
    string commandArgument = button.CommandArgument; 
    RepeaterItem item = button.NamingContainer as RepeaterItem; 
    var workText = (Label)item.FindControl("workName1") as Label; 
    string work = workText.Text; 
    string text = workDetails1.Text; 
    if (text == " ") 
     text = ""; 

    if (text == "") 
    { 
     if (!text.Contains(work)) 
     { 
      text +="\u2022 " + work; 
      workDetails1.Text = text; 
     } 
    } 
    else if (!string.IsNullOrEmpty(work)) 
    { 
     if (!text.Contains(work)) 
     { 
      text += "\n\u2022 " + work; 
      workDetails1.Text = text; 
     } 
     else 
     { 
      workDetails1.Text = text; 
     } 
    } 
    UpdatePanel4.Update(); 

Надеюсь, это поможет кому-то!

 Смежные вопросы

  • Нет связанных вопросов^_^