2015-04-25 121 views
2

我在中繼器控件內的中繼器控件和外部中有一個隱藏字段。下面是我有的asp.code。ASP.NET中繼器控制 - 獲得中繼器控制內的Hiddenfield值

<asp:Repeater ID="rptAccordian" runat="server" OnItemDataBound="rptAccordian_ItemDataBound"> 
 
    <ItemTemplate> 
 
    <div class="s_panel"> 
 
     <h1> 
 
      <a href="#" data-content="Tool tip"><%# Eval("Name") %></a> 
 
     </h1> 
 
     <div> 
 
     <p> 
 
      <small><span style="font-family: 'Segoe UI'; font-weight: bold;">Category Objective: </span><span style="font-family: 'Segoe UI'"><%# Eval("Objective") %></span></small> 
 
     </p> 
 
     <p> 
 
      <small><span style="font-family: 'Segoe UI'; font-weight: bold;">Category Riskscore: </span> 
 
       <code><%# Eval("Score") %><span>%</span></code></small> 
 
     </p> 
 
     <p> 
 
      <code> 
 
       <img src="Content/img/add.png" /><asp:LinkButton ID="Add" runat="server">Add Question</asp:LinkButton> 
 
      </code> 
 
     </p> 
 
     <asp:HiddenField ID="hdnCategoryID" runat="server" Value='<%# Bind("CategoryID") %>' /> 
 
     </div> 
 
    </ItemTemplate> 
 
</asp:Repeater> 
 
<div id="modalpopup"> 
 
    <asp:Button ID="btnInsertQuestion" runat="server" Text="Save" OnClick="btnInsertQuestion_Click" /> 
 
</div>

我的後端代碼如下。

protected void btnInsertQuestion_Click(object sender, EventArgs e) 
{ 
    HiddenField hf = (HiddenField)rptAccordian.FindControl("hdnCategoryID"); 
    catID = Convert.ToInt16(hf.Value); 
    Response.Write("ID is") + catID; 
} 

有13箇中繼器,每個中繼器都有不同的CategoryID。我有一個鏈接按鈕稱爲添加每個中繼器,當我按下該按鈕,我將打開一個模式彈出,它將有一個按鈕。單擊該按鈕時,我需要顯示屬於該中繼器控件的相應CategoryID,在該控件中單擊ADD鏈接按鈕。

但是,hiddenfield hf顯示爲空,我無法獲得該手風琴隱藏字段的值。

+0

使用此鏈接將會有用。 http://stackoverflow.com/questions/29862571/dropdownlist-is-not-showing-the-selected-value –

+0

你正在尋找一個帶有「hiddenid」id的'HiddenField',但你的aspx裏的實際id是「 hdnCategoryID」。 – dario

回答

1

我已經使用jQuery獲得了我的答案。我發現控件hdnCategoryID和它的值使用jQuery,我將該值分配給一個新的隱藏字段,並檢索到我的點擊事件的值。

2

你必須得到中繼項目訪問hiddenfield:

protected void btnInsertQuestion_Click(object sender, EventArgs e) 
     { 
      for (int i = 0; i < rptAccordian.Items.Count; i++) 
      { 
       var item = rptAccordian.Items[i]; 

       var hf = item.FindControl("hdnCategoryID") as HiddenField; 
       var val = hf.Value; 
      } 
     } 

修訂

protected void Add_Click(object sender, EventArgs e) 
     { 
      var lb = sender as LinkButton; 
      var par = lb.Parent.FindControl("hdnCategoryID"); 

     } 
+0

嗨,試着用你的代碼。然而,我在for(int i = 0; i

+0

它看起來像項集合爲空。你是否在回發數據綁定中繼器? – Legends

+0

是的。我在頁面加載中綁定中繼器(!this.IsPostBack) –

0

你可以得到不使用hiddenfield。你需要在這個中繼器中聲明Add LinkBut​​ton。

<asp:LinkButton ID="Add" runat="server" CommandArgument = '<%# Bind("CategoryID")'%> OnClick = "Add_Click">Add Question</asp:LinkButton>

你已經寫代碼btnInsertQuestion單擊按鈕,在這裏,你要求得到CategoryID中添加按鈕的點擊,所以我假設你的要求是正確的,但您鍵入別的東西。

要在Add按鈕點擊中獲得CategoryId,您需要這樣寫。

protected void Add_Click(object sender, EventArgs e) 
{ 
    //Get the reference of the clicked button. 
    LinkButton button = (sender as LinkButton); 

    //Get the command argument 
    string cat_id = button.CommandArgument; 
    // Type cast to int if application and use it. 
}